Click to See Complete Forum and Search --> : Removing carridge Return from A Java String
bjr149
06-12-2006, 12:11 PM
I have a box with a string that i need to check to see if there is a carridge return in the box. If there is i need to remove it. Im fairly new to Java so im not really too good at it. Is there an example of this somewhere that i can learn from? Thanks.
sjalle
06-13-2006, 06:17 PM
You could use the replace method of the String class, like:
String cr2Blank=boxString.replace('\r',' ');
Just remember, the replace methods create a new String, they don't change the initial String object.
AdRock
01-10-2007, 08:56 AM
I am trying to do the same thing but it's not working
Is there a way I can replace more than one character at a time?
I want to replace the carriage return and hyphen so it splits and hyphenated word into 2.
while((textline = MyFile.readLine()) !=null)
{
newString = textline.replace('\r',' ');
String[] numWords = newString.split(" ");
wordCount = wordCount + numWords.length;
System.out.println(textline);
sjalle
01-11-2007, 10:43 PM
Nope, not to my knowledge. You have the replace and the replaceAll -methods for the String class. The last one will replace whole sequences of characters. However, to save keystrokes you could write:
newString=textline.replace('\r',' ').replace('\'',' ');
devx.com
Copyright Internet.com Inc. All Rights Reserved