This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 7 years ago.
I need to be able to split a single word string like "x.y" into an array of size 2 where the "." occurs, so the array would look like ["x", "y"]. What I've tried is:
String word = "hello.world";
String[] split = word.split(".")
return split.length == 2;
but this seems to just return an empty array (false). How would I go about doing this? Thanks.
Repalce this
String[] split = word.split(".")
with
String[] split = word.split("\\.")
. (dot) has a special meaning in regex so you need to escape it if you want to use it as a literal to split.
Related
This question already has answers here:
Replacing all non-alphanumeric characters with empty strings
(14 answers)
Closed 4 years ago.
I want to get rid of all symbols or punctuation marks in a string
for example
String str = "\"hello!.?,\"";
the output of str should be: hello
You can use String's replaceAll.
String str = "\"hello!.?,\"";
String newStr = str.replaceAll("[^\\w]", "");
System.out.println(newStr);
For more details on how to construct a regex, see the documentation of Pattern.
This question already has an answer here:
Split regex to extract Strings of contiguous characters
(1 answer)
Closed 5 years ago.
How to split a string in java with change in alphabetical order.
For example split aaaabb to aaaa & bb.
I tried to sort the string by sort function but i am unable to split alphabets in groups.
Try with,
String str = "aaaaabbb";
String strArray[] = str.split("(?<=(.))(?!\\1)");
for(String value : strArray){
System.out.println(value);
}
This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 5 years ago.
I'm trying to fill an array of 2 elements with a element from another array, split by "." but it doesn't fill (for instance, the input is "83.105" and i need the first element to be "83" and the second "105"). When i try to get the 0 element from numberParts array it shows out of bounds exception. I'm really confused since this method is working on C# but not Java.
String[] inputNumbers = console.nextLine().split(" ");
String[] numberParts = inputNumbers[0].split(".");
System.out.println(numberParts[0]);
Dot (.) is a char that must be escaped in the split method.
why? because is a reserved character in regex
if not, splitting will return an empty array
String myString = "83.105";
String[] x = myString.split("\\.");
System.out.println(x[0]);
System.out.println(x[1]);
This question already has answers here:
String.split(".") is not splitting my long String
(2 answers)
Closed 8 years ago.
My problem is that double value converted to string cannot be splitted by dot.
Here you can see my code:
String valueOf = String.valueOf(12.34);
System.out.println("valueOf=" + valueOf);
String[] split = valueOf.split(".");
System.out.println("split=" + Arrays.toString(split));
The output is:
valueOf = 12.34
split = []
Why is split array empty?
You can try to run it on https://ideone.com/BBL4z2.
You need to escape . here. you can use \\.
String[] split = valueOf.split("\\.");
Because in regex you need to escape .
This question already has an answer here:
Split string on spaces in Java, except if between quotes (i.e. treat \"hello world\" as one token) [duplicate]
(1 answer)
Closed 8 years ago.
I have a String(freeText) "Manas \"Jaikant IBM\"". I want to split into two strings:
String normalMatch="Manas";
String exactMatch="Jaikant IBM";
It means that String normalMatch contains Manas and String exactMatch contains Jaikant IBM.
i am using the split() method of String class in Java
String[] splittedText= freeText.split("\\s");
I am getting 3 string elements but i need 2 string elements only.
Use substring instead of split:
int index = freeText.indexOf(" ");
String normalMatch = freeText.substring(0,index);
String exactMatch = freeText.substring(index); // endIndex == freeText.length())
Split on quotes(") then, you will get Manas and Jaikant IBM and can ignore the 3rd value.