strings

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a following strings
strA = " 1 my first work out".
strB = "2 my second work out"
strC = " My last work out"

My strA and strB first char is int and strB first char is " ". I want to
check if in a string first char returns int (1, 2 or ---) then do this
otherwise do that.

How can I know that the first char of string is returing an int or not.

Thanks
 
Actually, the first character of strA and strC is a space, the first
character of strB is the character '2', not the actual integer to (it is the
character representation of 2 from the perspective of the framework).

Basically, to do what you want, I would split the string into an array
of strings using space as the delimiter (I'm assuming that's what you are
using to delimit words) and then seeing if the first element in the array is
an integer representation:

public static bool StartsWithInt(string value, out int intValue)
{
// Split the string.
string[] parts = value.Split(new char[]{ ' ' });

// If there are elements, then continue, otherwise, return
// false.
if (parts.Length = 0)
{
// No integer here.
return false;
}

// Try and parse the first element.
return Int32.TryParse(parts[0], out intValue);
}

Mind you, there is no check to see if the value parameter is null, which
you might want to do.
 
[...]
My strA and strB first char is int and strB first char is " ". I want to
check if in a string first char returns int (1, 2 or ---) then do this
otherwise do that.

How can I know that the first char of string is returing an int or not.

The "first char" of a string is always a char. It is never an int.

However, you seem to really just want to find out whether that character
can be _converted_ to an int. IMHO, the easiest is to try to parse it as
an int:

int num;

if (int.TryParse(str.Substring(0, 1), out num))
{
// first character can be parsed as an int, and the value is
// in the variable num
}

Pete
 
Hi,

I have a following strings
strA = " 1 my first work out".
strB = "2 my second work out"
strC = " My last work out"

My strA and strB first char is int and strB first char is " ". I wantto
check if in a string first char returns int (1, 2 or ---) then do this
otherwise do that.

How can I know that the first char of string is returing an int or not..

Thanks

Or, if you can ignore whitespace characters at the beginning, do

if (Char.IsDigit(strA.TrimStart()[0]))
{
// number
}
 
[...]
// Try and parse the first element.
return Int32.TryParse(parts[0], out intValue);
}

Mind you, there is no check to see if the value parameter is null,
which
you might want to do.

It also does not limit the attempt at conversion to a single character.

Based on a literal reading of the OP, only the first character should be
used to attempt to convert to an int.

It is often the case that someone writes a question that is actually not
exactly what they really want to do, so your solution may in fact be the
correct one. But as stated, it's not doing what the OP asked for.

Pete
 
It is this that makes me think that the first character might not be
exactly what they are looking for:

I want to check if in a string first char returns int (1, 2 or ---)

"---" is ambiguous, and more than one character. An ellipsis would have
been better, but saying 1-9 would have been best.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)



Peter Duniho said:
[...]
// Try and parse the first element.
return Int32.TryParse(parts[0], out intValue);
}

Mind you, there is no check to see if the value parameter is null,
which
you might want to do.

It also does not limit the attempt at conversion to a single character.

Based on a literal reading of the OP, only the first character should be
used to attempt to convert to an int.

It is often the case that someone writes a question that is actually not
exactly what they really want to do, so your solution may in fact be the
correct one. But as stated, it's not doing what the OP asked for.

Pete
 
It is this that makes me think that the first character might not be
exactly what they are looking for:

I want to check if in a string first char returns int (1, 2 or ---)

"---" is ambiguous, and more than one character. An ellipsis would
have been better, but saying 1-9 would have been best.

Yup...a valid interpretation IMHO. Sadly, we are often left trying to
extract some consistent interpretation from an internally-inconsistent
post. :) I just wanted to clarify that there are some differences from
the explicit statements (as opposed to my own suggestion which has
differences from the implicit statements).

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top