Spilt sentence to array of words keep fomat - java

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");

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);
}

After I split a string, how would I modify each word independently? - Java

For example, if I have a string called 'S', split it using .split and store all the words in String[] arr = s.split(" "), how would I modify each word? Let's say I want to remove the first letter and add into the end, then insert some more characters. I know I'll be using StringBuilder class and deleteCharAt, append(), etc..
Ex. Hello World ----> elloHto orldWto
String input = "Hello World";
String[] words = input.split(" ");
for(String s: words){
System.out.println(s); //do whatever you want
}

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

Traversing through a sentence word by word

How is it possible to traverse through any given sentence word by word? Is there any in-built functions in java? I have no idea how to begin.
Something like this:
String sentence = "Your sentence here.";
String[] words = sentence.split("\\s+"); // splits by whitespace
for (String word : words) {
System.out.println(word);
}
A lot of people are suggesting to split on spaces, but even this very sentence contains commas, etc. You should split on more than just spaces; split on punctuation characters too:
String words = sentence.split("([\\s.,;:\"?!,.…(){}[\\]%#/]|(- )|( -))+");
This regex splits on all reasonably expected punctuation characters. Note that the in-word hyphen and the apostrophe are not "punctuation"; they are part of the word.
This approach, or something similar, will also handle non-English character sentences.
String[] array = input.split(" ");
That way the string is converted into an array separated by spaces (you can change the separator in the split()'s argumen) and then you can loop through the array as you want.
Start with StringTokenizer for example or use String.split(" ")
Try splitting the sentence by whitespace character.
String sentence = "This is a sentence.";
for(String word: sentence.split("\\s+"){
System.out.println(word);
}
String s="sfgasdfg jhsadfkjashfd sajdfhjkasdfh hjskafhasj";
String wordArray[] =s.split("\\s+");
for(String sT :wordArray)
{
System.out.println(st);
}
Take a look at the String Split function here http://www.tek-tips.com/viewthread.cfm?qid=1167964
Assuming you already have the sentence stored as a string, you could use the String.replaceAll("[./,]"," ") method to remove the stop words and then use the String.split("\\s+") to obtain the individual words making up the phrase.
you can use StringTokenizer class which will divide the string into words.
public static void main(String ae[]){
String st = "This is Java";
StringTokenizer str= new StringTokenizer(st);
while(str.hasMoreTokens()){
System.out.println(str.nextToken());
}
}
I would Say StringTokenizer might help You.
String str = "This is String , split by StringTokenizer, created by mkyong";
StringTokenizer st = new StringTokenizer(str);
System.out.println("---- Split by space ------");
while (st.hasMoreElements()) {
System.out.println(st.nextElement());
}
System.out.println("---- Split by comma ',' ------");
StringTokenizer st2 = new StringTokenizer(str, ",");
while (st2.hasMoreElements()) {
System.out.println(st2.nextElement());
}
Also String.split() may help You:
String[] result = "this is a test".split("\\s");
for (int x=0; x<result.length; x++)
System.out.println(result[x]);
OUTPUT:
this
is
a
test
System.out.println(Arrays.toString(
"Many words//separated.by-different\tcharacters"
.split("\\W+")));
//[Many, words, separated, by, different, characters]

Categories

Resources