Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 5-Sep-12 22:57pm
v2
Comments
Timberbird 6-Sep-12 3:45am    
Which language is that? If it's C#, try string.Split()

This should do:

C#
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
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900