1

I'm trying to split a String.

I have been searching but I only found cases where split is used with an array, so for example if you have the string "i love stackoverflow" and use split , the output will be [i,love,stackoverflow]

Instead of that I have a string with this format "URL_cars_es" and I just want the es part of it, because I have multiple files into my database with different languages lets say I have "URL_cars_it" "URL_cars_nb" and so on. I just need to split and get only the nb,es,it so i can pass it to my StorageReference and get the different files

I am getting URL_cars_es by using below code

selecteditem =  adapterView.getItemAtPosition(i).toString();

and I also need to trim/split it to only es.

2
  • Will you always only want the last two characters of the string? You could use a substring function in this case. Commented Jan 31, 2018 at 14:14
  • You may also look for regular expressions and java.util.Pattern Commented Jan 31, 2018 at 14:17

3 Answers 3

9

If you don't wanted to use array/split. Then you can use lastIndexOf (index of the last occurrence of given character) and then substring from that index position.

String s1   = "URL_cars_it";
int lastPos = s1.lastIndexOf('_'); // return the index of the last occurrence

s1 = s1.substring(lastPos + 1); // return `it`
Sign up to request clarification or add additional context in comments.

6 Comments

thanks, will check this as correct as you were the first one answering
@IDroid let me know, if you have any specific requirement, where it doesn't fulfill as this is specific to your mentioned scenario.
its ok, i was just needing this because i was making a spinner with different elements from firebase database, but i just realised that i cant get from the storage the Locale language or the language that people are using because im doing another app to manage how users are interacting with the app. So i was looking for a workaround to get the different user languages , thanks for the help
@IDroid So, was it helpful ? or you have any other scenario ?
it was really helpfull and solved my problem, now with this one line i can get different information from different languages of how users are interacting with the app, thanks
|
3

What about this:

String countryCode = es.split("_")[2] //only take the 3rd entry of the array

I do not have the split() implementation in my mind now, just guessing it should look like the above.

2 Comments

This is assuming the string always splits into 3 parts. If a different prefix is used this could produce an IndexOutOfBoundsException
@MichaelDodd this is absolutely correct. But he stated that his String does always look like that.
2

Another way is to remove URL_cars_ using the String::replace method:

String str = "URL_cars_es";

System.out.println(str.replace("URL_cars_", ""));

Output:

es

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.