Splitting a string with multiple spaces - java

I want to split a string like
"first middle last"
with String.split(). But when i try to split it I get
String[] array = {"first","","","","middle","","last"}
I tried using String.isEmpty() to check for empty strings after I split them but I it doesn't work in android. Here is my code:
String s = "First Middle Last";
String[] array = s.split(" ");
for(int i=0; i<array.length; i++) {
//displays segmented strings here
}
I think there is a way to split it like this: {"first","middle","last"} but can't figure out how.
Thanks for the help!

Since the argument to split() is a regular expression, you can look for one or more spaces (" +") instead of just one space (" ").
String[] array = s.split(" +");

try using this s.split("\\s+");

if you have a string like
String s = "This is a test string This is the next part This is the third part";
and want to get an array like
String[] sArray = { "This is a test string", "This is the next part", "This is the third part" }
you should try
String[] sArray = s.split("\\s{2,}");
The {2,} part defines that at least 2 and up to almost infinity whitespace characters are needed for the split to occur.

This worked for me.
s.split(/\s+/)
var foo = "first middle last";
console.log(foo.split(/\s+/));

Since split() uses regular expressions, you can do something like s.split("\\s+") to set the split delimiter to be any number of whitespace characters.

How about using something that is provided out of the box by Android SDK.
TextUtils.split(stringToSplit, " +");

If someone is looking for koltin code
val str = " fly me to the moon "
println(str.trim().split(" +".toRegex()))
// output - [fly, me, to, the, moon]

Related

How to return only first n number of words in a sentence Java

Say i have a simple sentence as below.
For example, this is what have:
A simple sentence consists of only one clause. A compound sentence
consists of two or more independent clauses. A complex sentence has at
least one independent clause plus at least one dependent clause. A set
of words with no independent clause may be an incomplete sentence,
also called a sentence fragment.
I want only first 10 words in the sentence above.
I'm trying to produce the following string:
A simple sentence consists of only one clause. A compound
I tried this:
bigString.split(" " ,10).toString()
But it returns the same bigString wrapped with [] array.
Thanks in advance.
Assume bigString : String equals your text. First thing you want to do is split the string in single words.
String[] words = bigString.split(" ");
How many words do you like to extract?
int n = 10;
Put words together
String newString = "";
for (int i = 0; i < n; i++) { newString = newString + " " + words[i];}
System.out.println(newString);
Hope this is what you needed.
If you want to know more about regular expressions (i.e. to tell java where to split), see here: How to split a string in Java
If you use the split-Method with a limiter (yours is 10) it won't just give you the first 10 parts and stop but give you the first 9 parts and the 10th place of the array contains the rest of the input String. ToString concatenates all Strings from the array resulting in the whole input String. What you can do to achieve what you initially wanted is:
String[] myArray = bigString.split(" " ,11);
myArray[10] = ""; //setting the rest to an empty String
myArray.toString(); //This should give you now what you wanted but surrouned with array so just cut that off iterating the array instead of toString or something.
This will help you
String[] strings = Arrays.stream(bigstring.split(" "))
.limit(10)
.toArray(String[]::new);
Here is exactly what you want:
String[] result = new String[10];
// regex \s matches a whitespace character: [ \t\n\x0B\f\r]
String[] raw = bigString.split("\\s", 11);
// the last entry of raw array is the whole sentence, need to be trimmed.
System.arraycopy(raw, 0, result , 0, 10);
System.out.println(Arrays.toString(result));

Java Splitting A Sentence

