ron cohen
ron cohen

Reputation: 21

Length of a string in c# WinForm

I have a text box in my form and I want to check if the first char is equal to something,
basically I want it to do this :

if(Textbox1.text.length(0) == "a")
{
  do stuff
}

Upvotes: 1

Views: 772

Answers (5)

KARAN
KARAN

Reputation: 1041

if(textBox1.Text.Substring(0,1)=="a")

Upvotes: 0

Neel
Neel

Reputation: 11721

try smething like this:

Textbox1.Text.StartsWith("a") ? do something : do something else;

and if it is not just about first character then try something like:

Textbox1.Text.Contains("a") ? do something : do something else;

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

Eh, are you looking for

  if (Textbox1.Text.StartsWith("a")) {
    // do stuff
    ...
  }

Upvotes: 3

user5308950
user5308950

Reputation:

you can do this by two ways... one i mentioned in your question's comment
second is

if(myTextBox.Text[0] == 'B')
{
//do studd
}

Upvotes: 0

Neal
Neal

Reputation: 811

.length only returns an integer of length of the string. This will compare the first char with 'a'.

if(Textbox1.text[0] == 'a')

Upvotes: 3

Related Questions