Jump to content
Tuts 4 You

String Format In C# (more Advanced This Time!)


Recommended Posts

Is there anyone in here who could help me with this little problem here:

I have a string of 30 chars, I want to divide that string into 3 lines with 10 chars at max per line.

Only problem is; NO WORDS SHOULD BE DIVIDED INTO 2 PARTS, it must stop and choose next line

if it see that the word is too long for the line....

What I have so far:

String[] Splitter = input.Split(new char[] { ' ' });

That code will then split the string into bits and parts, now to the rebuilding part where the actual help I need is needed.... Any good and valid suggestions?

/Regards, n00b

Link to comment

Um.. I think, you need to hardcode it.

For example like this:

		private void tbxInput_TextChanged(object sender, EventArgs e)
string[] Input = tbxInput.Text.Split(' ');
int Position = 0; tbxOutput1.Text = Split2Array(Input, ref Position);
tbxOutput2.Text = Split2Array(Input, ref Position);
tbxOutput3.Text = Split2Array(Input, ref Position);
} private string Split2Array(string[] IN, ref int POS)
string Split = "";
string OUT = "";
while ((Split.Length <= 10) && (POS < IN.Length))
Split += IN[POS];
if (Split.Length > 10) break;
OUT = Split;
return OUT;

But it leaves out the spaces between the words..

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...