Click here to Skip to main content
15,884,093 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to delete the last character in line 1 and line 2 and .....(every line) from a Text Box?
for exam i want delete (e - in line 1 & m - in line 2 & 4 - in line 3 , and ....) in this text in text box:
hi every one (delete (e))
i am 180 cm (delete (m))
i am 244 (delete (4))


http://i.stack.imgur.com/9z6JQ.png
Posted
Updated 13-Mar-15 5:01am
v3
Comments
Sergey Alexandrovich Kryukov 13-Mar-15 10:51am    
Why? Would it be a bit better to avoid adding unwanted characters in first place?
—SA

First of all, System.String it immutable. You never add, remove or modify anything it its instance. All string methods create a brand new instance of string, which may cause performance problems when used unreasonably. See also my comment to the question.

It's not clear what prevents your from reading original MSDN documentation:
https://msdn.microsoft.com/en-us/library/system.string%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.text.stringbuilder%28v=vs.110%29.aspx[^].

Any questions?

—SA
 
Share this answer
 
As said in Solution1 it is not very efficient to remove characters from a string as a new string is always created as a result.
However, if you want to remove the last character of each line, you can for example use Regular Expressions.

Add this line of code into an existing method, e.g. textBox1_TextChanged or a button_Click event.
C#
textBox1.Text = Regex.Replace(textBox1.Text, @"(?<char>\S)(?<eol>\s*$)", new MatchEvaluator(ReplaceCharWithWhiteSpace), RegexOptions.Multiline);

You might think that this line would be sufficient
textBox1.Text = Regex.Replace(textBox1.Text, @"\S$)", "", RegexOptions.Multiline);
but unfortunately the line end characters, \r and \n, are not stripped from the string, hence the need for \s* to include these characters. You also want to return the line ends from the replacement method, therefor I have used named groups to make it more easy.
Then add this static method to your class
C#
private static string ReplaceCharWithWhiteSpace(Match m)
{
    return m.Groups["eol"].Value; // Return the line ends, but not the last character
}

This is definitely not the most efficient solution, but it works.

You can add the following fields to your class to make the execution a bit more efficient:
C#
private static Regex lastCharExpression = new Regex(@"(?<char>\S)(?<eol>\s*$)", RegexOptions.Multiline);
private MatchEvaluator eval = new MatchEvaluator(new MatchEvaluator(ReplaceCharWithWhiteSpace));

then change to
C#
textBox1.Text = lastCharExpression.Replace(textBox1.Text, eval);


If you want to replace only characters you can replace
\S with \w, which is equal to [a-zA-Z0-9_]
 
Share this answer
 
v2

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