简体   繁体   中英

Grab specific text from string

I am needing to grab specific text in the following string:

"{client_unique_identifier=cdakLs1W7oI+M3z+CzFv1lEUkaY=, cldbid=2086, client_nickname=sman}"

What I need is it to return their nickname cldbid which is "2086" as a int

static int getNumber(){
    int result = 0;
    Pattern p = Pattern.compile("cldbid=(\\d+)");
    Matcher m = p.matcher("client_unique_identifier=cdakLs1W7oI+M3z+CzFv1lEUkaY=, cldbid=2086, client_nickname=sman");
    if(m.find()){
        result = Integer.valueOf(m.group(1));
    }
    return result;
}

Assuming the data is always in the format you listed.

//property = 'cldbid='
public int getUserProperty(String property, String data) {
    int start = data.indexOf(property)+property.length();
    int end = data.indexOf(',', start);

    return Integer.parseInt(data.substring(start, end));
}

However; I would implement this as a regex/pattern matching like previous poster suggested. Tutorial here. http://docs.oracle.com/javase/tutorial/essential/regex/

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