Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a string contains a character in C#?

Tags:

string

c#

Is there a function I can apply to a string that will return true of false if a string contains a character.

I have strings with one or more character options such as:

var abc = "s"; var def = "aB"; var ghi = "Sj"; 

What I would like to do for example is have a function that would return true or false if the above contained a lower or upper case "s".

if (def.Somefunction("s") == true) { } 

Also in C# do I need to check if something is true like this or could I just remove the "== true" ?

like image 725
Samantha J T Star Avatar asked Jan 16 '12 12:01

Samantha J T Star


People also ask

How do you check if a string contains a specific character in c?

The strchr function returns a pointer to the first occurrence of character c in string or a null pointer if no matching character is found.

How do I check if a string contains a specific character?

Use the String. includes() method to check if a string contains a character, e.g. if (str. includes(char)) {} . The include() method will return true if the string contains the provided character, otherwise false is returned.

How do I check if a string contains something?

The includes() method returns true if a string contains a specified string. Otherwise it returns false .

Is char A string in c?

In C programming, a string is a sequence of characters terminated with a null character \0 . For example: char c[] = "c string"; When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character \0 at the end by default.


1 Answers

You can use the extension method .Contains() from the namespace System.Linq:

using System.Linq;      ...      if (abc.ToLower().Contains('s')) { } 

And also, to check if a boolean expression is true, you don't need == true

Since the Contains method is an extension method, my solution might be confusing. Here are two versions that don't require you to add using System.Linq;:

if (abc.ToLower().IndexOf('s') != -1) { }  // or:  if (abc.IndexOf("s", StringComparison.CurrentCultureIgnoreCase) != -1) { } 

Update

If you want to, you can write your own extensions method for easier reuse:

public static class MyStringExtensions {     public static bool ContainsAnyCaseInvariant(this string haystack, char needle)     {         return haystack.IndexOf(needle, StringComparison.InvariantCultureIgnoreCase) != -1;     }          public static bool ContainsAnyCase(this string haystack, char needle)     {         return haystack.IndexOf(needle, StringComparison.CurrentCultureIgnoreCase) != -1;     } } 

Then you can call them like this:

if (def.ContainsAnyCaseInvariant('s')) { } // or if (def.ContainsAnyCase('s')) { } 

In most cases when dealing with user data, you actually want to use CurrentCultureIgnoreCase (or the ContainsAnyCase extension method), because that way you let the system handle upper/lowercase issues, which depend on the language. When dealing with computational issues, like names of HTML tags and so on, you want to use the invariant culture.

For example: In Turkish, the uppercase letter I in lowercase is ı (without a dot), and not i (with a dot).

like image 65
Anders Marzi Tornblad Avatar answered Sep 23 '22 10:09

Anders Marzi Tornblad