check word length

Discussion in 'ASP .Net' started by Aaron, Apr 11, 2004.

  1. Aaron

    Aaron Guest

    DOes anyone know how to check the length of words in a string?

    I want all the word in a string to be less than 100 char to prevent buffer
    overflow.


    Thanks.
    Aaron
    Aaron, Apr 11, 2004
    #1
    1. Advertising

  2. Aaron,

    You could try something like this

    string msg = " This is my message";

    public class StringUtil
    {
    public StringUtil()
    {
    }

    public bool IsValidWordLenght(string value, int maxLenght)
    {
    string[] words = value.Split(' ');
    foreach(string word in words)
    {
    if( word.Trim().Length > maxLenght)
    return false;
    }
    return true;
    }
    }

    Then call it like this (it will trim of leading and trailing whitespaces if
    there are any).

    string message ="This is a message";
    bool ret = StringUtil.IsValidWordLenght(message, 100);

    HTH,

    //Andreas

    "Aaron" <> skrev i meddelandet
    news:OPAOX$...
    > DOes anyone know how to check the length of words in a string?
    >
    > I want all the word in a string to be less than 100 char to prevent buffer
    > overflow.
    >
    >
    > Thanks.
    > Aaron
    >
    >
    >
    >
    Andreas Håkansson, Apr 11, 2004
    #2
    1. Advertising

  3. Aaron

    Tony Guest

    On Sun, 11 Apr 2004 14:57:09 -0700, "Aaron" <> wrote:

    >DOes anyone know how to check the length of words in a string?
    >
    >I want all the word in a string to be less than 100 char to prevent buffer
    >overflow.
    >
    >
    >Thanks.
    >Aaron


    I'm just begining to learn C#, so most likely this isn't what you're
    looking for but, I thought I would mention my thought :)


    string s;
    s="served";
    int i;
    i=(s.Length);
    Console.WriteLine("i is: " + i );

    you can use an if/then or a try/catch block if the length is 100 or
    more?

    -Tony!-
    Tony, Apr 12, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mitchua
    Replies:
    5
    Views:
    2,715
    Eric J. Roode
    Jul 17, 2003
  2. =?Utf-8?B?SG96aQ==?=
    Replies:
    1
    Views:
    6,937
    Ken Cox [Microsoft MVP]
    Jun 2, 2004
  3. Sam
    Replies:
    3
    Views:
    14,080
    Karl Seguin
    Feb 17, 2005
  4. Replies:
    2
    Views:
    5,911
  5. Replies:
    5
    Views:
    912
    X-Centric
    Jun 30, 2005
Loading...

Share This Page