If you want something simple:
String value = myString.substring(myString.indexOf("\"")+1,
myString.indexOf("\"", myString.indexOf("\"")+1));
The code above utilizes the String.substring() method in conjunction with the String.IndexOf() method. Since the data you want to pull out of the string is the first bit of data contained within the string with double quote marks it makes it relatively easy.
In the example code above we use the String.substring() method to gather our sub-string from within the string. To get this sub-string we need to supply the String.substring() method with two specific arguments, the Start Index of where the sub-string starts within the string and the End Index of where the sub-string ends within the string. The String.substring() method is a Overloaded method which means that there are other methods with the same name that allow you to manipulate the string a little differently since they contain different argument requirements. The method we are using is the:
String.substring(startIndex, endIndex)
To get these Index values we use the String.indexOf() method against the double quote marks first encountered which happens to be where we want to extract our data. The String.indexOf() method always retrieves the index of the first supplied item encountered within the string it is applied against unless you use the overloaded version of the String.indexOf() method which allows for the fromIndex argument. Yes, the String.indexOf() method is also Overloaded and we are using two versions of it:
the
String.indexOf(String)
method and the
String.indexOf(String, fromIndex)
method. We use the fromIndex so as to ensure we catch the double quote mark after the actual first double quote marks within the string.
In reality we could have done this another way using both the String.substring() and String.indexOf() methods since it looks like your data string will always follow the same data format:
String myString = "<option value=\"BA7233_550\" data-maxavailable=\"22.0\" data-maxorderqty=\"10.0\" data-status=\"IN_STOCK\" data-context=\"sizes:36\"> 37 1/3";
String value = myString.substring(myString.indexOf("<option value=\"")+15,
myString.indexOf("\" data-maxavailable="));
In this example I have also shown your supplied string we are working against for clarity. You can also quickly see that we've only used the one type of the String.indexOf() method and this is because your data field names are unique within the string. To get our Indexes for the String.substring() method we just use the String.indexOf(String) method and as arguments we simply supply the field names where our desired data lays between.
Do you also notice the +15? We need to add 15 to the index value since the String.indexOf() method will always provide the index from where the string argument supplied begins within the work String (myString). Since the string we supplied to String.indexOf() method is 15 characters long we need to add that to the returned index value (Note: we do not count the escape character (\) as a character). This isn't necessary for our String.substring() method endIndex argument.
Using this principle you can basically pull out whatever data you like from your work string. Let's say we want the data which is related to the data-status= field within the work string:
String status = myString.substring(myString.indexOf("data-status=\"")+13,
myString.indexOf("\" data-context="));
What is with this \" all over the place?
To represent double quote marks within a Java String they must be escaped with the escape character which is the Backslash (\). In general, Java takes care of this for you when processing Strings from a file but you need to be aware of it when coding for them. To ensure your strings which are meant to contain double quote marks are properly escaped you can do this:
myString = myString.replaceAll("\"","\\\"");
Now you can make your own custom parser (or whatever :/ ).