0

I'm trying to find a way to cycle through a string and get data within two characters, for example... I have the following String.

String test = "<172>Lorem Ipsum";

Lets say I want the data that is in-between the two characters '<' & '>' So the result should be "172"

Now, if the string was going to be 3 digits inbetween these every time, using a sub-string would be fine, however that's not the case, as this string will be changing, so lets say this string could be

String test = "<9>Lorem Ipsum"

I would need the result to be "9"

How should I go about getting this information.

4
  • 2
    I guess it's find the start marker, the <, then find the next end marker >, then take a substring using those positions. Which part do you need help with? Commented Mar 9, 2014 at 10:30
  • @MarounMaroun For these simple cases probably regular expressions would be fine too. Commented Mar 9, 2014 at 10:32
  • To give you correct answer we would need more information about format used in your input. If it is XML/HTML then you should use parser for it. But if this format really is as simple as you show then regex can be used here easily. Commented Mar 9, 2014 at 10:34
  • 1
    What should the result be if there is more than one <...> in the string? Commented Mar 9, 2014 at 10:36

6 Answers 6

1

The code will be the following:

String test = "<172>Lorem Ipsum";
int index1 = test.indexOf('<');
int index2 = test.indexOf('>', index1);

String result = test.substring(index1 + 1, index2);
System.out.println("result = " + result);

And the result:

result = 172
Sign up to request clarification or add additional context in comments.

Comments

1
String data = test.substring(test.indexOf("<")+1,test.indexOf(">"));

Comments

1

You can use regexp to get the data you need. Something like this maybe

Pattern p = Pattern.compile("^<(\\d+)>");
Matcher m = p.matcher("<172>Lorem Ipsum");
if (m.find())
    System.out.println(m.group(1));
else
    System.out.println("your string doesn't start with \"<digits>\"");

1 Comment

You're assuming that it's only digits he wants, but this is a safer regex than the other one which might be over-greedy so this one gets my vote.
0

In fact for this you can also try using replaceAll with a regex.

System.out.println("<172>Lorem Ipsum".replaceAll(".*<|>.*", ""));

Comments

0

Try something like this:

public Test() {
    String test = "<172>Lorem Ipsum";
    String number = "";
    if (test.startsWith("<")) {
        for (int index = 1 ; index < test.length() ; index++) {
            if (!test.substring(index, index+1).equals(">")) {
                number += test.substring(index, index+1);
            } else {
                break;
            }
        }
    }
    System.out.println(number);
}

2 Comments

You can work with characters directly rather than using a single character substring. Also I'm not sold on string appending for this: yes, this means you only need a single pass through the source string but a single substring to get the answer's likely going to be more efficient in Java string objects.
Why would you use substring in place of charAt ?
0
     int leftBound = data.indexOf("<");
     int rightBound = data.indexOf(">");
     data.substring(leftBound+1, rightBound));

Figured it out. It was one of those "Ask" then instantly figure it out things.

1 Comment

To cope with error-type cases you might want to take the first close after the first end, e.g. see Ashot's answer, rather than just the first close. And there's probably more edge case detection and handling to be done anyway, e.g. result is empty.

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.