473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Checking for special characters

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

Nov 17 '05 #1
6 15526
Take a look at the String.Trim method. That should help you out.

http://msdn.microsoft.com/library/de...strimtopic.asp

"dwight" wrote:
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

Nov 17 '05 #2


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

http://msdn.microsoft.com/library/de...strimtopic.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[i].ToString().Trim(delim.ToCharArray());
if (str2 =="")
Console.WriteLine("Trimmed string: Empty");
Console.WriteLine("Trimmed string: {0}", str2);
}
}
Nov 17 '05 #3
KH
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" wrote:


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

http://msdn.microsoft.com/library/de...strimtopic.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[i].ToString().Trim(delim.ToCharArray());
if (str2 =="")
Console.WriteLine("Trimmed string: Empty");
Console.WriteLine("Trimmed string: {0}", str2);
}
}

Nov 17 '05 #4


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

http://msdn.microsoft.com/library/de...strimtopic.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[i].ToString().Trim(delim.ToCharArray());
if (str2 =="")
Console.WriteLine("Trimmed string: Empty");
Console.WriteLine("Trimmed string: {0}", str2);
}
}
Nov 17 '05 #5

"KH" wrote:
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.
Nov 17 '05 #6
Sorry, I was thinking about removing the special characters from your string
so it could still be processed.

"dwight" wrote:


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

http://msdn.microsoft.com/library/de...strimtopic.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[i].ToString().Trim(delim.ToCharArray());
if (str2 =="")
Console.WriteLine("Trimmed string: Empty");
Console.WriteLine("Trimmed string: {0}", str2);
}
}

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Roy W. Andersen | last post by:
I've been searching google about this for days but can't find anything, so I'm hoping someone here can help me out. I'm trying to create zip-files without needing the zip-file extension in PHP,...
3
by: Barry Olly | last post by:
Hi, I'm working on a mini content management system and need help with dealing with special characters. The input are taken from html form which are then stored into a varchar column in...
4
by: Ewok | last post by:
let me just say. it's not by choice but im dealing with a .net web app (top down approach with VB and a MySQL database) sigh..... Anyhow, I've just about got all the kinks worked out but I am...
5
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file"....
17
by: Carl Mercier | last post by:
Hi, Is it possible to use special characters like \n or \t in a VB.NET string, just like in C#? My guess is NO, but maybe there's something I don't know. If it's not possible, does anybody...
8
by: david.lindsay.green | last post by:
Hello all, I am quite new a web scripting and making web pages in general and I have stumbled across a problem I have as yet been unable to solve. I am trying to take the contents of a textarea box...
5
by: Doc | last post by:
Hello! I'm experiencing a little problem counting the number of characters in a textarea on a html page. This is the content type of my HTML document content="text/html; charset=iso-8859-1" ...
3
KevinADC
by: KevinADC | last post by:
Purpose The purpose of this article is to discuss the difference between characters inside a character class and outside a character class and some special characters inside a character class....
3
ilikesuresh
by: ilikesuresh | last post by:
Hi, I need a way to remove special characters in C. do you have any way other than checking ascii values? Please let me Know Thanks in advance
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.