String replace using replace first method - java

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

Related

String replaceAll not replacing i++;

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\\+\\+;", "");

Replace anything within two brackets

I have this string:
String string = "The status is %status(" + randomString + ")%";
How can I replace the part between percent signs with anything, but without knowing randomString?
You can use String.replaceAll. An example:
String randomString = "asdf";
String string = "The status is %status(" + randomString + ")%";
String replacement = "myStatus";
String fString = string.replaceAll("%status(.*)%", replacement);
System.out.println(fString);
Which outputs:
The status is myStatus
The regex you want to use is %(.+)%, which matches %status(foo)% and %status(asidh37887123-48hsZXas;;fgfg)%.
To replace it, you can do string.replaceAll("%(.+)%", "anything").
This will turn The status is %status(foo)% to The status is anything.
Two ways to do it :
String string = "The status is %status(aaaaaaaaaaaaaaa)%";
int x = string.indexOf("%");
int y = string.lastIndexOf("%");
System.out.println(x);
System.out.println(y);
string = string.substring(0,x)+string.substring(y+1,string.length());
System.out.println(string);
string = "The status is %status(aaaaaaaaaaaaaaa)%";
string = string.replaceAll("%.+%", "");
System.out.println(string);
Result :

Android String format not working

I have a problem with String.format In android I want replace { 0 } with my id.
My this code not working:
String str = "abc&id={0}";
String result = String.format(str, "myId");
I think you should use replace method instead of format.
String str = "abc&id={0}";
str.replace("{0}","myId");
you have 2 ways to do that and you are mixing them :)
1.String format:
String str = "abc&id=%s";//note the format string appender %s
String result = String.format(str, "myId");
or
2.Message Format:
String str = "abc&id={0}"; // note the index here, in this case 0
String result = MessageFormat.format(str, "myId");
You have to set your integer value as a seperate variable.
String str = "abc&id";
int myId = 001;
String result = str+myId;
try this,
String result = String.format("abc&id=%s", "myId");
edit if you want more than one id,
String.format("abc&id=%s.id2=%s", "myId1", "myId2");
The syntax you're looking for is:
String str = "abc&id=%1$d";
String result = String.format(str, id);
$d because it's a decimal.
Other use case:
String.format("More %2$s for %1$s", "Steven", "coffee");
// ==> "More coffee for Steven"
which allows you to repeat an argument any number of times, at any position.

how to remove \r and \n from my string in java?

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.

Get the specific value from the last part of a String in Android

I wanted to get the value from the last part of a String, here's my example String
String str="www.mywebsite.com?id=0001&user=myname"
I like to get the word myname from that String, All examples that I'm seeing is like this
String getUser = str.substring(str.length() - 6);
but user value length changes every transaction so I can't fix that to any value. Can anyone please help me in how I will be able to get the user value from that String. Thanks.
String getUser=str.subString(str.lastIndexOf("=")+1,Str.length());
Will return myname.
If your string is going to be a uri, you can use Uri's getQueryParameter:
String str="www.mywebsite.com?id=0001&user=myname";
Uri uri = Uri.parse(str);
return uri.getQueryParameter("user");
Try this
String getUser = str.substring(str.lastIndexOf("=") + 1);
Try this
String foo = "www.mywebsite.com?id=0001&user=myname";
String[] split = foo.split("=");
String name = split[split.length-1];
with String.split(), you can split strings with a delimiter and put the values in an array. It is similar to PHPs' explode method.
String str="www.mywebsite.com?id=0001&user=myname";
String user = getQueryParamValue(str);
public static final String getQueryParam(String url) {
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
for (NameValuePair param : params){
if("user".equalsIgnoreCase(param.getName())) {
return param.getValue();
}
}
return null;
}

Categories