I have a button that when clicked pushes(adds) text into a label in C# win form. Now I need to identify the certain number of times a character in the text in the label is repeated, for example how many times the character "a" is repeated. And after identifying this I plan on having a different action perform when another button is pressed.
Below I will include some of the code I have wrote to give an idea on how I want my app to work.
//button that adds text to the label
private void btnLeave_Click(object sender, EventArgs e)
{
if (txtMembertype.Text == "Level A" && borrow_count > 3)
{
MessageBox.Show("You can only borrow three books at a time", "SyscoTech", MessageBoxButtons.OK, MessageBoxIcon.Warning);
lblBorrowings.Text = "Books borrowed - ";
}
else if (txtMembertype.Text == "Level B" && borrow_count > 2)
{
MessageBox.Show("You can only borrow two books at a time", "SyscoTech", MessageBoxButtons.OK, MessageBoxIcon.Warning);
lblBorrowings.Text = "Books borrowed - ";
}
//another button which does the same thing as above
private void btnBorrowedBk_Click(object sender, EventArgs e)
{
lblBorrowings.Text = lblBorrowings.Text "\n\t " cmbBooks.Text "";
cmbBooks.Text = "";
borrow_count = borrow_count 1;
}
//I plan on having an if-else like below
if (txtMembertype.Text == "Level A" && label.text == "a" > 3 [if a is repeated 3 times])
{
// do this
}
else if (txtMembertype.Text == "Level B" && label.text == "a" > 2 [if a is repeated two times])
{
// do this
}
Any help is appreciated. Thanks in advance.
CodePudding user response:
With linq you can use the Count extension method:
label.text.Count(c=> c == 'a') > 3
Don't forget to add using System.Linq;