I have a scenario like i need to check a string if it has only one word. I split on white space and its fine, now I need to check if a single word has a space attached to it and I'm unable to handle it.
I tried the below and Its not working
String temp1[] = temp.split("\\s+");
System.out.println("Size "+temp1.length);
if(temp1.length==1 && !temp1[0].contains(" "));
{
System.out.println("Single keyword");
}
I need to avoid the single word along with a space.
first remove starting & ending white spaces using trim()
method & then split it
Ex:~
if your i/p is String s= "Hello " then s.trim() gives "Hello".
read documentation once you will get an idea.
You are checking if a string has one and only word or not.So basically if a string has atleast one space then that string is not considered as single word(simple grammar).So you need to check if that particular string has any spaces or not.
String t1="hhh fff";
if(t1.contains(" "))
{
System.out.println("yes");
}
above will also check if a string is prefixed or post fixed with space
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.