473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

check word length

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


Jul 21 '05 #1
4 2007
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" <ku*****@yahoo.com> skrev i meddelandet
news:OP**************@TK2MSFTNGP12.phx.gbl...
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

Jul 21 '05 #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" <ku*****@yahoo.com> skrev i meddelandet
news:OP**************@TK2MSFTNGP12.phx.gbl...
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

Jul 21 '05 #3
On Sun, 11 Apr 2004 14:57:09 -0700, "Aaron" <ku*****@yahoo.com> 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!-

Jul 21 '05 #4
On Sun, 11 Apr 2004 14:57:09 -0700, "Aaron" <ku*****@yahoo.com> 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!-

Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Eric A. Johnson | last post by:
Hi, Thanks for reading this. How do I list all possible permutations of a word? I've figured out how to get all the characters in alphabetical order. I then want to display every possible...
4
by: Paul | last post by:
Hi, (First apologies if this is not the most relevant place to post this but I wasn't sure of where was and I am writing my app in VB.) I'm attempting to parse a binary file for which I have...
15
by: tabonni | last post by:
I want to check each button groups and save the checked value into a 2 dimensional array. But, it doesn't work. Could anyone tell me what's wrong with my code. My code is as follow: <html>...
5
by: Krechting | last post by:
Hi ALl, I have a code that checks if the documents in a hyperlink field are still where they should be. I use fileexist(). First I want to filter out all the hyperlink fields that are empty. I...
2
by: CR | last post by:
having a problem with the final strcpy of filename from *argv into a structure containing an array with 20 elements called fname It seems that strcpy & strncpy aren't stopping after null is found...
10
by: Douglas G | last post by:
I've tried various ideas on this problem, but I don't see word wrapping. Can you point out what is wrong? It's a K&R exercise, and I'm still new to programming. Other pointers would be helpful...
4
by: s.subbarayan | last post by:
Dear all, I would like to know the easiest efficient way to set or inject a particular value in the given word or byte?The problem is: I have to implement a function which will set a value from...
2
by: Aaron | last post by:
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
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.