0

I have following text:

    <option value="{0}">hello1</option>
    <option value="{1}">hello2</option>
    <option value="{2}">hello3</option>
    <option value="{3}">hello4</option>
    <option value="{4}">hello5</option>
    <option value="{5}">hello6</option>
    <option value="{6}">hello7</option>
    <option value="{7}">hello8</option>

All above is in one String.

I have 2 Arrays:

valueArray;
nameArray;

So, any tip on how to get the helloX into nameArray and get all value into valueArray?

//Simon

1
  • You need to use an XMLPullParser, and then parse the option start tag for your value. Commented Apr 26, 2012 at 17:45

2 Answers 2

1

You can find the position of > using start =str.indexof(">");

also position of </ using stop=str.indexof("</");

then get string str = str.subString(start,stop);

you will get helloX...

=====================================

https://stackoverflow.com/a/10000095/1289716 see this answer.....

replace your { bracket and } bracket with & #123; ** and **& #125;

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

1 Comment

I find this type of solution to be clunky and error prone. It will quickly fail with <option value="<{0}>">hello0</option>.
0

Use a regex and populate the arrays. Here is a small test program I wrote:

public static void main(String [] args) {
    Pattern p = Pattern.compile("<option value=\"(.*?)\">(.*?)</option>");
    String line = "<option value=\"{1}\">hello0</option>";
    Matcher m = p.matcher(line);
    while (m.find()) {
        String value = m.group(1);
        String name = m.group(2);
        System.out.println(value);
        System.out.println(name);
        // Now add to your arrays
    }
}

I would not use this as a standard method for parsing a complicated xml file. Use an xml parser if this is the case.

5 Comments

Hmm, m.matches is never true...Any idea?
Well, your code works but it won't find if I replace line with my string
Can you provide the actual string that you're trying to parse?
In that case, use m.find() instead of m.matches(). If there may exist multiple matches per line of text, call m.find() multiple times until it returns false.

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.