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";
Related
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:
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:
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: Getting a substring from a string starting after a particular character
(10 answers)
Closed 8 years ago.
I would like to extract ccadmin from /ccadmin/hrp/filelist ?
i know that i can get last substring using
String uri = "/ccadmin/hrp/filelist";
String commandKey = uri.substring(uri.lastIndexOf("/") + 1, uri.length());
but how to extract first Substring ccadmin?
If I understand your question, then you could use something like,
String uri = "/ccadmin/hrp/filelist";
String first = uri.substring(1, uri.indexOf("/", 2));
System.out.println(first);
Output is
ccadmin
Split the string by the / character:
String[] parts = uri.split("/");
parts[1] is what you want.
you can use this to split your string and then get the part of uri you need:
String uri = "/s/d/f";
String[] parts = uri.split("/");
String item = parts[1];
System.out.println( item );
This question already has answers here:
Java: Simplest way to get last word in a string
(7 answers)
Closed 8 years ago.
I need some help to split up a string. Right now, my string contains this:
"RM 8 Text"
Now I only want Text to be printed in my string builder using append, how do I get rid of the RM 8 in the start of the string?
Right now I have done it this way, but there has to be an easier way.
String[] lines = fromServer.split("\\s+");
String line2 = lines[2];
logbuilder.append(line2);
Assuming that is standard format
String lastWord = fromServer.substring(fromServer.lastIndexOf(" ")+1);
You could simply replace the RM 8.
String content = fromServer.replaceAll("RM 8", "");
logBuilder.append(content);
try this
s = s.replaceAll(".*\\s+(\\S+)$", "$1")
Try the following code sample,
String abc = "RM 8 Text" ;
int a = abc.lastIndexOf(" ");
String xyz = abc.substring(a,abc.length());
System.out.println(xyz);