简体   繁体   中英

How to extract specific parts of a string JAVA

If for example I have this string:

<option value="BA7233_550" data-maxavailable="22.0" data-maxorderqty="10.0" data-status="IN_STOCK" data-context="sizes:36"> 37 1/3 

and I would like to grab this part of the string: BA7233_550 and save it to a variable.

Could you please help me?

You can obtain a part of a String using either of the following String class methods:

  1. Method Name : substring(int firstIndex)
    Description : Returns the part of this String from the character at firstIndex to the last character (inclusive).
  2. Method Name : substring(int firstIndex, int lastIndex)
    Description : Returns the part of this String from the character at firstIndex to the character at lastIndex - 1 (inclusive).

You can extract the String portion you want with the following code (that portion is saved to the stringExcerpt variable):

String initialString = "<option value=\"BA7233_550\" data-maxavailable=\"22.0\" data-maxorderqty=\"10.0\" data-status=\"IN_STOCK\" data-context=\"sizes:36\"> 37 1/3 ";
String stringExcerpt = initialString.substring(15, 25);

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 :/ ).

I'm assuming that the XML fragment in your question is part of a valid XML document. For reading XML you should use an XML parser and not a regular expression .

Here is an example of getting that value using an XML parser (note that I added the close option tag at the end to make the string valid XML):

String xmlString = "<option value=\"BA7233_550\" data-maxavailable=\"22.0\" data-maxorderqty=\"10.0\" data-status=\"IN_STOCK\" data-context=\"sizes:36\"> 37 1/3 </option>";
InputSource is = new InputSource(new StringReader(xmlString));
DOMParser dp = new DOMParser();
dp.parse(is);
Document doc = dp.getDocument();
NodeList nl = doc.getElementsByTagName("option");
NamedNodeMap nnm = nl.item(0).getAttributes();
String value = nnm.getNamedItem("value").getFirstChild().getTextContent();

use string methods

for getting a part of a string, use the method

<your string>.substring(<start index>, <end index>)


additionally, you can use the method

<your string>.indexOf(<character>)
in order to get the index of a given character. this can help you find index values for your string if you don't know them or want to use a character as the reference instead.

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