check word length

  • Thread starter Thread starter Aaron
  • Start date Start date
A

Aaron

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,

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
 
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!-
 
Back
Top