Click to See Complete Forum and Search --> : String Manipulation in Java
Hi,
I have an error message,
"This {0} is not valid."
The String that needs to be replaced in {0} is "component" so that the final string reads "This component is not valid."
How can you do this in java.
Checked MessageFormat.Format -> This only takes (sting, and object)
Am stuck - any/all replies appreciated :confused:
thanks in advance
-Nili
chazzy
06-14-2006, 08:38 PM
post some code plz.
Here is the code i am working with. hope it helps.
I am missing the key information of how to add value into errorString - pls help.
thx
public static void guiErrorHandler ()
{
String errorString ="This {0} is not Valid";
String value ="TcComponet"
/* I need to be able to get value into errorString - how do you do this;
}
chazzy
06-14-2006, 10:05 PM
take a look at printf in the PrintStream class, might help you.
http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html
Waylander
06-14-2006, 11:38 PM
So you want to put value in place of the {0} inside the errorString ?
Use the method String.replace() it takes a regular expression and a string value, write the regular expression to match the "{0}" and send value as the replace value.
However, if you are ever going to get something like:
"This {0} is not Valid because {1} and something {2} "
(Just as a simple example... )
You will have to do a little more processing, You can use the String.Split() method split the string into an array based on a regular expression, then look at the numbers, put the right values into the positions and concatenate the string up again.
If you need any help with the specifics just ask. :)
Waylander.
Thanks for the link Chazzy
but the only thing i could find was the below but that won't work for my case. I think it deals with output stream and format :cool:
Any other suggestions.
Thanks
public PrintStream printf(String format,
Object... args)
Khalid Ali
06-15-2006, 01:14 AM
Ok here you go , I think this may help you
public static void guiErrorHandler ()
{
String errorString ="This {0} is not Valid";
String value ="TcComponet"
/* I need to be able to get value into errorString - how do you do this;*/
//following is the test code
System.out.println(replaceStr(errorString,"{0}",removeStr(value,"Component")));
}
//I have written and used following 2 functions for ur testing
/**
* This function removes a value that is provided as the second parameter
* in the function call.
* @param string :String from which a value is needed to be removed
* @param valueToRemove :Value that will be removed from the string
* @return String :Return 1 or many entries of the parameter separated by comma(,)
*/
public String removeStr(String string,String valueToRemove){
String removedValues="";
while(string.indexOf(valueToRemove)>-1){
int wstart= string.indexOf(valueToRemove);
int wend = wstart + valueToRemove.length();
removedValues+=string.substring(wstart,wend)+",";
string = string.substring(wend,string.length());
}
removedValues = removedValues.substring(0,removedValues.lastIndexOf(","));
return removedValues;
}
/**
* This function replaces a value that is provided as the second parameter
* in the function call.
* @param string :String from which a value is needed to be replaced
* @param valueToReplace :Value that will be replaced from the string
* @param valueToBeInserted :Value that will be valueToBeInserted instead of valueToReplace
* @return String
*/
public String replaceStr(String string,String valueToReplace, String valueToBeInserted){
String removedValues="";
if(string.indexOf(valueToReplace)>-1){
String s1 = string.substring(0,string.indexOf(valueToReplace));
String s2 = string.substring(string.indexOf(valueToReplace)+valueToReplace.length(),string.length());
removedValues = s1+ valueToBeInserted + s2;
}
if(removedValues.equals(""))removedValues = string;
return removedValues;
}
chazzy
06-15-2006, 07:50 AM
maybe I missed the point then, because using the printf would be like
out.printf(String format,args...);
//so maybe format is like "The %s message" and the arg would be "Larry" printing out "The Larry message"
//kind of like
out.println("Hello, world!");
mwmwnmw
06-15-2006, 08:40 AM
I'm just learning java and read a lot of these forums as a way of picking up tips and such. In regards to the question and answers listed I have what is probably a stupid question but I'll ask anyway.....
Why wouldn't this work?
public static void guiErrorHandler ()
{
String value ="TcComponet"
String errorString ="This " + value + "is not Valid";
I'm sure I'm not understanding the question properly, but asking is the only way to learn.
Khalid Ali
06-15-2006, 08:54 AM
mw, yes it will work, but let me say this(I hope)....what poster wanted is that there is an error string variable that has some value in brackets which needs to be replaced by stripping a part of string out of the second string. And my next guess is that it needs to be done programmatically such as in functions so that this can be done by just passing some values....Now if I am wrong in my understanding of this issue...then I'll just go and throw some cold water on my face....and get ready for work.. :p
You are perfectly right Khalid! That is what i want done.
Thanks for all your replies - they have been mighty helpful.
Khalid: Will test our code with System.out's to see what you did. Thanks for the detailed reply.
Waylander: Thanks for your reply as well. Can you give me a bit more details on what you meant by this:
"You will have to do a little more processing, You can use the String.Split() method split the string into an array based on a regular expression, then look at the numbers, put the right values into the positions and concatenate the string up again."
-Nili