473,671 Members | 2,558 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 1528
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
8742
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 "i" and two "s". The function should also handle if String A and B have huge values, like two big dictionary. What's the best approach to achieve this with the best performance? what's the Big O then? I am thinking to put A and B into two...
2
2021
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 doesn't override 'Equals' method, but I _DO_ override it in my class. Have a look at thread:...
38
6923
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
5174
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 Framework 2.0 has this method. It seems to me that the question whether a collection has some element should be answerable by any collection, not just IList or IDictionary. An obvious implementation must exist, since every ICollection inherits...
2
5095
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 this. I have tried using the Like comparision operator but about all I can do with it is replace a whole string that contains the wildcard search string. I want to do something like finding *mysea?rch in a string like "This is mysearch string...
11
3936
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
11985
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 as that returned from Active Directory. For example using 'cn=' in the userdn string instead of 'CN=' does not work. As far as I am aware Active Directory is not case sensitive and it is therefore difficult to predict the case of a string.
1
1162
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
1208
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
1369
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 have shown how to use custom collections to store lists of data. I've coded the custom collection and before I add members to it, I want to use the contains method to ensure the data does not already exist in the collection. My play code is...
0
8403
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8930
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8828
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8605
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8677
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4417
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.