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 :
Related
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.
I have a url for examle:
http://www.abc.com/ABC/ABC-Boots-in-Leather/Prod/product.aspx?iid=34487
i have to convert it into:
http://www.abc.com/product.aspx?iid=34487
I am using a regex expression as :
String u = url.replaceAll("/.*?/","");
But it doesn't remove the text but just removes the slashes.? How should i correct it?
int x = url.indexOf('/');
int y = url.lastIndexOf('/')+1;
String u = url.substring(0, x) + url.substring(y);
String s = "http:www.abc.com/ABC/ABC-Boots-in-Leather/Prod/product.aspx?iid=34487";
String s1 = s.replaceAll("(/(.)*/)","/");
output: http:www.abc.com/product.aspx?iid=34487
Updated to changed question
Probably not simplest solution, but works
String url = "http://www.abc.com/ABC/ABC-Boots-in-Leather/Prod/product.aspx?iid=34487";
String u = "http:/" + url.replaceAll("(http://)|/.*/", "/");
Result:
http://www.abc.com/product.aspx?iid=34487
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 ");
I am working with this piece of code:
start = str.indexOf('<');
finish = str.indexOf('>');
between = str.substring(start + 1, finish);
replacement = str.substring(start, finish + 1);
System.out.println(between + " " + replacement); //for debug purposes
forreplacement = JOptionPane.showInputDialog(null, "Enter a " + between);
System.out.println(forreplacement); //for debug purposes
counter = counter - 1;
where "str" is the String that is being worked with. How do I replace the substring "replacement" with the string "forreplacement" (what is entered into the popup box) within the string str?
Not sure if that is what you want but maybe take part before start, add forreplacemeent and after that add part after finish like
String result = str.substring(0, start)+forreplacement+str.substring(finish+1);
Or if you want to replace all occurrences of replacement with forreplacement you can also use
String result = str.replace(replacement, forreplacement)
Your question is not clear. If you want to replace, you can do something like-
String str = "Hello World!";
String replacement = "World";
String stringToReplaceWith = "Earth";
str = str.replace(replacement, stringToReplaceWith);
System.out.println(str); // This prints Hello Earth!
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");