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.
Related
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]
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:
Delete everything after part of a string
(10 answers)
Closed 6 years ago.
I am not an expert of regex. Suppose I have this string:
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00,normal."
If I want to remove ,normal and replace it by dot so the string becomes like this:
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00."
How can I do that in regex?
Thank you very much.
You can use a regular expression like ,\\w+\\.$ which matches any String ending in , a word and then a . and String.replaceAll(String, String) like
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00,normal."
.replaceAll(",\\w+\\.$", "\\.");
System.out.println(str);
Output is (as requested)
0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00.
This question already has answers here:
Java: removing numeric values from string
(7 answers)
Closed 7 years ago.
I want to remove xxxx-xxxx in string.
String data = "help text 2015-2016 dummy string";
Need output: help text dummy string
String data = "abcd 2057-3827 any string";
Need output: abcd any string
The following code should work:
String regexp = "[^\\s]{4}-[^\\s]{4}\\s";
String str = "help text 2015-2016 dummy string";
System.out.println(str.replaceFirst(regexp, ""));
If xxxx-xxxx is always a number, then you can use:
String r = "[0-9]{4}-[0-9]{4}\\s";
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