473,511 Members | 16,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I search for a char in a string using the Contains method.

110 New Member
I am trying to run a test that will examine a selected file for illegal characters. It needs to test each read line before it fails or completes. I have this:

Expand|Select|Wrap|Line Numbers
  1. string[] lines = File.ReadAllLines(@newobject.csvDirectory);
  2.  
  3. string[] illegal = { "/", "*", "\\", "|", 
  4. ":", "\"", "?", "<", ">" };
  5.  
  6.  
  7.  
  8. // Return an error for file with illegal characters
  9. for (int i = 0; i < lines.Length; i++)
  10. {
  11.  
  12. if (lines[i].Contains(illegal[]))
  13. {
  14. newobject.illegalError();
  15. }
  16.  
in this line of code
Expand|Select|Wrap|Line Numbers
  1. if (lines[i].Contains(illegal[]))
  2.  
I need it to search lines[i] for every declared char in the illegal array.

Thanks for our help with this.
Jun 27 '10 #1
3 1520
Christian Binder
218 Recognized Expert New Member
You have to iterate over each element of you char-array and do a check.

Also you could use string's IndexOfAny-method, which takes a char-array and returns the index of any match.
Jun 28 '10 #2
Fuzz13
110 New Member
If I understand what indexofany does I like that as my option but I've never used it and don't understand how to impliment it in my code from reading msdn's examples. Any chance you could give me an idea of what it would look like?
Jun 28 '10 #3
brunoss
8 New Member
The first thing you have to do is to turn your array to a char array because you are seaching for a char not for a string. then do the following

Expand|Select|Wrap|Line Numbers
  1. if(lines[i].IndexOfAny(illegal) != -1)
  2. //in this condition lines[i] has illegal chars
  3.  
Jun 28 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

13
8717
by: usgog | last post by:
I need to implement a function to return True/false whether String A contains String B. For example, String A = "This is a test"; String B = "is is". So it will return TRUE if String A includes two...
2
2001
by: Piotr Szukalski | last post by:
Hi! I have trouble with 'Contains' method in ListViewItemCollection class - it seems like it nevers calls 'Equals' method of class inherited from ListViewItem... I've found that ListViewItem...
38
6885
by: Alan | last post by:
I want to change a 3 digits integer to characters, how can i do that? the 3 digits integer maybe 123, 23 or 3 I want to change the integer to "123", " 23" or " 3" thx
3
5156
by: andrew.miadowicz | last post by:
It's funny that I've only now run into this question, after a few years of using C#, but I find it intriguiging all the same. All the more so, that the generic version of ICollection in .Net...
2
5071
by: Dennis | last post by:
I am trying to implement a "Find and Replace" dialog that allows using wildcards in the find string, much like the Find and Replace Dialogs in Ms Word, etc. Are there any references or examples on...
11
3914
by: wreckingcru | last post by:
I'm trying to tokenize a C++ string with the following code: #include <iostream> #include <stdio> #include <string> using namespace std; int main(int argc, char* argv)
8
11971
by: Chris Noble | last post by:
I need to check whether a particular user is already a member of an Active Directory Security Group. The following code extract works but only if the user distinguished name is exactly the same...
1
1157
by: rviancha | last post by:
Hi, I am trying to find some funtions that i will use to find a string in many files, the most important thing is that i will use a pattern to do the search.
4
1201
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, can someone please show me how to search my String array to see if an element "contains" a certain string constant and then return the index value of the found element? thanks, rodchar
8
1362
by: Tanzen | last post by:
I'm working in visual studio 2005 trying to learn visual basic. Having come from an VB for Access background, I'm finding it a big learning curve. I have been working through several e-books which...
0
7245
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
7356
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
7427
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...
1
7085
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
7512
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...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.