This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 3 months ago.
I want to split the string when it contains the symbol "+" and "-", how can I do that?
Example:
str1 = "2x^3+3x-8";
//Result:
['2x^3', '3x', '8']
A regex split should work here:
String str1 = "2x^3+3x-8";
String[] parts = str1.split("[+-]");
System.out.println(Arrays.toString(parts)); // [2x^3, 3x, 8]
Related
This question already has answers here:
Split string into array of character strings
(12 answers)
Closed 3 years ago.
For instance, I have the String variable abbaabbbba. I would like to use String split so each character would be separated, so it would be {a,b,b,a,a,b,b,b,b,a}. All the instructions I can find for string split say i need to have something, like a a space:
String mySplit = str.split("/");
Is there anyway to do this by character?
You can use the toCharArray method like this:
String myString = "abbaabbbba";
char[] myCharacters = myString.toCharArray();
This question already has answers here:
How to split a string, but also keep the delimiters?
(24 answers)
Closed 3 years ago.
if I read 1symbol2 as a String i can split that using String.split("") into 3 variables but if I read 12 symbol 16 as a string and if I apply String.split(" ") it is split into 6 variables. How can I split that into 3 variables that are (12, symbol,16)?
Note:
The following any of them can be considered as Symbols +,-,*,/,%,~,!,#,#,$,^,&
If you can separate the three string 12 + and 16 with a comma, means something like --> 12,+,16 then below code will work for you.
String str = "12,+,16";
String a[] = str.split(",");
System.out.println(a[0]+" "+a[1]+" "+a[2]);
Result will be --> 12 + 16
Try this and let me know
You can use following regex to separate your string in parts:
String myString = "12+16";
String[] result = myString.split("(?<=[-+*/])|(?=[-+*/])");
System.out.println(Arrays.toString(result));
Output:
[12,+,16]
Instead of String.split(" "), you can just do: String.split("+")
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 5 years ago.
Can someone please tell me how to split this string?
Sample Input
String in = "Erin M Haggens";
Desired Output
String str1 = "Erin";
String str2 = "M";
String str3 = "Haggens";
Use the String.split() method.
The code myStr.split("\\s+") should work for you. Then you can use each element of the returned array as one of your outputted strings.
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 7 years ago.
How to split the following string How to split following string "LLSlotBook17-07-2015#Friday#1#10.00AM-12.00PM#10#LMV,mCWG" with ',' and '#'
You can use String.split(regexp) function :
String[] array = "LLSlotBook17-07-2015#Friday#1#10.00AM-12.00PM#10#LMV,mCWG".split(",|#");
You should prefer the String.split method as mentioned by VLef,
but for the sake of completeness:
String s = "LLSlotBook17-07-2015#Friday#1#10.00AM-12.00PM#10#LMV,mCWG";
int x = s.indexOf("#");
String sub = s.substring(0, x);
will give you the first substring "LLSlotBook17-07-2015".
See: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
This question already has answers here:
Java regular expression to match _all_ whitespace characters
(7 answers)
Closed 8 years ago.
I'm trying to replace all all spaces in string with one space. I'm trying this:
String src = "2. Test Sentence with spaces";
String out = src.replaceAll("\\s+", " ");
System.out.println(out);
And this is what I'm getting:
2. Test Sentence with spaces
Spaces after dot were not replaced... Why?
You can try with the Unicode category: separator, space, combined with whitespace:
String input = "\u0020\u00A0\u1680\u2000\u2001\t"; //etc. 17 characters
System.out.println(input.replaceAll("[\\p{Zs}\\s]+", " "));
Output
[1 space]
See here for the list of characters in category Zs.