I want to split a line for a period and put this sentence on a separate line

I want to take user input and then split the line after each period. Then I want to put each sentence on its own line in a text box in Visual Studio.

I know how to store sentences in an array, and put each sentence on every other line, but when I run, the result for each letter I type is system.string[]

.

 string input = TextEditor.Text;
 string[] tokens = input.Split('.');
 Output.AppendText(Environment.NewLine);
 Output.Text += tokens;

      

I'm sure it comes from output.text = token;

, but I don't know what to replace. Any idea?

+3


source to share


1 answer


You should just use string.Join

string input = TextEditor.Text;
string[] tokens = input.Split('.');
Output.Text = string.Join(Environment.NewLine, tokens);

      

You can even achieve your goal within one line

Output.Text = string.Join(Environment.NewLine, TextEditor.Text.Split('.'));

      



or use string.Replace (not sure if it's faster)

Output.Text = TextEditor.Text.Replace(".", Environment.NewLine));

      

Your current code is crashing because you are adding an array, not individual string elements of the array. Instead of string.Join joins the individual elements of the array with a delimiter specified as the first parameter.

Remember, however, that in order to display multiple lines of text, you need to have a text box with sufficient height space and a Multiline attribute of True.

+2


source







All Articles