I have text in a text box that I want to format to a fixed-width column of between 30 and 60 characters. The user will be allowed to specify how wide each line should be (i.e. the width of the column) but it will default to 50 characters.
I have done some research however, I couldn't find anything of what my goal is.
I have tried String.Format
however, it doesn't do anything
string test = ""
if(txtBoxContent.Text == string.Empty)
{
MessageBox.Show("There are no '>' symbols to remove. Please paste the infected text first");
}
else if (!txtBoxContent.Text.Contains(">"))
{
MessageBox.Show("There are no '>' symbol(s) in the text OR are already removed");
}
else
{
contentWithoutCharacters = txtBoxContent.Text.Replace(">", "");
txtBoxContent.Text = contentWithoutCharacters;
//MessageBox.Show("Removed Successfully");
test = string.Format("{0,50}", contentWithoutCharacters); // this
}
}
I am assume I am taking the wrong approach or not using the string.format
correctly. Any guidance will be appreciated
Update: I think i didn't explain my self clearly.
For example: the below text is a original text
I have text in a text box that I want to format to a fixed-width column of between 30 and 60 characters. The user will be allowed to specify how wide each line should be (i.e. the width of the column) but it will default to 50 characters.
when i press a button, i want the above text to display 50 characters on each line
I have text in a text box that I want to format to a fixed-width new line column of between 30 and 60 characters. The user will be allowed to specify how new line wide each line should be (i.e. the width of the column) but it will default to 50 characters.
Hope it makes more sense now
If you're determined to break your string up into n-width pieces, here's an approach. However, you'll soon see that the string gets broken up in ways you may not want...which is why you're getting comments wondering whay you want to do this.
var originalText = "Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! ";
txtBoxContent.Clear();
txtBoxContent.WordWrap = false; //--> to make sure nothing spills to the next line
var part = string.Empty;
var index = 0; //--> define this outside the loop, so you can use it to pick up any remainder
var width = 30; //--> ...or whatever width you need
for ( index = 0; index < ( originalText.Length - width ); index += width )
{
part = originalText.Substring( index, width );
//--> do something useful with the fixed-length part...
txtBoxContent.Append( part + Environment.NewLine );
}
//--> deal with the remainder, if any...
if ( index < originalText.Length )
{
//--> the last piece...it will be less than width wide...
part = originalText.Substring( index, originalText.Length - index );
txtBoxContent.Append( part );
}
...and here's what you get:
Now is the time; yada yada; th
ing and the thing! Now is the
time; yada yada; thing and the
thing! Now is the time; yada
yada; thing and the thing! Now
is the time; yada yada; thing
and the thing! Now is the tim
e; yada yada; thing and the th
ing! Now is the time; yada yad
a; thing and the thing! Now is
the time; yada yada; thing an
d the thing!