Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a suffix from end of string?

I want to:

  1. Check a variable and determine if the last 2 characters are "Id"
  2. If yes, remove them.

I can do it with this below, but them it will blow up if there is an "Id" substring other than the end. Is there a RemoveFromEnd() method that takes a number of characters argument?

 if (column.EndsWith("Id"))  {        //remove last 2 characters        column = column.replace("Id", "");  } 

I see this solution: which does this:

column = System.Text.RegularExpressions.Regex.Replace(column, "Id$", ""); 

but it says it's pretty slow and I am going to be running this code inside a code block that I would like to be extremely fast so I wanted to see if a faster solution is available.

like image 415
leora Avatar asked Mar 12 '11 18:03

leora


People also ask

How do you remove a suffix from a string?

There are multiple ways to remove whitespace and other characters from a string in Python. The most commonly known methods are strip() , lstrip() , and rstrip() . Since Python version 3.9, two highly anticipated methods were introduced to remove the prefix or suffix of a string: removeprefix() and removesuffix() .

How do I remove the last 3 letters from a string?

Use the String. slice() method to remove the last 3 characters from a string, e.g. const withoutLast3 = str. slice(0, -3); . The slice method will return a new string that doesn't contain the last 3 characters of the original string.

How do I remove characters from the end of a string in Python?

Use the . rstrip() method to remove whitespace and characters only from the end of a string.

How do I remove a character from a string?

Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.


2 Answers

String.Substring can do that:

column = column.Substring(0, column.Length - 2); 

You can use it to roll your own RemoveFromEnd:

public static string RemoveFromEnd(this string s, string suffix) {     if (s.EndsWith(suffix))     {         return s.Substring(0, s.Length - suffix.Length);     }     else     {         return s;     } } 
like image 78
Jon Avatar answered Sep 24 '22 10:09

Jon


An alternative to the SubString method is to use a Regex.Replace from System.Text.RegularExpressions:

using System.Text.RegularExpressions; ... column = Regex.Replace(column, @"Id$", String.Empty); 

This way enables you to avoid the test, but not sure if it is really a speed benefit :-). At least an alternative that might be useful in some cases where you need to check for more than one thing at a time.

The regex can be compiled and re-used to get some performance increase and used instead of the call to the static method and can be used like this:

// stored as a private member private static Regex _checkId = new Regex(@"Id$", RegexOptions.Compiled); ... // inside some method column = _checkId.Replace(column, String.Empty); 
like image 22
jdehaan Avatar answered Sep 24 '22 10:09

jdehaan