How to split a string in java by using following String? [duplicate] - java

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

Related

Split the String when it contains operation symbols in Java? [duplicate]

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]

In java, can i use string split to split by character? [duplicate]

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();

Remove all occurrences of char "." from string in Java [duplicate]

This question already has answers here:
How to replace a String in java which contains dot?
(3 answers)
Closed 4 years ago.
I would like to remove the period/decimal character from a String using Java.
String originalString = "1.2345";
originalString = originalString.replaceAll(".", "");
Printing originalString returns empty.
How can I remove . from originalString?
The first argument of replaceAll is a regex pattern. Since . means "any character", all the characters are removed. In order to refer to the actual . character, you need to escape it:
originalString = originalString.replaceAll("\\.", "");
String originalString = "1.2345";
originalString = originalString.replaceAll("\\.", "");

How to split a String into 3 STRINGS [duplicate]

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.

Using regex to trim a specific word in String [duplicate]

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.

Categories