0

A simple code snippet is as follows:

public static void Main()
{
    string str = "IsRecorded<code>0</code>";
    str = str.TrimEnd("<code>0</code>".ToCharArray());
    Console.WriteLine(str);
}

The output string that I get is IsRecor. Why does the TrimEnd function strips of ded from the string when it is supposed to strip only <code>0</code>. Also if I reduce the str to IsRec then it gives IsR as output. Why is this happening?

Navjot Singh
  • 678
  • 7
  • 18
  • Your .ToCharArray specify that you want to delete all content after every char in your string. So d is in your String, so it cut after it. – KiwiJaune Apr 23 '19 at 12:45
  • If you want the moral equivalent of "cut off this string, but only if it occurs at the end", you could use `Regex.Replace("IsRecorded0", Regex.Escape("0") + "$", "")`. (There are more efficient alternatives, but something something premature optimization.) – Jeroen Mostert Apr 23 '19 at 12:48
  • Possible duplicate of [Strange TrimEnd behaviour with \ char](https://stackoverflow.com/questions/5700903/strange-trimend-behaviour-with-char) – Igor Apr 23 '19 at 12:48

4 Answers4

6

The parameter for TrimEnd specifies the set of characters to be trimmed. It's not meant to be a suffix to be trimmed.

So you're saying you want to trim any character in the set { '<', 'c', 'o', 'd', 'e', '>', '0', '/' }. The letters "ded" are all in that set, so they're being trimmed.

If you want to remove a suffix, don't use TrimEnd. Use something like this:

public static string RemoveSuffix(string input, string suffix) =>
    input.EndsWith(suffix, StringComparison.Ordinal)
        ? input.Substring(0, input.Length - suffix.Length)
        : input;

(The string comparison part is important to avoid "interesting" culture-specific effects in some cases. It basically does the simplest match possible.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

You can use .LastIndexOf() & Remove() to remove string which is at the end

string str = "IsRecorded<code>0</code>";
str = str.Remove(str.LastIndexOf("<code>0</code>"));

.LastIndexOf(string param) : This will find index of last occurrence of specified string.

.Remove(int startIndex) : Remove string from given index

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
0

Try this instead :

        string str = "IsRecorded<code>0</code>";
        str = str.Substring(0, str.IndexOf("<code>0</code>"));
        Console.WriteLine(str);
KiwiJaune
  • 530
  • 2
  • 16
  • Note that if "0" occurs more than once in the string, that will remove everything before the *first* of them. That may or may not be what's desired... – Jon Skeet Apr 23 '19 at 12:47
  • Sure, it's not a final solution, just a simple way to do what he thinked TrimEnd does. Your solution is more flexible. – KiwiJaune Apr 23 '19 at 12:50
0

Another solution could be:

string str = "IsRecorded<code>0</code>";
str = str.Replace("<code>0</code>", "");
Console.WriteLine(str);

PS: I know that wont trim just the end part, but if it's not a problem then it could be used