strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?c2VlbWE=?=

    strings

    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


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: strings

    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(s tring 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.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "seema" <seema@discussi ons.microsoft.c omwrote in message
    news:6026AB01-CE18-4487-BFD5-984F5B6FF122@mi crosoft.com...
    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
    >
    >

    Comment

    • Peter Duniho

      #3
      Re: strings

      On Mon, 23 Jul 2007 12:54:04 -0700, seema
      <seema@discussi ons.microsoft.c omwrote:
      [...]
      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(s tr.Substring(0, 1), out num))
      {
      // first character can be parsed as an int, and the value is
      // in the variable num
      }

      Pete

      Comment

      • Morten Wennevik [C# MVP]

        #4
        Re: strings

        On Mon, 23 Jul 2007 21:54:04 +0200, seema <seema@discussi ons.microsoft.c omwrote:
        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(s trA.TrimStart()[0]))
        {
        // number
        }

        --
        Happy coding!
        Morten Wennevik [C# MVP]

        Comment

        • Peter Duniho

          #5
          Re: strings

          On Mon, 23 Jul 2007 13:05:10 -0700, Nicholas Paldino [.NET/C# MVP]
          <mvp@spam.guard .caspershouse.c omwrote:
          [...]
          // 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

          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: strings

            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]
            - mvp@spam.guard. caspershouse.co m



            "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
            news:op.tvxuirp x8jd0ej@petes-computer.local. ..
            On Mon, 23 Jul 2007 13:05:10 -0700, Nicholas Paldino [.NET/C# MVP]
            <mvp@spam.guard .caspershouse.c omwrote:
            >
            >[...]
            > // 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

            Comment

            • Peter Duniho

              #7
              Re: strings

              On Mon, 23 Jul 2007 17:41:30 -0700, Nicholas Paldino [.NET/C# MVP]
              <mvp@spam.guard .caspershouse.c omwrote:
              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

              Comment

              Working...