replacing space
I am trying to do a replaceAll on a String with a space in it to replace it with nothing (i.e. remove the space). I have tried all kinds of unicode, but I don't seem to find the right one matching a space. Because
myString = myString.replaceAll(" ","");
doesn't work.
Anyone have the answer?
/Olle
[396 byte] By [
oahnve] at [2007-9-30 10:36:33]

try [url= http://forum.java.sun.com/thread.jsp?forum=31&thread=48851] this[/url]
This works:
public class Example {
public static void main(String[] args) {
String str = "hello there again";
String temp = "";
for (int i = 0; i < str.length(); i++)
{
if( str.charAt(i)!=' ')temp +=str.charAt(i);
}
System.out.print(temp);
}
}
myString = myString.replaceAll(" ", "");
This does work. You must have had some other error.
System.out.println("Hello world".replaceAll(" ", ""));
prints out:
Helloworld
for me.
Thanks for the reply. It does not work for me in neither 1.4.2_02 or 1.4.2_04. Could it have to do with me getting the text from a JFormattedTextfield?
Have you tried the escape sequence \s ?
I have tried it, but I am not sure I am using it correctly (since it didn't work...). Would it be possible to give me an example?
Update: If I use getValue() it works, but not if I use getText().
So now you say use getValue(), but I can't. (stop reading now if you're not really interested...) The problem is that if I use getValue() the update of the parameter connected to the textfield is not updated before the textfield loses focus. I am using a tabbedpane and the results (where the parameter is involved) are calculated when the correct tab is clicked. What happens if a user clicks the tab while the altered textfield has the focus is that the textfield doesn't lose the focus until after the results are calculated. I.e. the results are calculated with the old value of the parameter.
Or, actually I am of course just being stupid when it comes to getValue() - it removes all spaces by itself...
> ... The problem is that if I use getValue() the update of the parameter connected to the textfield is not
> updated before the textfield loses focus. I am using a tabbedpane and the results (where the parameter
> is involved) are calculated when the correct tab is clicked.
> What happens if a user clicks the tab while the altered textfield has the focus is that the
> textfield doesn't lose the focus until after the results are calculated. I.e. the results are
> calculated with the old value of the parameter.
I think you must force a commitEdit() of the field... Perhaps in the ChangeListener for the tabbed pane ?
I have tried it... Doesn't do the trick. Also tried using focuslisteners with no luck.
Then, I'm afraid you'll have to post some code now...
this is the textfield
JFormattedTextField ftf = new JFormattedTextField(NumberFormat.getIntegerInstance());
ftf.setPreferredSize(tfDim);
ftf.setColumns(6);
ftf.setMinimumSize(tfDim);
ftf.getDocument().addDocumentListener(this);
This is the doc-listener
Document doc = e.getDocument();
try{
if(doc == ftf.getDocument()){
String text = bcBestCementTf.getText();
if(!text.equals("")){
HkaMatrix.procedureCostScen1_better = Double.parseDouble(text);
}
}
}catch(NumberFormatException nfe){
}
A tip that would do it is how to find a space when getting the text from the JFormattedTextfield since that is what I need to check for - the locale I have uses the space for large numbers (i.e. 10 000 000). Another tip (the best one) would be to find the way to use getValue() that gets the actual value when the tab is clicked (i.e. referring to the lost focus-case above). I don't know what more code I should post, but I will try this for now...