Split string on whitespace OR dash only. Java Applet - java

I'm trying to split a space AND a dash character between words in a string array. The following code shows what kind of result I am after.
Code:
String[] wordSplit = txtInput.split (" ") && txtInput.split ("-");
input:
hello world hello-world
expected output:
there are: 4 word(s) of length 5.

Use set of characters([..]); it matches one of character listed.
String[] wordSplit = txtInput.split("[-\\s]")
Example:
class T {
public static void main(String[] args) {
String[] words = "hello world hello-world".split("[-\\s]");
for (String word : words) {
System.out.println(word);
}
}
}
output:
hello
world
hello
world

Use a character class:
String[] wordSplit = txtInput.split("[ -]");

Use below code :
String str = "hello world hello-world";
String[] splitArray = str.split("[-\\s]");
System.out.println("Size of array is :: "+splitArray.length);
Output : 4

You have to use more delimiters in one split.
Like this:
String[]wordSplit = txtInput.split(" |\\-");

Related

Splitting string with letters and letter combinations divided by space into array in Java

I am making an app for Android, and it turned out that I do really need to "split" the string (which is actually entered by the user) to the parts and then put them into array. The string itself is supposed to contain letter or letter combination (2 letters) and then space, letter or combination and space... for example "ab c de f g hi j". So for this particular example array would be like array[1]= "ab", array[2]= "c", array[3]= "de" and so on... And each letter or letter combination is supposed to get to array. I've tried to use charAt with IF, but it doesn't seem to work. I'm novice to java so the only possible solution I see is to "cut" the string from the beginning and put it to array, but aren't there any other ways?
Thanks.
Using split method from String
String[] myString = userString.split(" ");
Example:
Input:
String userString = "Hello world";
String[] myString = userString.split(" ");
Output:
myString[0] = "Hello"
myString[1] = "world"
You can use the split method of String class.
String inputString = "ab c d efg h";
String[] arrayOfWords = inputString.split(" ");
for(String word:arrayOfWords) {
System.out.println(word);
}
if your inputString have any other delimeter instead of space between the words, you can use that inside the split method.
Example
String inputString = "ab,c,d,efg,h";
String[] arrayOfWords = inputString.split(",");
for(String word:arrayOfWords) {
System.out.println(word);
}

Why split() and StringTokenizer() behaving differently

public class NewClass
{
public static void main(String args[])
{
System.out.println("Operation 1");
StringTokenizer st1 =
new StringTokenizer("Hello Geeks How are you", "\\s+");
while (st1.hasMoreTokens())
System.out.println(st1.nextToken());
String s = "Hello Geeks How are you";
String s1[]= s.split("\\s+");
System.out.println("Operation 2");
for( String temp : s1) {
System.out.println(temp);
}
}
}
Here after executing the code i am getting the output as :-
Operation 1
Hello Geek
How are you
Operation 2
Hello
Geeks
How
are
you
I am not getting why split() and StringTokenizer() behaving differently with same parameter.
The delim parameter of new StringTokenizer(str, delim) is not a regular expression. You are telling StringTokenizer to split at any of \, s, or +, not at "one or more whitespace characters". And the only character this applies to in your string is the "s" in "Geeks", thus that's where the string is split.
If you want to split at whitespaces (one or more), just use the other constructor taking no delim parameter, using " \t\n\r\f" as default.

Spilt sentence to array of words keep fomat

I wanna spilt a sentence to a array of words
e.g.
Hello this a sentence
this is a new line
I used String[] arr = String.spilt(str)
but when i wanna combine array to sentence the result is:
Hello this a sentence this is a new line
So how i can split string keep new line characters?
Thank you!
Split takes regular expression,
split("(?=\n)")
This will preserve the \n
Provided your string contains \n
public static void main(String[] arg){
String str= "Hello this a sentence \n this is a new line";
System.out.println(str);
String[] xyz = str.split("(?=\n)");
System.out.println(Arrays.toString(xyz));
}
this is a new line
you should use any special character when you make string i.e.
string str = "Hello this a sentence" +"\n"+ "this is a new line"
Then split your string on the base of special character i.e.
string[] arr = str.split("\n");

Split Strings in java by words

