12,064,812 members (40,055 online)
Rate this:
Please Sign up or sign in to vote.
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.
Please help me!
Posted 5-Sep-12 22:35pm
duonglg481
Edited 5-Sep-12 23:57pm
v2
Comments
Timberbird 6-Sep-12 3:45am

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

## 2 solutions

Rate this:
Please Sign up or sign in to vote.

## Solution 1

Rate this:
Please Sign up or sign in to vote.

## 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
Comments
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
 d@nish 200 CPallini 130 CHill60 120 OriginalGriff 90 RDBurmon 50
 Dave Kreskowiak 1,857 Richard MacCutchan 1,250 d@nish 980 OriginalGriff 874 Sergey Alexandrovich Kryukov 762

Advertise | Privacy | Mobile
Web03 | 2.8.160204.4 | Last Updated 6 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100