13,252,362 members (61,474 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 22:35pm
duonglg481
Updated 5-Sep-12 23: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

Cheers

Top Experts
Last 24hrsThis month
 OriginalGriff 300 Karthik Bangalore 114 ppolymorphe 100 Jochen Arndt 75 Richard Deeming 50
 OriginalGriff 3,824 Karthik Bangalore 2,151 ppolymorphe 1,529 Dave Kreskowiak 1,301 CPallini 1,240