Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: format strings
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 at 6-Sep-12 3:45am
   
Which language is that? If it's C#, try string.Split()
Rate this: bad
good
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
  Permalink  
v2
Comments
duonglg at 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 at 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 at 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 at 6-Sep-12 5:59am
   
You solve my problem. Thank Mario Majčica very much. I'm try.
Mario Majčica at 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)

  Print Answers RSS
0 OriginalGriff 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 6 Sep 2012
Copyright © CodeProject, 1999-2014
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