I am passing in text which is combination of {} filler and text. I am trying to fill {} with some values and tried using MessageFormat.
String sss = "{0}SomeText{1}\'.{2}SomeText{2}SomeText{0}{0}SomeText{2}{0}SomeText{0}{1}SomeText{0}{2}{0}{0}{1}{0}{2}{0}{0}{2}{0}{0}{1}{0}{2}{0}";
Object[] testArgs = {"nits1", "Nits2","nits#"};
System.out.println(MessageFormat.format(sss,testArgs));
OUTPUT
nits1SomeTextNits2.{2}SomeText{2}SomeText{0}{0}SomeText{2}{0}SomeText{0}{1}SomeText{0}{2}{0}{0}{1}{0}{2}{0}{0}{2}{0}{0}{1}{0}{2}{0}
The single quote must be escaped using a double single quote:
String sss = "{0}SomeText{1}''.{2}S..."
My mistake was, that I didn't use the returned value from method 'format(..)'
Wrong code:
MessageFormat.format(sss, testArgs);
System.out.println(sss);
Correct code:
String newString = MessageFormat.format(sss, testArgs);
System.out.println(newString);
Related
I have one variable representing the regex, when run replaceAll, none of string is replaced. Please help to take a look.
String s = "Issue 3 for 5 describe the title";
String regex = "Issue\\s\\d+\\sfor\\s\\d+";
System.out.println(s.replaceAll(regex, "test"));
replaceAll returns the modified String, but it does not modify the original String, as String in Java is immutable.
You need to:
String resultString = s.replaceAll(regex, "test")
System.out.println(resultString);
Java String is immutable.
If you want to change string s use this:
s = s.replaceAll(regex, "test"));
This could be caused by the fact that in your code you have to double escape your regexp, but in the xml you don't:
String regex = "Issue\\s\\d+\\sfor\\s\\d+";
is equivalent to the parsed
<regex>Issue\s\d+\sfor\s\d+</regex>
Say I enter a string:-
Hello
Java!
I want the output as:-
Hello\nJava!
Is there any way in which I could get the output in this format?
I am stuck on this one and not able to think about any logic which could do this for me.
It looks like http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html does what you want.
EG
String escaped = StringEscapeUtils.escapeJava(String str)
Will return "Hello\nJava!" when supplied with your string.
Hm..You can replace the new line character(\n) with \\n. For example:
String helloWithNewLine = "Hello\nJava";
String helloWithoutNewLine = helloWithNewLine.replace("\n", "\\n");
System.out.println(helloWithoutNewLine);
Output:
Hello\nJava
I have a string like this:
String str="\"myValue\".\"Folder\".\"FolderCentury\"";
Is it possible to split the above string by . but instead of getting three resulting strings only two like:
columnArray[0]= "myValue"."Folder";
columnArray[1]= "FolderCentury";
Or do I have to use an other java method to get it done?
Try this.
String s = "myValue.Folder.FolderCentury";
String[] a = s.split(java.util.regex.Pattern.quote("."));
Hi programmer/Yannish,
First of all the split(".") will not work and this will not return any result. I think java String split method not work for . delimiter, so please try java.util.regex.Pattern.quote(".") instead of split(".")
As I posted on the original Post (here), the next code:
String input = "myValue.Folder.FolderCentury";
String regex = "(?!(.+\\.))\\.";
String[] result=input.split(regex);
System.out.println("result: "+Arrays.toString(result));
Produces the required output (an array with two values):
result: [myValue.Folder, FolderCentury]
If the problem you're trying to solve is really that specific, you could do it even without using regular expression matches at all:
int lastDot = str.lastIndexOf(".");
columnArray[0] = str.substring(0, lastDot);
columnArray[1] = str.substring(lastDot + 1);
In java, I have a string with a date in dd-mm-yyyy format:
String value = "31-01-1989";
Now, I want the value in another variable to be ddmmyyyy format:
String value = "31011989";
How to do this?
In this case you can simply remove dashes
value = value.replace("-", "");
or
value = value.replaceAll("-", "");
but according to my tests the first version is a little bit faster. So I personally prefer to use replaceAll only when the first parameter is a regex.
Note that, despite a confusion in the names, String.replace replaces ALL substrings that match the first arg, just as String.replaceAll does. The main difference is that the String.replace treats the first arg as a string literal and String.replaceAll uses it as a regex.
easy solution:
String value1 = "31-01-1989";
String value2 = value1.replace("-", "");
Have a look at SimpleDateFormat for a general solution. Write a SimpleDateFormat to parse the first date and use format in another to have the expected output.
You can use String.replace(Charseq, Charseq) to remove the delimiters.
String value = "31-01-1989";
String value2 = value.replace("-", "");
System.out.println(value2);
I am getting response for some images in json format within this tag:
"xmlImageIds":"57948916||57948917||57948918||57948919||57948920||57948921||57948 922||57948923||57948924||57948925||57948926||5794892"
What i want to do is to separate each image id using .split("||") of the string class. Then append url with this image id and display it.
I have tried .replace("\"|\"|","\"|"); but its not working for me. Please help.
EDIT: Shabbir, I tried to update your question according to your comments below. Please edit it again, if I didn't get it right.
Use
.replace("||", "|");
| is no special char.
However, if you are using split() or replaceAll instead of replace(), beware that you need to escape the pipe symbol as \\|, because these methods take a regex as parameter.
For example:
public static void main(String[] args) {
String in = "\"xmlImageIds\":\"57948916||57948917||57948918||57948919||57948920||57948921||57948922||57948923||57948924||57948925||57948926||5794892\"".replace("||", "|");
String[] q = in.split("\"");
String[] ids = q[3].split("\\|");
for (String id : ids) {
System.out.println("http://test/" + id);
}
}
I think I know what your problem is. You need to assign the result of replace(), not just call it.
String s = "foo||bar||baz";
s = s.replace("||", "|");
System.out.println(s);
I tested it, and just calling s.replace("||", "|"); doesn't seem to modify the string; you have to assign that result back to s.
Edit: The Java 6 spec says "Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar." (the emphasis is mine).
According to http://docs.oracle.com/javase/6/docs/api/java/lang/String.html, replace() takes chars instead of Strings. Perhaps you should try replaceAll(String, String) instead? Either that, or try changing your String ("") quotation marks into char ('') quotation marks.
Edit: I just noticed the overload for replace() that takes a CharSequence. I'd still give replaceAll() a try though.
String pipe="pipes||";
System.out.println("Old Pipe:::"+pipe);
System.out.println("Updated Pipe:::"+pipe.replace("||", "|"));
i dont remember how it works that method... but you can make your own:
String withTwoPipes = "helloTwo||pipes";
for(int i=0; i<withTwoPipes.lenght;i++){
char a = withTwoPipes.charAt(i);
if(a=='|' && i<withTwoPipes.lenght+1){
char b = withTwoPipes.charAt(i+1);
if(b=='|' && i<withTwoPipes.lenght){
withTwoPipes.charAt(i)='';
withTwoPipes.charAt(i+1)='|';
}
}
}
I think that some code like this should work... its not a perfect answer but can help...