This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I split a string with any whitespace chars as delimiters?
Yes, I tried to search it on google and stackoverflow, but no good results. I have a string, lets say: "Lets do some coding in Java" and I would like to got strings (split it on whitespaces):
Lets, do, some, coding, in, Java
I used string.split("\\s") for this, but know I need to use regex instead. Any ideas?
String str = "Hello How are you";
String arrayString[] = str.split("\\s+")
Please use this
to specify space as splitting char, you can pass " " as parameter to String#split.
Example:
String test="Lets do some coding in Java";
for(String token : test.split(" "))
System.out.println(token);
Related
This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 6 years ago.
I have a String called filename:
filename = "z_cams_c_ecmf_20170217000000_prod_fc_pl_015_aermr04.nc";
When I try to split the filename to get the variable name aermr04.nc, I tried the following:
String varibleName = filename.split("_")[9].split(".")[0];
The above line of code throws an IndexOutOfBoundsException.
Why?
I can get it tow work by using:
String varibleName = filename.split("_")[9].split("\\.")[0];
However, it seems rather silly that I have to fiddle around with such trivial tasks...
Any idea why the 2nd example works? What is the reasoning behind such syntax?
The argument to .split() is treated as a regular expression. "." as a regex matches everything.
To match a period, you need to escape the "." regex as "\\."
This question already has an answer here:
scala exactly matching a word in a given string
(1 answer)
Closed 6 years ago.
I am very new to regular expression. I want to replace string from sentence using regular expression in scala or java.
Ex.
"I am new to scala and scalapark is differnt"
I want to remove "scala" string from this statement not "scalapark".
"I am new to and scalapark is differnt"
How can I perform this using regex.
Thanks in advance
You could try this
String s = "I am new to scala and scalapark is differnt";
s = s.replaceAll("\\bscala\\b", "");
Explanation
\\b means word boundary
scala just matches scala
This question already has answers here:
How to remove duplicate white spaces in string using Java?
(9 answers)
Closed 6 years ago.
A String can contain multiple spaces in a row - I need to replace multiple subsequent spaces by one single space char. The "problem" is that i cant know how many spaces there may encounter. The function I look for shall not only replace the first occurance of a found match, but all multiple equal characters in a String.
I searched a lot on the internet and tried the regex "X*? (X, zero or more times)" which I found unter "Reluctant quantifiers" on https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#sum
That didnt work: s1 = s1.replaceAll(" *?", " ");
Where s1 = "Hello World"; should be converted to s1 = "Hello World";
I'd be thankful for any help.
You can use replaceAll() that replaces whitespaces with just a single space.
String st = "helllo world"
System.out.println(st.replaceAll("\\s+"," "))
Output : helllo world
This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I want to split an android string to smaller ones with any | char.
Just imagine I have this long string :
This|is|a|long|string|in|java
So, I wanna split it. I need to get a array in output with this values :
[1]=>"This"
[2]=>"is"
[3]=>"a"
[4]=>"long"
[5]=>"string"
[6]=>"in"
[7]=>"java"
I have tried :
separated = oldstring.split("|");
But, i didn't give me the thing i need!
How can i do that? Any code can do that?
Note that String's split() method take regex as a param. Not string.
public String[] split(String regex)
Since | is a meta character, and it's have a special meaning in regex.
It works when you escape that.
String separated[] = oldstring.split("\\|");
This question already has answers here:
Closed 10 years ago.
Please see also:
How to see if a substring exists inside another string in Java 1.4
Searching for one string in another string
How to search a string in another string?
I have two strings, like so:
String str1 = "He is doing very good";
String str2 = "doing";
I want to know how to find the word "doing" in string str1, even if str1 is changed like so:
str1 = "He isdoing very good";
The easiest way would be
str1.contains(str2);
How about the String.contains method?