473,322 Members | 1,523 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,322 software developers and data experts.

CSharp problem

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
3 1294
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: anand | last post by:
Hi All, Are ownerdraw buttons supported in C# ? I have an active X control which I would like to use in CSharp. Buttons have a style BS_OWNERDRAW, and to do custom drawing you have to...
3
by: C# | last post by:
Hey all, in my MSN-Like project .. i needed a trigger that should send Notifications to all online users when a contact in their lists is online for example.. Server is in C# .. The problem is...
7
by: Peter Smirnov | last post by:
Sorry for this newbie question but as far as I heard one need at least VisualStudio to develop CSharp applications. Is this correct? Are there otherwise some command line tools like javac.exe and...
0
by: Elhanan Maayan | last post by:
hi.. so i'm slowly moving the transition to csharp and once again i'm faced with the problem for flipped hebew letters etc... but now the problem may not be so hard to solve as i have seen...
2
by: news.microsoft.com | last post by:
Hi: I work in Csharp's parser files by LEX/YACC.Now I have only CSharp-lex.l and CSharp.y file,but they not have CSharp'comment Parse. this is a part of CSharp-lex.l. ........................
10
by: Brian Parker | last post by:
I inherited a C++ DLL that I need to remotely call multiple times asynchronously. What I have developed is: CSharp web application that makes asynchronous calls to a CSharp Web Service. The...
1
by: Dennis Kuntzemann | last post by:
Hello, I am trying to get access to an MySql-Server over CSharp. As I am not sure whether it is an csharp or sql driver problem, i am posting it here. I got the following error: PM...
2
by: Karl | last post by:
Hi all, I'm reasonably new to csharp so you have to forgive me asking what may be a stupid question... As I said, I'm new to CSharp but in VB I was able to click anywhere on a DataGridView...
1
by: kalaivanan | last post by:
hi, how should i compile csharp programs in the cmd command prompt without being installing visual studio .net. I have installed framework 1.1 and sdk. also i have executed the vsvars32 btach...
3
by: =?Utf-8?B?R3JlZw==?= | last post by:
I''ve been working with a CSharp web-site (and am new to it). I have a javascript function that accepts variables from my CSharp code. It all works fine, with the exception of one variable type. I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.