0

I have some special chars declared that way.

private static readonly char[] Chars = "…".ToCharArray();

Also, I can check if that chars are present in given string like that way.

private static bool ContainsSpecialChars(string text)
    {
        return text.IndexOfAny(Chars) >= 0;
    }

But I am stuck in counting how many occurencies are in the given string.

NOTE: I have array of chars

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
hoozr
  • 403
  • 4
  • 15
  • 1
    change text to char array, use `Count` with predicate which checks if `Chars` contains given char `text.ToCharArray().Count(c => Chars.Contains(c))` – Selvin Mar 02 '20 at 12:30
  • 1
    hmm I don't think if its duplicate of "How would you count occurrences of a string (actually a char) within a string?" – Selvin Mar 02 '20 at 12:40

2 Answers2

2

Try using Linq. For arbitrary special chars

 private static readonly HashSet<char> SpecialChars = new HashSet<char>() {
   //TODO: put all special chars here
   '.', '#', '?',
 };

 ...

 private static bool ContainsSpecialChars(string text) => 
   text != null && text.Any(c => SpecialChars.Contains(c));

 private static int CountSpecialChars(string text) => 
   text?.Count(c => SpecialChars.Contains(c)) ?? 0;

If special char actually means some kind(s) of Unicode Category (say, punctuation) try using char static methods:

 private static bool ContainsSpecialChars(string text) => 
   text != null && text.Any(c => char.IsPunctuation(c));

 private static int CountSpecialChars(string text) => 
   text?.Count(c => char.IsPunctuation(c)) ?? 0;
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Thank you, it works perfectly, is it possible to add **out** parameter to the Count method? – hoozr Mar 02 '20 at 12:41
  • @hoozr: technically, yes, but why should we add `out` parameter into `Count`? What should it return? – Dmitry Bychenko Mar 02 '20 at 12:43
  • With out I can use this: `if(CountSpecialChars(string, out int num) > 0) count += num` – hoozr Mar 02 '20 at 12:45
  • 1
    @hoozr: in the current implementation we have `0` if `string` doesn't have special chars; that's why we don't want `num` and `if` at all: `count += CountSpecialChars(string);`. If `string` *doesn't have* special chars we add `0` to `count` which doesn't change anything – Dmitry Bychenko Mar 02 '20 at 12:47
  • perfect, thanks! Idk, I can't tag your username, its like disapearing when I post comment. – hoozr Mar 02 '20 at 12:48
  • I think, I was need `char.IsHighSurrogate` static method – hoozr Mar 02 '20 at 13:21
  • @hoozr: When working with *surrogate* symbols, you may want *normalize* the string, e.g. `text.Normalize(NormalizationForm.FormD).Count(...)...`, see https://learn.microsoft.com/ru-ru/dotnet/api/system.text.normalizationform?view=netframework-4.8 for details – Dmitry Bychenko Mar 02 '20 at 13:26
  • Yes, I am already using normalization with `StringInfo` member. `var info = new StringInfo(tmp.Normalize(NormalizationForm.FormC));` – hoozr Mar 02 '20 at 13:45
1

You can try

Chars.ToList().ForEach(x =>
{
    if (toCheck.Contains(x))
       charOccur.Add(x, toCheck.ToList<char>().FindAll(word => word == x).Count);
});

as per @Selvins Comment you can also change above code to

Chars.ToList().ForEach(x =>
{
   if (toCheck.Contains(x))
       charOccur.Add(x, toCheck.ToCharArray().Count(c => Chars.Contains(c)));
});
Lucifer
  • 1,594
  • 2
  • 18
  • 32