Checking for special characters

G

Guest

I have a string that can't contain the following characters:
../\|}{[:;+=_)(*&^%$#@!~`

What would be easiest way to check the string?

Should I use regexp or create a array with these characters and compare each
character of string to the array?

Thanks
 
G

Guest

jcooper said:
Take a look at the String.Trim method. That should help you out.

http://msdn.microsoft.com/library/d...pref/html/frlrfsystemstringclasstrimtopic.asp

Never thought to use trim. I guess this would work. Once I find one instance
of the trim result being empty I can exit, since I know I have an error.

Thanks.

public void trimstring()
{
String str1 = "*;|@123***456@|;*";
String delim = "*";
String str2; //= str1.Trim(delim.ToCharArray());

Console.WriteLine("Delimiters: {0}", delim);
Console.WriteLine("Original string: {0}", str1);

for( int i = 0; i < str1.Length; i++)
{
str2 = str1.ToString().Trim(delim.ToCharArray());
if (str2 =="")
Console.WriteLine("Trimmed string: Empty");
Console.WriteLine("Trimmed string: {0}", str2);
}
}
 
G

Guest

2 things here:

1. Trim(char[]) removes those chars from only the beginning and end of the
string -- not the middle.

2. Trim is expensive -- each call returns a new string.

If you just want to see if the string contains those chars, check
IndexOfAny(char[]) - if it returns > -1 you have at least one of those chars
in your string.


dwight said:
jcooper said:
Take a look at the String.Trim method. That should help you out.

http://msdn.microsoft.com/library/d...pref/html/frlrfsystemstringclasstrimtopic.asp

Never thought to use trim. I guess this would work. Once I find one instance
of the trim result being empty I can exit, since I know I have an error.

Thanks.

public void trimstring()
{
String str1 = "*;|@123***456@|;*";
String delim = "*";
String str2; //= str1.Trim(delim.ToCharArray());

Console.WriteLine("Delimiters: {0}", delim);
Console.WriteLine("Original string: {0}", str1);

for( int i = 0; i < str1.Length; i++)
{
str2 = str1.ToString().Trim(delim.ToCharArray());
if (str2 =="")
Console.WriteLine("Trimmed string: Empty");
Console.WriteLine("Trimmed string: {0}", str2);
}
}
 
G

Guest

jcooper said:
Take a look at the String.Trim method. That should help you out.

http://msdn.microsoft.com/library/d...pref/html/frlrfsystemstringclasstrimtopic.asp

Never thought of using trim. I guess I could do the following. I only need
to find the first occurance and then I can break the loop.

Thanks.

public void trimstring()
{
String str1 = "*;|@123***456@|;*";
String delim = "*";
String str2; //= str1.Trim(delim.ToCharArray());

Console.WriteLine("Delimiters: {0}", delim);
Console.WriteLine("Original string: {0}", str1);

for( int i = 0; i < str1.Length; i++)
{
str2 = str1.ToString().Trim(delim.ToCharArray());
if (str2 =="")
Console.WriteLine("Trimmed string: Empty");
Console.WriteLine("Trimmed string: {0}", str2);
}
}
 
G

Guest

KH said:
2 things here:

1. Trim(char[]) removes those chars from only the beginning and end of the
string -- not the middle.

2. Trim is expensive -- each call returns a new string.

If you just want to see if the string contains those chars, check
IndexOfAny(char[]) - if it returns > -1 you have at least one of those chars
in your string.

That would do exactly what I want.

Thank you.
 
G

Guest

Sorry, I was thinking about removing the special characters from your string
so it could still be processed.

dwight said:
jcooper said:
Take a look at the String.Trim method. That should help you out.

http://msdn.microsoft.com/library/d...pref/html/frlrfsystemstringclasstrimtopic.asp

Never thought of using trim. I guess I could do the following. I only need
to find the first occurance and then I can break the loop.

Thanks.

public void trimstring()
{
String str1 = "*;|@123***456@|;*";
String delim = "*";
String str2; //= str1.Trim(delim.ToCharArray());

Console.WriteLine("Delimiters: {0}", delim);
Console.WriteLine("Original string: {0}", str1);

for( int i = 0; i < str1.Length; i++)
{
str2 = str1.ToString().Trim(delim.ToCharArray());
if (str2 =="")
Console.WriteLine("Trimmed string: Empty");
Console.WriteLine("Trimmed string: {0}", str2);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top