Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
4.00/5 (5 votes)
See more:
hello..

i have problem with replace character.

if i have one textbox, and i write into textbox with many new line.

and the results I wanted a lot of new lines replaced with one or two new lines such as comments in the facebook.

please your assistance and your opinions.

Thank you
Ade
Posted
Updated 14-Jun-11 18:02pm
v3
Comments
Sergey Alexandrovich Kryukov 14-Jun-11 23:53pm    
Remove ASP.NET tag, it is irrelevant.
--SA
Sergey Alexandrovich Kryukov 14-Jun-11 23:55pm    
How can this be a problem? What did you try?
--SA
Ade Ruyani 15-Jun-11 0:48am    
i'l try this code from Prerak Patel :
litText.Text = System.Text.RegularExpressions.Regex.Replace(Text1.Text, "[\\r\\n]+", "<br /><br />", System.Text.RegularExpressions.RegexOptions.Multiline);

this works if i press a lot of button enter,but this isn't works if i press button enter once then displays new line twice. i want if i press once or two button enter, fixed display once or two new line. except three times or more than two.

Using array
C#
String.Join(System.Environment.NewLine, text.Split(System.Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))


or regular expression
C#
System.Text.RegularExpressions.Regex.Replace(System.text, "[\\r\\n]+", System.Environment.NewLine, System.Text.RegularExpressions.RegexOptions.Multiline)


or loop at last
C#
while (txt.Contains(System.Environment.NewLine + System.Environment.NewLine)) {
	txt = txt.Replace(System.Environment.NewLine + System.Environment.NewLine, System.Environment.NewLine);
}
 
Share this answer
 
v2
Comments
Ade Ruyani 15-Jun-11 1:02am    
i'l try your code :

litText.Text = System.Text.RegularExpressions.Regex.Replace(Text1.Text, "[\\r\\n]+", "<br /><br />", System.Text.RegularExpressions.RegexOptions.Multiline); this works if i press a lot of button enter,but this isn't works if i press button enter once then displays new line twice. i want if i press once or two button enter, fixed display once or two new line. except three times or more than two.

please your assistance and solutions.

Thansk before.
Take help of Regex. Below code worked for me:
textBox1.Text = Regex.Replace(textBox1.Text, "[\r\n]+", "\r\n");


Hope this works for you as well.
 
Share this answer
 

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