Im new to C# and I need to make a program that can count these characters (š, ž, č) in a textbox. I put the replace method in but it says No overload for method "Replace" takes 3 arguments. I figured out that the Replace method can't take 3 arguments. The problem is that I don't know what other code I can use. Can anybody help?
public Form1()
{
InitializeComponent();
}
private void btn1_Click(object sender, EventArgs e)
{
lblDolzinaStavka.Text = txtBesedilo.Text.Length.ToString();
int Sumniki = txtBesedilo.Text.Length - txtBesedilo.Text.Replace("š", "č", "ž").Length;
}
Replace
is used to replace one character or string within a string with another. For example, "mum".Replace("u", "o")
will return "mom"
. That's not counting occurrences of anything - it's not the method you want at all.
It sounds like you want something like:
// Replace "foo" with a more meaningful name - we don't know what the
// significance of these characters is.
int foo = txtBesedilo.Text.Count(c => c == 'š' || c == 'č' || c == 'ž');
Or:
char[] characters = { 'š', 'č', 'ž' };
int foo = txtBesedilo.Text.Count(c => characters.Contains(c));
Both of these snippets use the Enumerable.Count
extension method which counts how many items in a collection match a certain condition. In this case the "items in a collection" are "characters in txtBesedilo.Text
" and the condition is whether or not it's one of the characters you're interested in.
See more on this question at Stackoverflow