12,397,610 members (65,231 online)
Rate this:
See more:
Hello everybody!
In project C#, I have a problem, please everybody help me!
I have a string that i will enter to textbox, example:
"
If I had to live my life without you near me
The days would all be empty
The nights would seem so long
With you I see forever oh so clearly
I might have been in love before
But it never felt this strong
"
dont have quote. When i click button, i need a following results:

1. If I had to live my life without you near me
2. The days would all be empty
3. The nights would seem so long
4. With you I see forever oh so clearly
5. I might have been in love before
6. But it never felt this strong
in below string, when i meet enter character, it will do substring, but it only cut one time.
Posted 5-Sep-12 21:35pm
duonglg481
Updated 5-Sep-12 22:57pm
v2
Timberbird 6-Sep-12 3:45am

Which language is that? If it's C#, try string.Split()

Rate this:

Rate this:

## Solution 2

This should do:

```string a = @"If I had to live my life without you near me
The days would all be empty
The nights would seem so long
With you I see forever oh so clearly
I might have been in love before
But it never felt this strong";

string[] lines = a.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

StringBuilder s = new StringBuilder();

for (int i = 0; i < lines.Length; i++)
{
s.AppendFormat("{0}. {1}{2}", i + 1, lines[i], Environment.NewLine);
}

string newString = s.ToString();```

Cheers
v2
duonglg 6-Sep-12 5:42am

Thank Mario Majčica, this is the solution for me.
In your answer, i think have small problem, when Im input multiple space before strings.
Ex: "If I had to live my life without you near me
' 'The days would all be empty"
Space is the user enter space
Result:
1. If I had to live my life without you near me
2. ' 'The days would all be empty"
result request:
1. If I had to live my life without you near me
2. The days would all be empty
Thank you very much.
Mario Majčica 6-Sep-12 5:47am

If you are trying to remove the unused spaces, use the Trim function:
s.AppendFormat("{0}. {1}{2}", i + 1, lines[i].Trim(), Environment.NewLine);
You can also use TrimStart or TrimEnd in order to remove only initial or endin spaces.
Mario Majčica 6-Sep-12 5:51am

Also my example contains spaces because of formatting. If you get out of the declared string all the spaces, they will not be visible in the example. Try it by setting the text in the multiline textbox and then process it. It should work. Inform yourself about '@' string literal.

Cheers
duonglg 6-Sep-12 5:59am

You solve my problem. Thank Mario Majčica very much. I'm try.
Mario Majčica 6-Sep-12 6:02am

Glad to here that. Vote up the answer if it was helpful!

Cheers

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 290 Richard MacCutchan 225 Richard Deeming 214 Karthik Bangalore 190 F-ES Sitecore 100
 OriginalGriff 6,368 Karthik Bangalore 2,572 ppolymorphe 2,455 F-ES Sitecore 1,977 Richard MacCutchan 1,952

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 6 Sep 2012