By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,896 Members | 2,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,896 IT Pros & Developers. It's quick & easy.

CSharp problem

P: n/a
i have an application that will take user input from a text box and
write it
to an access database. i need to make sure that if they ever enter a
single
line of text that it has at least 1 space for every 40 characters.

so before i write the info to the database i have to make sure there is
no
lines of text that are longer than 40 characters without a space, and
if
there are insert a space at the 41st character. is that as hard as it
sounds?
hk**********@hotmail.com

Sep 30 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hello hk**********@hotmail.com,

use Regexp class or manually check the TextBox.TextLength property
longer than 40 characters without a space, and
if
there are insert a space at the 41st character. is that as hard as it
sounds?
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Sep 30 '06 #2

P: n/a

hk**********@hotmail.com wrote:
i have an application that will take user input from a text box and
write it
to an access database. i need to make sure that if they ever enter a
single
line of text that it has at least 1 space for every 40 characters.

so before i write the info to the database i have to make sure there is
no
lines of text that are longer than 40 characters without a space, and
if
there are insert a space at the 41st character. is that as hard as it
sounds?
I'm assuming that you want a space every 40 characters on a line. That
is, that you want a line of 90 non-blank characters to have spaces
inserted at 40 and at 80, not just at 40.

string rawText;
StringBuilder sb = new StringBuilder();
int i = 0;
int charsWithNoSpace = 0;
while (i < rawText.Length)
{
if (charsWithNoSpace == 41)
{
sb.Append(' ');
charsWithNoSpace = 0;
}
if (rawText[i] == ' ' || rawText[i] == '\n')
{
charsWithNoSpace = 0;
}
sb.Append(rawText[i]);
charsWithNoSpace = 0;
i += 1;
}

Possible refinements: if you want _any_ whitespace character to count,
not just space and newline, you could use Char.IsWhitespace(rawText[i])
instead of the explicit test for blank and newline.

Sep 30 '06 #3

P: n/a
The following function will do what I believe you want. Note that I used a
parameter to specify the maximum number of characters before a space, just
in case you decide to change it from 40.

public string EnsureSpaces(string target, int maxChars)
{
StringBuilder result = new StringBuilder();
int charCount = 0;
for (int i = 0; i < target.Length; i++)
{
if (target[i] != ' ') charCount++;
else charCount = 0;
if (charCount >= maxChars) result.Append(' ');
result.Append(target[i]);
}
return result.ToString();
}

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

<hk**********@hotmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
>i have an application that will take user input from a text box and
write it
to an access database. i need to make sure that if they ever enter a
single
line of text that it has at least 1 space for every 40 characters.

so before i write the info to the database i have to make sure there is
no
lines of text that are longer than 40 characters without a space, and
if
there are insert a space at the 41st character. is that as hard as it
sounds?
hk**********@hotmail.com

Oct 1 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.