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.

Ravi
  • 30,829
  • 42
  • 119
  • 173
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77

3 Answers3

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`
Ravi
  • 30,829
  • 42
  • 119
  • 173
  • thanks, will check this as correct as you were the first one answering – Gastón Saillén Jan 31 '18 at 14:18
  • @IDroid let me know, if you have any specific requirement, where it doesn't fulfill as this is specific to your mentioned scenario. – Ravi Jan 31 '18 at 14:23
  • 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 – Gastón Saillén Jan 31 '18 at 14:25
  • @IDroid So, was it helpful ? or you have any other scenario ? – Ravi Jan 31 '18 at 14:26
  • 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 – Gastón Saillén Jan 31 '18 at 14:28
  • @IDroid glad to know. :-) – Ravi Jan 31 '18 at 14:28
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.

PKlumpp
  • 4,913
  • 8
  • 36
  • 64
  • 1
    This is assuming the string **always** splits into 3 parts. If a different prefix is used this could produce an `IndexOutOfBoundsException` – Michael Dodd Jan 31 '18 at 14:18
  • @MichaelDodd this is absolutely correct. But he stated that his `String` does always look like that. – PKlumpp Jan 31 '18 at 14:20
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

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50