I wanna split the content of a Label into smaller part. For example the sentence is "Hello how are you now. I'm fine thanks you". How do I split the sentence every time a full stop appear and I wanna make the second sentence place below the first sentence. Thank you.
Asked
Active
Viewed 111 times
0
-
What type of the application do you implement? WPF, WebForms, WinForms...? – alexmac Dec 11 '13 at 08:30
-
If I understand correctly, you can use `String.Split(new[] { '.', '?', '!' , StringSplitOptions.RemoveEmptyEntries)` and format your array items like `string.Format("{0} \n{1}", array[0], array[1]);` – Soner Gönül Dec 11 '13 at 08:33
2 Answers
2
To split text into senteces
string[] sentences = Regex.Split(input, @"(?<=[\.!\?])\s+");
then you can place your senteces in different boxes, or add new lines.
EDIT: in your code:
var input = label1.Content;
string[] sentences = Regex.Split(input, @"(?<=[\.!\?])\s+");
but to allow multilines you should use textblock and write:
textblock1.Content = string.Join("\n", sentences);

Community
- 1
- 1

Kamil Budziewski
- 22,699
- 14
- 85
- 105
-
thank you. But the sentence I get is read from database which is label1.content = reader["Greeting"]; How should I insert the line above into my codes? – user3016854 Dec 11 '13 at 08:36
-
@user3016854 in your code it should look like above, but to allow multilines you should use textblock – Kamil Budziewski Dec 11 '13 at 08:39
-
I have tried it but the Regex.Split(input, @"(?<=[\.!\?])\s+"); has an error says "The best overloaded method match for "System.Text.RegularTextExpression.Regex.Split(string, int)" has some invalid arguments". – user3016854 Dec 11 '13 at 08:56
-
thanks I have solved it, I just change the var data type for "input" into string and add ToString() behind the label1.Content; – user3016854 Dec 11 '13 at 09:02
0
You could also simply use string.Split
:
string[] sentences = text.Split(new[] { '.', '?', '!' }, StringSplitOptions.RemoveEmptyEntries);
string newText = string.Join(Environment.NewLine, sentences.Select(s => s.Trim()));

Tim Schmelter
- 450,073
- 74
- 686
- 939