Separate particular word from string - java

I have One String
which has value
String someString = "/category/subcategory/Fruit/apple/";
I want to separate "Fruit" from string.

Make an Array of Strings by splitting with / character like:
String[] split = someString.split("/");
This Array split[] has all the elements separated. You can use whichever you want.

String[] values = someString.split("/");
Log.i(TAG, values[3]);

Related

Splitting string based on delimiter

A string is taken as input which is in the form of 23,4,555,67 via deadline nd another input is key yo search the element linearly ?my question is how can we recognize the elements from string separated by comma
You can split the String using split :
String[] tokens = "23,4,555,67".split(",");
String s = "23,4,555,67"
String[] tokens = s.split(",");
This will give you a string array with the numbers.
Alternatively, you can use a StringTokenizer. (java.util)
This can be used if your string is delimited by more than one characters (can be be used as well in case of single character). Your example using StringTokenizer
SrringTokenizer st = new StringTokenizer("23,4,555,67", ",");
while(st.hasMoreElements())
System.out.println(st.nextToken());

convery string into array with brackets

I have the following String
[http://images.com/1.jpg, http://images.com/2.jpg, http://images.com/3.jpg]
I want to store the contents of this array inside a string array or array list of type string.
I tried using .split method, but it fails mainly because the string also contains the brackets at the beginning.
String[] splittedString = theString.substring(1, theString.length()-1).split(", ")
Notice space after comma in the split method.
Use substring to exclude the string from the brackets:
mystring = mystring.substring(1,mystring.length()-1);
And then the split:
String[] myarray = mystring.split(", ");
String arry[] = yourstr.replace("[", "").replace("]", "").split(",");
Escape the first and the last characters of your String containing the brackets before using the split() method, like this :
yourString= yourString.substring(1, yourString.length()-1));
// do your split() method
Optionally you can use StringTokenizer.

how to remove certain character or select certain index character in string?

I have this string,
anyType{image0=images/articles/4_APRIL_BLACK_copy.jpg; image1=images/articles/4_APRIL_COLOR_copy.jpg; }
What i want is only
"images/articles/4_APRIL_BLACK_copy.jpg"
How do i get this?
This is how I perform a split in my app.
String link = "image0=images/articles/4_APRIL_BLACK_copy.jpg";
String[] parts = link.split("=");
String first = parts[0];
Log.v("FIRST", first);
String second = parts[1];
Log.v("SECOND", second);
This method will split your string into 2 at the "=" and give you 2 split strings. In your case, the String second is the result you want.
This should work:
s.split("=")[1]
You are splitting the string on = which would return substrings in an array. The second element is what you need.

Java: String splitting into multiple elements

I am having a difficult time figuring out how to split a string like the one following:
String str = "hi=bye,hello,goodbye,pickle,noodle
This string was read from a text file and I need to split the string into each element between the commas. So I would need to split each element into their own string no matter what the text file reads. Keep in mind, each element could be any length and there could be any amount of elements which 'hi' is equal to. Any ideas? Thanks!
use split!
String[] set=str.split(",");
then access each string as you need from set[...] (so lets say you want the 3rd string, you would say: set[2]).
As a test, you can print them all out:
for(int i=0; i<set.length;i++){
System.out.println(set[i]);
}
If you need a bit more advanced approach, I suggest guava's Splitter class:
Iterable<String> split = Splitter.on(',')
.omitEmptyStrings()
.trimResults()
.split(" bye,hello,goodbye,, , pickle, noodle ");
This will get rid of leading or trailing whitespaces and omit blank matches. The class has some more cool stuff in it like splitting your String into key/value pairs.
str = str.subString(indexOf('=')+1); // remove "hi=" part
String[] set=str.split(",");
I'm wondering: Do you mean to split it as such:
"hi=bye"
"hi=hello"
"hi=goodbye"
"hi=pickle"
"hi=noodle"
Because a simple split(",") will not do this. What's the purpose of having "hi=" in your given string?
Probably, if you mean to chop hi= from the front of the string, do this instead:
String input = "hi=bye,hello,goodbye,pickle,noodle";
String hi[] = input.split(",");
hi[0] = (hi[0].split("="))[1];
for (String item : hi) {
System.out.println(item);
}

Splitting and assigning a string with whitespace as the delimeter

I need help splitting this string, but i can't seem to come with the right way of doing it.
Suppose I have two numbers on a line
12 101
I would like to take the first and assign it to variable, and then take the second and assign it to a variable, this may sounds easy, but for me i can't come up with the right way to do it?
Split the string on space which will give you an array of strings that can be stored in two variables. If necessary, you can convert them to ints as shown below:
String text = "12 101";
String[] split= text.split("\\s+");
String first = split[0];
String second = split[1];
//if you want them as ints
int firstNum = Integer.parseInt(first);
int secondNum = Integer.parseInt(second);
String[] result = myString.split(" ");
should work. Then you can assign the values to a variable from the array if you want. Though you should note that if there are two spaces, it will create an array with length of three, the middle element being an empty string.
String s = "12 101";
String[] splitted = s.split("\s"); // \s = any whitespace character except newline
String[] myStringArray = myString.split(" ");
will split the string into an array myStringArray
String text = "12 101";
String[] splitted = text.split("\\s+");
System.out.println(splitted[0]);
System.out.println(splitted[1]);
Will print:
12
101
Using \s+ splits the string at every whitespace in your text. Multiple whitespaces are ignores.
Only doing split(" "); will result in empty fields (e.g. "102 12 4" => [102, , 12, 4]).

Categories