Check if a string starts with any character in the list

I want to check if a string starts with any character in the list. My current implementation in C # is as follows:

char[] columnChars = new char[] { 'A', 'B', 'C', 'D', 'E' }; private bool startWithColumn(string toCheck) { for(int i=0; i<columnChars.Length; i++) if (toCheck.StartsWith(columnChars[i]+"")) { return true; } return false; } 

I would like to know if any solution is better?

+4
source share
13 answers

Turn the check and check if the first character is in a valid set.

  char[] columnChars = new char[] { 'A', 'B', 'C', 'D', 'E' }; private bool startWithColumn(string toCheck) { return toCheck != null && toCheck.Length > 0 && columnChars.Any( c => c == toCheck[0] ); } 
+7
source

You can get the first character from a string quite easily:

 char c = toCheck[0]; 

And then check if it is in the array:

 return columnChars.Contains(c); 
+4
source

I need something similar, but for the lines:

I wanted to know if my subject line began with any of these lines:

 var qualent3s = new string[] { "D", "M", "H", "JUK"}; 

LINQ for this is simple:

 qualent3s.Any(x => subject.StartsWith(x)) 
+4
source
 return columnChars.Any(x => x == toCheck[0]); 
+3
source

If your β€œlist” of your character will definitely be char[] , I would suggest that you are best off:

 return toCheck.IndexOfAny(columnChars) == 0; 

Disclaimer: I did not compare this. But this method just sat there.

+2
source
 return Regex.IsMatch(toCheck, "^[AE]"); 

As an alternative:

 return toCheck.Length > 0 && columnChars.Contains(toCheck[0]); 
+1
source

I believe this will be faster:

 char[] columnChars = new char[] { 'A', 'B', 'C', 'D', 'E' }; private bool startWithColumn(string toCheck) { for(int i=0; i<columnChars.Length; i++) if (toCheck.Length > 0 && toCheck[0] == columnChars[i])) { return true; } return false; } 
0
source
 private bool startWithColumn(string toCheck) { return (columnChars.IndexOf(toCheck[0]) >=0); } 
0
source

The obvious way would be to linearly search the array for the first character of the string:

 private bool startWithColumn(string toCheck) { return !string.IsNullOrEmpty(toCheck) && Array.IndexOf(columnChars, toCheck[0]) != -1; } 

If you're looking for performance, consider using a HashSet<char> or similar instead of an array, which should give you a constant search. This is probably worth it if the array was much larger; you have to measure one way or another.

0
source

Here is what I came up with:

  readonly char[] columnChars = new char[] { 'A', 'B', 'C', 'D', 'E' }; private bool startWithColumn(string toCheck) { return columnChars.Contains(toCheck.Substring(0, 1).ToCharArray()[0]); } 

Edit

Unable to make fewer conversions:

  readonly char[] columnChars = new char[] { 'A', 'B', 'C', 'D', 'E' }; private bool startWithColumn(string toCheck) { return columnChars.Contains(toCheck[0]); } 
0
source

You can do this using Contains and ElementAt:

 char[] columnChars = new char[] { 'A', 'B', 'C', 'D', 'E' }; String testString = "This is test String"; var exists = columnChars.Contains(testString.ElementAt(0)); 
0
source

I like my linq like this:

  string str = "A quick brown fox"; char[] chars = { 'A', 'B', 'C', 'D', 'E', 'F' }; var query = from c in str.Substring(0, 1) join c1 in chars on c equals c1 select c; 

This will give you all the characters in the list corresponding to the first character of the string. A slight modification, and you can even get the character index in the list of characters you are looking for, in this case index 0.

here is this code:

  string str = "A quick brown fox"; char[] chars = { 'Z', 'X', 'A', 'B', 'C', 'D', 'E', 'F' }; var query = from c in str.Substring(0, 1) join c1 in chars on c equals c1 select new { Character = c, Index = chars.ToList().IndexOf(c) }; var found = query.ToArray(); 
0
source

In such cases, I use the extension method as follows:

 public static bool StartsWithAny(this string Text, IEnumerable<string> Needles) { return Needles.Any(x => Text.StartsWith(x)); } 
0
source

Source: https://habr.com/ru/post/1335326/


All Articles