I'm just trying to remove (replace with "") \r and \n from my JSON. Here is the method I'm currently testing which doesn't work.
public static void testing(){
String string = "\r\r\r\n\n\n";
string.replace("\r", "");
string.replace("\n", "");
}
Try this regex (\\r\\n|\\n|\\r) and String#replaceAll, like:
string = string.replaceAll("(\\r\\n|\\n|\\r)", "");
After replacing you need to assign back to the original string. Because the string is immutable you cannot change the value of a string.
You need to use
String string = "\r\r\r\n\n\n";
string = string.replace("\r", "");
string = string.replace("\n", "");
Or you can use any libraries like Apache StringUtils.If you are using these utils , no needs to assign back the value to String
try this:
public static void testing(){
String string = "\r\r\r\n\n\n";
string = string.replace("\r", "");
string = string.replace("\n", "");
}
because replace return another string(new String) because String is immutable so unable to modified directly
String.replace will return a string. It doesn't change its value.
public static void testing(){
String str = "\r\r\r\n\n\n";
str = str.replace("\r", "");
str = str.replace("\n", "");
}
String string = "\r\r\r\n\n\n";
String newStr = string.replace("\r", "");
newStr = newStr.replace("\n", "");
System.out.println(newStr);
String will return new String Object.
try this:
String string = "\rte\r\rs\nti\nn\ng";
String newString = string.replaceAll("[^\\w]", "");
String string = "\r\r\r\n\n\n";
string = string.replaceAll("\r", "");
string = string.replaceAll("\n", "");
Try this,
string = string.replaceAll("[\r\n]", "");
Here is what I found:
data_json = data_json.replaceAll("\\\\r\\\\n", "");
Copied from OP's comment.
Related
String preCode = "helloi++;world";
String newCode = preCode.replaceAll("i++;", "");
// Desired output :: newCode = "helloworld";
But this is not replacing i++ with blank.
just use replace() instead of replaceAll()
String preCode = "helloi++;world";
String newCode = preCode.replace("i++;", "");
or if you want replaceAll(), apply following regex
String preCode = "helloi++;world";
String newCode = preCode.replaceAll("i\\+\\+;", "");
Note : in the case of replace() the first argument is a character sequence, but in the case of replaceAll the first argument is regex
try this one
public class Practice {
public static void main(String...args) {
String preCode = "Helloi++;world";
String newCode = preCode.replace(String.valueOf("i++;"),"");
System.out.println(newCode);
}
}
The problem is the string that you are using to replace , that is cnsidered as regex pattern to skip the meaning you will have to use escape sequence like below.
String newCode = preCode.replaceAll("i\\+\\+;", "");
I want to return "https://rumpelstiltskin.com:7071/service/admin" from "https://rumpelstiltskin.com:7071/service/admin/soap"
I'm doing like this:
String str="https://rumpelstiltskin.com:7071/service/admin/soap";
String strOut="";
strOut=str.split("/soap")[0];
System.out.println(strOut);
Is there a more elegant way to do that?
What about the following using String#replace():
String str = "https://rumpelstiltskin.com:7071/service/admin/soap";
String newString = str.replace("/soap", "");
System.out.println(newString);
Output is (tried and tested):
https://rumpelstiltskin.com:7071/service/admin
Try this
String str="https://rumpelstiltskin.com:7071/service/admin/soap";
String newStr = str.substring(0,str.lastIndexOf("/"));
System.out.println(newStr);
You may try this:-
String str = "https://rumpelstiltskin.com:7071/service/admin/soap";
String s = str.replace("/soap", "");
String str="https://rumpelstiltskin.com:7071/service/admin/soap";
String strOut= str.substring(0, str.lastIndexOf("/"));
Only works if, as asked the last chunk is always /soap
String str="https://rumpelstiltskin.com:7071/service/admin/soap";
String strOut=str.substring(0, str.lastIndexOf("/soap"));
System.out.println(strOut);
Example:
Input
Str = P.O.Box
Output
Str= PO BOX
I can able to convert the string to uppercase and replace all dot(.) with a space.
public static void main(String args[]){
String s = "P.O.Box 1836";
String uppercase = s.toUpperCase();
System.out.println("uppercase "+uppercase);
String replace = uppercase.replace("."," ");
System.out.println("replace "+replace);
}
System.out.print(s.toUpperCase().replaceFirst("[.]", "").replaceAll("[.]"," "));
If you look the String API carefully, you would notice that there's a methods that goes by:-
replaceFirst(String regex, String replacement)
Hope it helps.
You have to use the replaceFirst method twice. First for replacing the . with <nothing>. Second for replacing the second . with a <space>.
String str = "P.O.Box";
str = str.replaceFirst("[.]", "");
System.out.println(str.replaceFirst("[.]", " "));
This one liner should do the job:
String s = "P.O.Box";
String replace = s.toUpperCase().replaceAll("\\.(?=[^.]*\\.)", "").replace('.', ' ');
//=> PO BOX
String resultValue = "";
String[] result = uppercase.split("[.]");
for (String value : result)
{
if (value.toCharArray().length > 1)
{
resultValue = resultValue + " " + value;
}
else
{
resultValue = resultValue + value;
}
}
Try this
System.out.println("P.O.Box".toUpperCase().replaceFirst("\\.","").replaceAll("\\."," "));
Out put
PO BOX
NOTE: \\ is needed here if you just use . only your out put will blank.
Live demo.
You should use replaceFirst method twice.
String replace = uppercase.replace("\\.", "").replaceFirst("\\.", "");
As you want to remove the first dot and replace the second one with a space, you need replace the whole P.O. section
Use
replace("P\\.O\\.", "PO ");
In Java I have a String
String string = "sdfgjhjdfg.m\"gb=1234509876\"xcvbnfghj".
I want to replace it with Hi="1234509876".
In string replace function i could not do this.
string = string.replace(".*gb=(.*)\".*","Hi=(.*)");
In the 2nd parameter (.*) group, the group in 1st parameters should get replace
Please Help me....
Try to use the following code for getting number string from main string
public static void main(String[] args)
{
String string = "sdfgjhjdfg.m\"gb=1234509876\"xcvbnfghj";
Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher(string);
while (m.find()) {
string=m.group();
}
System.out.println("res"+string);
}
You can try something like this
String string = "sdfgjhjdfg.m\"gb=1234509876\"xcvbnfghj";
String newStr=string.replaceAll("gb=1234509876","Hi=1234509876");
System.out.println(newStr);
try
string = string.replace("gb=","Hi=");
try this
String string = "sdfgjhjdfg.m\"gb=1234509876\"xcvbnfghj";
System.out.println(string);
string = string.replaceAll("gb=-?\\d+","Hi='new value'");
System.out.println(string);
I was trying to use the string replace with another, it doesn't happen.
String requestURI = "/webapps-ab/public/Test.jsp"
String contextName = "webapps-ab";
String newRequestURI = requestURI.replaceFirst(contextName,"webapps");
I am expecting newRequestURI to be "/webapps/public/Test.jsp".
Your replace call should be:
String newRequestURI = requestURI.replaceFirst(contextName, "webapps");
Using:
String requestURI = "/webapps-ab/public/Test.jsp";
String contextName = "webapps-ab";
String newRequestURI = requestURI.replaceFirst(contextName, "webapps");
System.out.println("newRequestURI: " + newRequestURI);
The output will be what you're expecting:
newRequestURI: /webapps/public/Test.jsp
ideone example.
When referencing a variable, do not surround it in quotes, as that converts it into a literal String object.
String newRequestURI = requestURI.replaceFirst(contextName, "webapps");