|
-
Jul 8th, 2004, 11:47 AM
#1
Thread Starter
Addicted Member
Cleaning a string
I'm reading in a string from a database, but there are non-printing characters that are being read in that cause problems elsewhere.
I thought it was just a newline, but the following didn't work:
string theString = "junk read in from database";
theString.Replace("\n", "");
//This didn't work either:
theString.Replace(Environment.NewLine, "");
Since those don't work, I can only assume that there are other types of characters in the string. Is there a simple way to fix the string, or do I just need to write a loop going through the string one character at a time? What about regexp?
cudabean
-
Jul 8th, 2004, 12:28 PM
#2
Sleep mode
So you want to replace white spaces with newline chars ? If so , then try this :
PHP Code:
string theString = "junk read in from database";
theString =theString.Replace(" ",Environment.NewLine);
-
Jul 14th, 2004, 08:57 AM
#3
Frenzied Member
You may need to replace carriage returns and line feeds both.
Code:
theString.Replace("\r", "");
theString.Replace("\n", "");
-
Jul 14th, 2004, 02:35 PM
#4
Thread Starter
Addicted Member
Thanks all responders. I found this did the job nicely:
Code:
public static String filter(String userInput)
{
Regex re = new Regex("([^A-Za-z0-9@.' _-]+)");
String filtered = re.Replace(userInput, "");
return filtered;
}
It get's rid of more than just any non-printing character, but that was OK in this instance. The regular expression above only allows the character ranges that are listed and kills everything else.
cudabean
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|