I get a response back from the server like this : "Username|Name|AccountType|Organization". Is there a way to use the "|'s" as separators and get each variable separately. I'm guessing I would have to use a for loop.
you can use String.split
with |
. It will return a String[]
array. For instance
String test = "Username|Name|AccountType|Organization";
for (String token : test.split("\\|")) {
Log.i("TEST", token);
}
If you use Guava's Splitter class:
List<String> tokens = Splitter.on("|").split("Username|Name|AccountType|Organization");
With Apache Commons' StringUtils class:
String[] tokens = StringUtils.split("Username|Name|AccountType|Organization", '|');
And plain Java Strings:
String[] test = "Username|Name|AccountType|Organization".split("\\|");
PS: no you don't need Guava or Apache Commons just to split a string. But they bring in a lot of really useful stuff that will make your code more robust. Guava is one of the libraries I include in any project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With