0

i have have a string looks :

 mystring = "<EQHO state="degraded"...> at /NE[1]/EQHO[2]/@state to <EQHO state="working"...> at /NE[1]/EQHO[1]/@state"

and i want to get this value :

value="NE[1]/EQHO[1]"

how can i achieve that ? thanks

6
  • What kind of format is this? XML? Commented Sep 13, 2015 at 12:33
  • @Flown its a simple string Commented Sep 13, 2015 at 12:36
  • Where do you get this "simple" String? Because it's not a simple one. Maybe give it a try with regex Commented Sep 13, 2015 at 12:38
  • Try this: mystring.substring(mystring.lastIndexOf("at /")+4, mystring.lastIndexOf("/@")) but you probably should use a more generic solution. Commented Sep 13, 2015 at 12:41
  • this string is the result of comparing two xml files using xmlunit . the problem that i can get the first value ( NE[1]/EQHO[2] ) using substring(mystring.indexOf("> at /")+6, mystring.indexOf("/@")) but i dont know how to get the value (NE[1]/EQHO[1]) Commented Sep 13, 2015 at 12:49

2 Answers 2

1

Try this:

mystring.substring(mystring.lastIndexOf("at /")+4, mystring.lastIndexOf("/@")) 

but you probably should use a more generic solution. To extract all the section that have this format you can use something like this:

String mystring = "<EQHO state=\"degraded\"...> at /NE[1]/EQHO[2]/@state to <EQHO state=\"working\"...> at /NE[1]/EQHO[1]/@state";
ArrayList<String> values = new ArrayList<String>();
while(mystring.indexOf("at /") < mystring.indexOf("/@")){
    String val = mystring.substring(mystring.indexOf("at /") + 4, mystring.indexOf("/@"));
    values.add(val);
    mystring = mystring.substring(mystring.indexOf("/@")+2);
}
System.out.println(values);
Sign up to request clarification or add additional context in comments.

Comments

0

You can change the value of a string like this

mystring = "NE[1]/EQHO[1]";

Remember to include the semicolon!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.