简体   繁体   中英

how to get specific int value from string in java [122356]

How to get all int value from a string value, eg [122356]. I tried this:

Query query=s.createQuery("select pid from Patient where emailid=? ");

String pid = query.setParameter(0, email).list().toString();
char abc= pid.charAt(1);
int patientid=Character.getNumericValue(abc);

You can get an int value from a string value by using parseInt, like so:

int num = Integer.parseInt(string_of_number);

You should validate the value that num has after this operation. You can read about it here: parseInt

You could simply do :

int num = Integer.parseInt("[122356]".replaceAll("[\\[\\]]", ""));

Here we first replace all occurences of [ and ] with empty space and then parse the string.

list() might return more than one record,you need to get the specify record,and it better to use Integer.parseInt() get int value

Query query=s.createQuery("select pid from Patient where emailid=? ");

//get value from the first record
String pid = query.setParameter(0, email).list().get(0).toString();
char abc= pid.charAt(1);
int patientid=Character.getNumericValue(abc);
//better to use this get int
//int patientid=Integer.parseInt(abc);

将String解析回Integer;

int num = Integer.parseInt("122356");

In case you know that the String representation of your number is enclosed in brackets, then you have to remove them before parsing. You can do it like this:

// replace brackets with an empty String, needs two operations due to two different brackets
String numberInBrackets = "[123456]";
String numberWithoutBrackets = numberInBrackets.replace("[", "");
String numberWithoutBrackets = numberWithoutBrackets.replace("]", "");
int number = Integer.parseInt(numberWithoutBrackets);

Alternatively, you cut them off using String.substring(int beginIndex, int endIndex) :

// cut off the trailing and leading characters of the String
String numberInBrackets = "[123456]";
String numberWithoutBrackets = numberInBrackets.substring(1, numberInBrackets.length() - 1);

If the String just contains cyphers, then just do

String number = "123456";
int number = Integer.parseInt(number);

至少有3种方法(其中一种是Nicolas K.指出的),至少对我来说,这是最合适的方法,但是您也可以使用类似的方法(假设字符串位于[int]模式中) 。

 int number = Integer.parseInt(yourData.substring (1, yourData.lastIndexOf(']')));

you can use this for cut "[" and "]".

Thank you .

Query q = s.createQuery(" select profileid from Userprofile where emailid=?");

    String profileid = q.setParameter(0, email).list().toString();
    int start = 0; // '(' position in string
    int end = 0; // ')' position in string
    for (int i = 0; i < profileid.length(); i++) {
        if (profileid.charAt(i) == '[') // Looking for '(' position in string
        {
            start = i;
        } else if (profileid.charAt(i) == ']') // Looking for ')' position in  string
        {
            end = i;
        }
    }
    String number = profileid.substring(start + 1, end);

    int pkst = Integer.parseInt(number);
    System.out.println("your number is :" + number);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM