L
L
LebedevStr2020-04-26 16:03:52
C++ / C#
LebedevStr, 2020-04-26 16:03:52

How to count the number of occurrences in a string (C#)?

Hello. There is text in string.
How to count the number of text values ​​(repetitions) in it? Number of words hi = 2; Thank you!

string value = "привет привет hi привет hi";


Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twobomb, 2020-04-26
@LebedevStr

string value = "привет привет hi привет hi";
var counts = value.Split(' ').Distinct().Select(s => s+" = "+ value.Split(' ').Count(z => z == s));
foreach (var item in counts)
     Console.WriteLine(item);


hi = 3
hi = 2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question