I am writing a program for Twitter. It will read a tweet and get the hashtags in it.
The problem is, I couldn't split it. For example, "I love #computers so much." in this one, I need to obtain only the "computers" part.
I thought about using split function by using # but it will split the sentence in a half so still, it won't be a solution. Any ideas?
You want to split on the # indeed. After that you want to have the word. So split on the " " space :).
string="I love #computers so much.";
String[] parts = string.split("#");
String part1 = parts[0]; // I love
String part2 = parts[1]; // computers so much.
String[] parts2 = part2.split(" ");
String output = parts2[0];
The above should work, haven't tested it though.
If there are multiple hashtages the above won't work, try the below one:
String string="I love #computers so #much omg #lol .";
String[] stringParts = string.split("#");
//'delete' first element.
String[] parts = Arrays.copyOfRange(stringParts, 1, stringParts.length);
int i = 0;
String[] output = new String[10];
for(String part : parts)
{
if(part.contains(" "))
{
String[] parts2 = part.split(" ");
output[i] = parts2[0];
i++;
}
}
The only problem is with this code, that you need a space otherwise you will have different characters in your word.
You would do well to take a look at solving the problem using regular expressions.... try something like (?<=#)\w+ -- it will return all alpha numerics after the #, while not capturing the #. You may want to change the \w to include additional characters as required. Hope this helps.
You can use regular expressions to obtain the hash tags from the tweet. Something like:
String sentence = "I love #computers and #something_Else so much";
Pattern p = Pattern.compile("#\\S+");
List<String> hashTags = new ArrayList<>();
Matcher matcher = p.matcher(sentence);
while (matcher.find()) {
hashTags.add(matcher.group(0));
}
System.out.println(hashTags);

Split each string from a paragraph

I am trying to split each string from a paragraph, which has proper grammar based punctuation delimiters like ,.!? or more if any.
I am trying to achieve this using Java. Here is my code.
private void printWords(String inputString) {
String[] x = inputString.split("[.!,\\s]");
for(String temp: x){
System.out.println(temp);
}
}
Sample input String:
He is srk. Oh! I am a very good friend of srk.
My output:
He
is
srk
Oh
I
am
a
very
good
friend
of
srk
There is a problem here, It is having spaces as shown in the output. What should be my regular expression to split strings in any given paragraph, without spaces in the output.
You need to add a + to make your expression match one or more characters:
String[] x = inputString.split("[.!,\\s]+");
What about:
String[] x = inputString.split("\\W+");

How do I fill a new array with split pieces from an existing one? (Java)

I'm trying to split paragraphs of information from an array into a new one which is broken into individual words. I know that I need to use the String[] split(String regex), but I can't get this to output right.
What am I doing wrong?
(assume that sentences[i] is the existing array)
String phrase = sentences[i];
String[] sentencesArray = phrase.split("");
System.out.println(sentencesArray[i]);
Thanks!
It might be just the console output going wrong. Try replacing the last line by
System.out.println(java.util.Arrays.toString(sentencesArray));
The empty-string argument to phrase.split("") is suspect too. Try passing a word boundary:
phrase.split("\\b");
You are using an empty expression for splitting, try phrase.split(" ") and work from there.
This does nothing useful:
String[] sentencesArray = phrase.split("");
you're splitting on empty string and it will return an array of the individual characters in the string, starting with an empty string.
It's hard to tell from your question/code what you're trying to do but if you want to split on words you need something like:
private static final Pattern SPC = Pattern.compile("\\s+");
.
.
String[] words = SPC.split(phrase);
The regex will split on one or more spaces which is probably what you want.
String[] sentencesArray = phrase.split("");
The regex based on which the phrase needs to be split up is nothing here. If you wish to split it based on a space character, use:
String[] sentencesArray = phrase.split(" ");
// ^ Give this space

What is the best way to extract the first word from a string in Java?

Trying to write a short method so that I can parse a string and extract the first word. I have been looking for the best way to do this.
I assume I would use str.split(","), however I would like to grab just the first first word from a string, and save that in one variable, and and put the rest of the tokens in another variable.
Is there a concise way of doing this?
The second parameter of the split method is optional, and if specified will split the target string only N times.
For example:
String mystring = "the quick brown fox";
String arr[] = mystring.split(" ", 2);
String firstWord = arr[0]; //the
String theRest = arr[1]; //quick brown fox
Alternatively you could use the substring method of String.
You should be doing this
String input = "hello world, this is a line of text";
int i = input.indexOf(' ');
String word = input.substring(0, i);
String rest = input.substring(i);
The above is the fastest way of doing this task.
To simplify the above:
text.substring(0, text.indexOf(' '));
Here is a ready function:
private String getFirstWord(String text) {
int index = text.indexOf(' ');
if (index > -1) { // Check if there is more than one word.
return text.substring(0, index).trim(); // Extract first word.
} else {
return text; // Text is the first word itself.
}
}
The simple one I used to do is
str.contains(" ") ? str.split(" ")[0] : str
Where str is your string or text bla bla :). So, if
str is having empty value it returns as it is.
str is having one word, it returns as it is.
str is multiple words, it extract the first word and return.
Hope this is helpful.
import org.apache.commons.lang3.StringUtils;
...
StringUtils.substringBefore("Grigory Kislin", " ")
You can use String.split with a limit of 2.
String s = "Hello World, I'm the rest.";
String[] result = s.split(" ", 2);
String first = result[0];
String rest = result[1];
System.out.println("First: " + first);
System.out.println("Rest: " + rest);
// prints =>
// First: Hello
// Rest: World, I'm the rest.
API docs for: split
for those who are searching for kotlin
var delimiter = " "
var mFullname = "Mahendra Rajdhami"
var greetingName = mFullname.substringBefore(delimiter)
like this:
final String str = "This is a long sentence";
final String[] arr = str.split(" ", 2);
System.out.println(Arrays.toString(arr));
arr[0] is the first word, arr[1] is the rest
You could use a Scanner
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html
The scanner can also use delimiters
other than whitespace. This example
reads several items in from a string:
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close();
prints the following output:
1
2
red
blue
None of these answers appears to define what the OP might mean by a "word". As others have already said, a "word boundary" may be a comma, and certainly can't be counted on to be a space, or even "white space" (i.e. also tabs, newlines, etc.)
At the simplest, I'd say the word has to consist of any Unicode letters, and any digits. Even this may not be right: a String may not qualify as a word if it contains numbers, or starts with a number. Furthermore, what about hyphens, or apostrophes, of which there are presumably several variants in the whole of Unicode? All sorts of discussions of this kind and many others will apply not just to English but to all other languages, including non-human language, scientific notation, etc. It's a big topic.
But a start might be this (NB written in Groovy):
String givenString = "one two9 thr0ee four"
// String givenString = "oňňÜÐæne;:tŵo9===tĥr0eè? four!"
// String givenString = "mouse"
// String givenString = "&&^^^%"
String[] substrings = givenString.split( '[^\\p{L}^\\d]+' )
println "substrings |$substrings|"
println "first word |${substrings[0]}|"
This works OK for the first, second and third givenStrings. For "&&^^^%" it says that the first "word" is a zero-length string, and the second is "^^^". Actually a leading zero-length token is String.split's way of saying "your given String starts not with a token but a delimiter".
NB in regex \p{L} means "any Unicode letter". The parameter of String.split is of course what defines the "delimiter pattern"... i.e. a clump of characters which separates tokens.
NB2 Performance issues are irrelevant for a discussion like this, and almost certainly for all contexts.
NB3 My first port of call was Apache Commons' StringUtils package. They are likely to have the most effective and best engineered solutions for this sort of thing. But nothing jumped out... https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html ... although something of use may be lurking there.
You could also use http://download.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html
I know this question has been answered already, but I have another solution (For those still searching for answers) which can fit on one line:
It uses the split functionality but only gives you the 1st entity.
String test = "123_456";
String value = test.split("_")[0];
System.out.println(value);
The output will show:
123
The easiest way I found is this:
void main()
String input = "hello world, this is a line of text";
print(input.split(" ").first);
}
Output: hello
Assuming Delimiter is a blank space here:
Before Java 8:
private String getFirstWord(String sentence){
String delimiter = " "; //Blank space is delimiter here
String[] words = sentence.split(delimiter);
return words[0];
}
After Java 8:
private String getFirstWord(String sentence){
String delimiter = " "; //Blank space is delimiter here
String firstWord = Arrays.stream(sentence.split(delimiter))
.findFirst()
.orElse("No word found");
}
String anotherPalindrome = "Niagara. O roar again!";
String roar = anotherPalindrome.substring(11, 15);
You can also do like these

Categories