0

I am getting a list of data in a loop. For example :

List<String> and the values is :

abc{+6636593}
jkihe{qwsd223awq}
......
......etc

I need to have:

abc
jkihe

I write this code :

ArrayList<String> values = new ArrayList<String>();
for(String valueone : valuetwo){
     values.add(valueone);
}

Above code work good but below code get me crash :

for (int i = 0; i < values.size(); i++) {
    Log.i("test" , values.get(i).split("{")[0]); <==== HERE GET ME CRASH
}
3
  • So what exactly is your question? Any error message when your code gets you crash? Commented Nov 19, 2014 at 15:00
  • 1
    How does "code work good" yet also "crash"? Commented Nov 19, 2014 at 15:02
  • Why mixing the old fashion for loop with the enhanced? Commented Nov 19, 2014 at 15:03

2 Answers 2

2

String#split() accepts regex as parameter, you need to escape {.
Try this Log.i("test" , values.get(i).split("\\{")[0]);

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

Comments

0

Here is an answer with enhanced for loop and without regex

for(String test: values) {
   Log.i("test", test.replace(test.substring(test.indexOf("{")), ""));
}

Hope this was helpful!

1 Comment

more efficient: Log.i("test", values.get(i).substring(0, values.get(i).indexOf('{')));

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.