I would use Java Regular expressions.
Here is a class I wrote that simplifies
using them. Also for more pattern examples look at this java doc: http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html.
For what you want to do this _expression_
should work: [\\r\\n\\f]
import java.util.regex.*;
public class Regtil {
private Regtil() {
}
/**
*
Takes a string and based on a regular _expression_ replaces it with the given
value.
*
*
@param s The string to be changed
*
@param p The regular _expression_. Example: [^\\w'] replaces all non
a-z, A-Z characters
*
@param r The String that you wish to replace the characters that
match the pattern with.
*
@see Pattern
*
@return Changed string or if no changes original
*/
public static String
replaceReg(String s, String p, String r){
Pattern pn = Pattern.compile(p);
Matcher m = pn.matcher(s);
return m.replaceAll(r);
}
}
Example:
String s = Regtil.replaceReg(string, “[\\r\\n\\f]”,
“ “);
Hope this helps.
Andy
From:
webobjects-dev-bounces+abeier=email@hidden
[mailto:webobjects-dev-bounces+abeier=email@hidden] On Behalf Of Calven Eggert
Sent: Thursday, December 23, 2004 12:47
PM
To: email@hidden
Subject: Getting rid of Carriage
returns
Hi All,
I have text I want to export to an Excel file. The text for a field may
have carriage returns in it so I need to remove them and replace them with
spaces. I’ve used StringTokenizer and then I used the Class String
replace method and they both are not working for me.
Here is my little routine:
public String removeReturns(String
beforeStr) {
String tempStr = "";
StringTokenizer textTokens = new
StringTokenizer(beforeStr, "\n",
false);
while(textTokens.hasMoreTokens()) {
String
aToken = (String)textTokens.nextToken();
tempStr
+= aToken;
}
return tempStr;
}
It doesn’t work. It still returns my string with the carriageretuns
in it.
Can anyone help?
Calven