There is a string,string a=" *|** || |**|** ";
The space separated this string into three groups. How can I converse these three groups into an array with three elements?
I tried to use split,
String a=" *|** || |**|** ";
String names[]=a.trim().split(" ");
System.out.println(names.length);
The expected output should be 3, however, it shows 8. Anyone who can tell me how to do it? thanks
String a=" *|** || |**|** ";
String names[]=a.trim().split("\\s+");
System.out.println(names.length);
This splits by any amount of white space characters (regular spaces, tabs, etc)
The regex "\\s+" searches for any white space.
Try splitting on multiple spaces, your code splits on a single space character. \s+ is regex for 1 or more spaces.
String a = " *|** || |**|** ";
String names[] = a.trim().split("\\s+");
System.out.println(names.length);
System.out.println(Arrays.toString(names));
Output
3
[*|**, ||, |**|**]
Related
I have one String that has multiple values in single string. I just want to remove the space after decimal point only without removing other spaces from string.
String testString = "EB:3668. 19KWh DB:22. 29KWh";
testString = testString.trim();
String beforeDecimal = testString.substring(0, testString.indexOf("."));
String afterDecimal = testString.substring(testString.indexOf("."));
afterDecimal = afterDecimal.replaceAll("\\s+", "");
testString = beforeDecimal + afterDecimal;
textView.setText(testString);
Here as in my string there is two values in single string
EB:3668. 19KWh and DB:22. 29KWh.
I just want to remove space after decimal point and make String like this:
EB:3668.19KWh DB:22.29KWh
You can use 2 capture groups and match the space in between. In the replacement use the 2 groups without the space.
(\d+\.)\h+(\d+)
Regex demo
String testString="EB:3668. 19KWh DB:22. 29KWh";
String afterDecimal = testString.replaceAll("(\\d+\\.)\\h+(\\d+)","$1$2");
System.out.println(afterDecimal);
Output
EB:3668.19KWh DB:22.29KWh
Or a bit more specific pattern could be including the KWh:
\b(\d+\.)\h+(\d+KWh)
Regex demo
Just use string.replaceAll("\\. ", ".");
Thanks to Henry for pointing out I had to escape the .
I'm not in front of an editor right now, but wouldn't you be able to do this with the replaceAll method in a single line, without breaking it up?
var text = testString.replaceAll(". ", ".");
You can remove unnecessary spaces between the decimal point and the fractional part as follows. This code also removes other extra spaces:
String testString = " EB:3668. 19KWh DB:22. 29KWh ";
String test2 = testString
// remove leading and trailing spaces
.trim()
// replace non-empty sequences of space
// characters with a single space
.replaceAll("\\s+", " ")
// remove spaces between the decimal
// point and the fractional part
// regex groups:
// (\\d\\.) - $1 - digit and point
// ( ) - $2 - space
// (\\d) - $3 - digit
.replaceAll("(\\d\\.)( )(\\d)", "$1$3");
System.out.println(test2); //EB:3668.19KWh DB:22.29KWh
See also: How do I remove all whitespaces from a string?
I am working with Strings where I need to separate two chars/elements if there is a whitespace between them. I have seen a former post on SO about the same however it still has not worked for me as intended yet. As you would assume, I could just check if the String contains(" ") and then substring around the space. However my strings could possibly contains countless whitespaces at the end despite not having whitespace in between characters. Hence my question is "How do I detect a whitespace between two chars (numbers too) " ?
//Example with numbers in a String
String test = "2 2";
final Pattern P = Pattern.compile("^(\\d [\\d\\d] )*\\d$");
final Matcher m = P.matcher(test);
if (m.matches()) {
System.out.println("There is between space!");
}
You would use String.strip() to remove any leading or trailing whitespace, followed by String.split(). If there is a whitespace, the array will be of length 2 or greater. If there is not, it will be of length 1.
Example:
String test = " 2 2 ";
test = test.strip(); // Removes whitespace, test is now "2 2"
String[] testSplit = test.split(" "); // Splits the string, testSplit is ["2", "2"]
if (testSplit.length >= 2) {
System.out.println("There is whitespace!");
} else {
System.out.println("There is no whitespace");
}
If you need an array of a specified length, you can also specify a limit to split. For example:
"a b c".split(" ", 2); // Returns ["a", "b c"]
If you want a solution that only uses regex, the following regex matches any two groups of characters separated by a single space, with any amount of leading or trailing whitespace:
\s*(\S+\s\S+)\s*
Positive lookahead and lookbehind may also work if you use the regex (?<=\\w)\\s(?=\\w)
\w : a word character [a-zA-Z_0-9]
\\s : whitespace
(?<=\\w)\\s : positive lookbehind, matches if a whitespace preceeded by a \w
\\s(?=\\w) : positive lookahead, matches if a whitespace followed by a \w
List<String> testList = Arrays.asList("2 2", " 245 ");
Pattern p = Pattern.compile("(?<=\\w)\\s(?=\\w)");
for (String str : testList) {
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(str + "\t: There is a space!");
} else {
System.out.println(str + "\t: There is not a space!");
}
}
Output:
2 2 : There is a space!
245 : There is not a space!
The reason you pattern does not work as expected is because ^(\\d [\\d\\d] )*\\d$ which can be simplified to (\\d \\d )*\\d$ starts by repeating 0 or more times what is between the parenthesis.
Then it matches a digit at the end of the string. As the repetition is 0 or more times, it is optional and it would also match just a single digit.
If you want to check if there is a single space between 2 non whitespace chars:
\\S \\S
Regex demo | Java demo
final Pattern P = Pattern.compile("\\S \\S");
final Matcher m = P.matcher(test);
if (m.find()) {
System.out.println("There is between space!");
}
Here is the simplest way you can do it:
String testString = " Find if there is a space. ";
testString.trim(); //This removes all the leading and trailing spaces
testString.contains(" "); //Checks if the string contains a whitespace still
You can also use a shorthand method in one line by chaining the two methods:
String testString = " Find if there is a space. ";
testString.trim().contains(" ");
Use
String text = "2 2";
Matcher m = Pattern.compile("\\S\\s+\\S").matcher(text.trim());
if (m.find()) {
System.out.println("Space detected.");
}
Java code demo.
text.trim() will remove leading and trailing whitespaces, \S\s+\S pattern matches a non-whitespace, then one or more whitespace characters, and then a non-whitespace character again.
I have made a program that counts the frequency of a word in a very long string. My problem is that the program is counting for example "*it" (consider * a quotation mark) and "it" as different words and therefore putting them in different categories.
I tried to replace all the punctuation marks I know of with the following code:
text = text.replace("\n", " ");
text = text.replaceAll("\\p{Punct}", " ");
text = text.replace("\"", "");
text = text.replace("–", "");
text = text.replace("\t", "");
Unfortunately, the code didn't work and I think it is because there is a lot of different quotation marks in Unicode that I can't see a difference between, so is there a way to remove all Unicode characters except letters and whitespaces with the String.replaceAll method or do I have to make a CharArray and continue from there?
Thanks a lot, any help would be appreciated.
I think this might do it
text = text.replaceAll("[^a-zA-Z0-9 ]", "");
which will remove all the characters which are not either alphanumeric or special characters.
EDIT :-
As suggesed by #npinti
text = text.replaceAll("[^\\p{L}0-9 ]", "");
This will remove all non-letter/digit characters and squish the spaces so you don't get multiple consecutive spaces:
text = text.replaceAll("[^\\p{L}\\d]+", " ");
This will remove all not letters and whitespaces.
text.replaceAll("[^\\sa-zA-Z]", "");
Legend:
^ - exclude given characters from being replaced
\\s - all whitespaces (\n , \t , ' ')
a-zA-Z - all letters
Example:
String in="12ASxA sdr5%";
System.out.println(in.replaceAll("[^\\sa-zA-Z]", "")); // ASxA sdr
If I have a file containing random characters e.g:
sdo8kd oko ala la654
"sdo8kd", "oko", "ala" and "la654" would be considered words.
How can I represent a word not containing white space characters specifically using the method Character.isWhitespace(c) where c is the character being checked to see if it is white space.
You can use split(regex) from String and put into an array, after that do what you want with it.
String sentence = "sdo8kd oko ala la654";
String[] words = sentence.split("\\s+");
for(String word : words){
System.out.println("'" + word + "'"); //'sdo8kd', 'oko', 'ala', 'la654'
}
System.out.println(words.length); //4
try this regex "[a-zA-Z0-9]+"
boolean isAlphaNumeric = s.matches("[a-zA-Z0-9]+");
a-zA-Z all latin letters (lower and upper case)
0-9 all digits
[a-zA-Z0-9]+ at least 1 or more characters inside brackets.
If you are looking to split the words even any number of space is in between.
You can use
"sdo8kd oko ala la654".split(" +");
This will return String[] with values "sdo8kd", "oko", "ala" and "la654"
I have strings in the following form:
let the character - denote empty space
----100----100----1000---
that is, more empty spaces followed by a number, followed by more empty spaces followed by number, etc.
I need to extract the three numbers only. How do I do that in java?
Thanks
I need to extract the three numbers only. How do I do that in java?
So I understand your string is like below, in which case you can split it on white spaces:
String str = " 100 100 1000";
String[] numbers = str.trim().split("\\s+");
To collapse the spaces (the question asked):
String collapsed = str.replaceAll(" +", " ");
To extract the 3 numbers (the question alluded to):
String[] numbers = str.trim().split(" +");
simply try using,
String newstring = oldstring.replaceAll(" +", " ");
or
String[] selected = oldstring.trim().split(" +");