Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an string like"2=33=file". I can get the first number from the string by

char[] cArray = new char[4];
int nValue = Integer.parseInt(String.valueOf(cArray[0]));

How can i get the value "33" from the string .Also if i have in String like "2=3=file" also i want to get the value "3". Sorry if this is a very simple question to ask or may be this being very specific case.

share|improve this question

closed as too broad by Jens, LordT, tripleee, suspectus, EdChum Aug 11 '14 at 7:54

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs. If this question can be reworded to fit the rules in the help center, please edit the question.

    
Did you do any research? There are probably a hundred questions on this topic here on SO. –  Keppil Aug 10 '14 at 15:50
    
I got the "lastIndexOf" but couldnt distinguish 1-digit & 2-digit value to extract from the string –  sneha nambiar Aug 10 '14 at 15:51
2  
search for regex in java or String.split function. –  Jens Aug 10 '14 at 15:53

2 Answers 2

up vote 4 down vote accepted

Try this :

String str = "2=33=file";
String[] arr = str.split("=");
int num1 = Integer.parseInt(arr[0]);       // value - 2
int num2 = Integer.parseInt(arr[1]);       // value - 33
String s = arr[2];                         // value - "file"
share|improve this answer

I hope my example will help you too.

public class Solution{
public static void main(String[] args){
    String s = new String("beard=true;age=20;");
    System.out.println(parse(s, "beard"));
    System.out.println(parse(s, "age"));
}

static String parse(String where, String what){
    String s;
    if(!where.contains(what)) return null;
    int i = where.indexOf(what) + what.length() + 1;
    where = where.substring(i);
    i = where.indexOf(";");
    s = where.substring(0, i);
    return s;
}

}

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.