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.
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 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("\\.", "");
This question already has answers here:
Why can't I split a string with the dollar sign?
(6 answers)
Closed 7 years ago.
I have a IPv6 string
String str = "demo1 26:11:d0a2:f020:0:0:0:a3:2123 demo2";
String searchString = "26:11:d0a2:f020:0:0:0:a3:2123";
When i use str.split(searchString) code returns
["demo1 ", " demo2"]
Which is fine but when i use:
String str = "demo1 [26:11:d0a2:f020:0:0:0:a3]:2123 demo2";
String searchString = "[26:11:d0a2:f020:0:0:0:a3]:2123";
and do str.split(searchString) it reutrns
[demo1 [26:11:d0a2:f020:0:0:0:a3]:2123 demo2]
Which is wrong i guess , can some one tell why I am getting this sort of output?
Since split function takes a regex as parameter, you need to escape those brackets otherwise this [26:11:d0a2:f020:0:0:0:a3] would match a single character only.
String searchString = "\\[26:11:d0a2:f020:0:0:0:a3\\]:2123";
str.split(searchString);
It is happening because split(String str) take regex pattern string as argument. And that string will be used as regex pattern to match all the delimiter with this pattern.
In your regex pattern you are providing character sets in [].
To make it work your way you will have to use this regex pattern string :
\[26:11:d0a2:f020:0:0:0:a3\]:2123
i.e. in java :
String searchString = "\\[26:11:d0a2:f020:0:0:0:a3\\]:2123";
I hope you are familiar with the string regexs. In java, the regex [abc] means match with a OR b OR c I encourage you to escape your square brackets try:
String str = "demo1 [26:11:d0a2:f020:0:0:0:a3]:2123 demo2";
String searchString = "\\[26:11:d0a2:f020:0:0:0:a3\\]:2123";
You have to use an escape sequence for some special characters. Use \\[ ... \\] in the searchString variable.
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:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I have a string like |serialNo|checkDelta?|checkFuture?|checkThis?|.
Now I am using the following code to split the string.
String[] splitString = str.split("|");
but when I use this I get array of string that contains each and every character, whereas I need string which contains letter like serialNo, checkDelta?, checkFuture?, checkthis?.
How to get these? Am I missing something?
You'll have to escape your pipe character (split takes a regular expression as argument and therefore "|" is a control character):
str.split("\\|");
Please note: the resulting array contains an empty string at the beginning since you have "|" at start of your string.
You are using a special character and will have to escape it: str.split("\\|");
Use StringTokenizer..
String str="|serialNo|checkDelta?|checkFuture?|checkThis?|"
StringTokenizer st=new StringTokenizer(str,"|",false);
String s1 = st.nextToken();
String s2 = st.nextToken();
String s3 = st.nextToken();
String s4 = st.nextToken();
s1=serialNo
s2=checkDelta?
s3=checkFuture?
s4=checkThis?
Refer to javadocs for reading about StringTokenizer
http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html