How can I split the following word in to an array
That's the code
into
array
0 That
1 s
2 the
3 code
I tried something like this
String str = "That's the code";
String[] strs = str.split("\\'");
for (String sstr : strs) {
System.out.println(sstr);
}
But the output is
That
s the code
To specifically split on white space and the apostrophe:
public class Split {
public static void main(String[] args) {
String [] tokens = "That's the code".split("[\\s']");
for(String s:tokens){
System.out.println(s);
}
}
}
or to split on any non word character:
public class Split {
public static void main(String[] args) {
String [] tokens = "That's the code".split("[\\W]");
for(String s:tokens){
System.out.println(s);
}
}
}
The best solution I've found to split by words if your string contains accentuated letters is :
String[] listeMots = phrase.split("\\P{L}+");
For instance, if your String is
String phrase = "Salut mon homme, comment ça va aujourd'hui? Ce sera Noël puis Pâques bientôt.";
Then you will get the following words (enclosed within quotes and comma separated for clarity) :
"Salut", "mon", "homme", "comment", "ça", "va", "aujourd", "hui", "Ce",
"sera", "Noël", "puis", "Pâques", "bientôt".
Hope this helps!
You can split according to non-characters chars:
String str = "That's the code";
String[] splitted = str.split("[\\W]");
For your input, output will be:
That
s
the
code
You can split by a regex that would be one of the two characters - quote or space:
String[] strs = str.split("['\\s]");
You should first replace the ' with " " (blank space), using str.replaceAll("'", " ") and then you can split the string on the blank space separator, using str.split(" ").You could alternatively use a regular expression to split on ' OR space.
If you want to split on non alphabetic chars
String str = "That's the code";
String[] strs = str.split("\\P{Alpha}+");
for (String sstr : strs) {
System.out.println(sstr);
}
\P{Alpha} matches any non-alphabetic character and this is called POSIX character you can read more about it in this link It is very useful. + indicates that we should split on any continuous string of such characters.
and the output will be
That
s
the
code
You can use OR in regular expression
public static void main(String[] args) {
String str = "That's the code";
String[] strs = str.split("'|\\s");
for (String sstr : strs) {
System.out.println(sstr);
}
}
The string will be split by single quote (') or space. The single quote doesn't need to be escaped. The output would be
run:
That
s
the
code
BUILD SUCCESSFUL (total time: 0 seconds)
split uses regex and in regex ' is not special character so you don't need to escape it with \. To represent whitespaces you can use \s (which in String needs to be written as "\\s"). Also to create set of characters you can use "OR" operator | like a|b|c|d, or just use character class [abcd] which means exactly the same as (a|b|c|d).
To makes things simple you can use
String[] strs = str.split("'| ");
or
String[] strs = str.split("'|\\s");//to include all whitespaces
or
String[] strs = str.split("['\\s]");//equivalent of "'|\\s"

java split () method

I've got a string '123' (yes, it's a string in my program). Could anyone explain, when I use this method:
String[] str1Array = str2.split(" ");
Why I got str1Array[0]='123' rather than str1Array[0]=1?
str2 does not contain any spaces, therefore split copies the entire contents of str2 to the first index of str1Array.
You would have to do:
String str2 = "1 2 3";
String[] str1Array = str2.split(" ");
Alternatively, to find every character in str2 you could do:
for (char ch : str2.toCharArray()){
System.out.println(ch);
}
You could also assign it to the array in the loop.
str2.split("") ;
Try this:to split each character in a string .
Output:
[, 1, 2, 3]
but it will return an empty first value.
str2.split("(?!^)");
Output :
[1, 2, 3]
the regular expression that you pass to the split() should have a match in the string so that it will split the string in places where there is a match found in the string. Here you are passing " " which is not found in '123' hence there is no split happening.
Because there's no space in your String.
If you want single chars, try char[] characters = str2.toCharArray()
Simple...You are trying to split string by space and in your string "123", there is no space
This is because the split() method literally splits the string based on the characters given as a parameter.
We remove the splitting characters and form a new String every time we find the splitting characters.
String[] strs = "123".split(" ");
The String "123" does not have the character " " (space) and therefore cannot be split apart. So returned is just a single item in the array - { "123" }.
To do the "Split" you must use a delimiter, in this case insert a "," between each number
public static void main(String[] args) {
String[] list = "123456".replaceAll("(\\d)", ",$1").substring(1)
.split(",");
for (String string : list) {
System.out.println(string);
}
}
Try this:
String str = "123";
String res = str.split("");
will return the following result:
1,2,3

Categories

Resources