从字符串中删除最后一个字符

时间:2022-09-13 08:30:45

I have a string say

我有一个字串说。

"Hello! world!" 

I want to do a trim or a remove to take out the ! off world but not off Hello.

我想修剪一下,或者把它移开。离开世界,但不是离开你好。

11 个解决方案

#1


213  

"Hello! world!".TrimEnd('!');

read more

阅读更多

EDIT:

编辑:

What I've noticed in this type of questions that quite everyone suggest to remove the last char of given string. But this does not fulfill the definition of Trim method.

我在这类问题中注意到,每个人都建议删除给定字符串的最后一个字符。但这并不能满足Trim方法的定义。

Trim - Removes all occurrences of white space characters from the beginning and end of this instance.

从这个实例的开头和结尾删除所有出现的空白字符。

MSDN-Trim

MSDN-Trim

Under this definition removing only last character from string is bad solution.

在这个定义下,只从字符串中删除最后一个字符是不好的解决方案。

So if we want to "Trim last character from string" we should do something like this

如果我们想要“从字符串中删除最后一个字符”,我们应该这样做

Example as extension method:

示例扩展方法:

public static class MyExtensions
{
  public static string TrimLastCharacter(this String str)
  {
     if(String.IsNullOrEmpty(str)){
        return str;
     } else {
        return str.TrimEnd(str[str.Length - 1]);
     }
  }
}

Note if you want to remove all characters of the same value i.e(!!!!)the method above removes all existences of '!' from the end of the string, but if you want to remove only the last character you should use this :

注意,如果您想删除所有相同值的字符,例如(!!!! !),上面的方法将删除'!“如果你想只删除最后一个字符,你应该这样做:

else { return str.Remove(str.Length - 1); }

#2


52  

String withoutLast = yourString.Substring(0,(yourString.Length - 1));

#3


7  

if (yourString.Length > 1)
    withoutLast = yourString.Substring(0, yourString.Length - 1);

or

if (yourString.Length > 1)
    withoutLast = yourString.TrimEnd().Substring(0, yourString.Length - 1);

...in case you want to remove a non-whitespace character from the end.

…如果您想从最后删除一个非空白字符。

#4


5  

string helloOriginal = "Hello! World!";
string newString = helloOriginal.Substring(0,helloOriginal.LastIndexOf('!'));

#5


5  

        string s1 = "Hello! world!";
        string s2 = s1.Trim('!');

#6


4  

The another example of trimming last character from a string:

另一个从字符串中删除最后一个字符的例子:

string outputText = inputText.Remove(inputText.Length - 1, 1);

You can put it into an extension method and prevent it from null string, etc.

可以将它放入扩展方法中,并防止它出现空字符串等。

#7


2  

string s1 = "Hello! world!"
string s2 = s1.Substring(0, s1.Length - 1);
Console.WriteLine(s1);
Console.WriteLine(s2);

#8


1  

you could also use this:

你也可以用这个:

public static class Extensions
 {

        public static string RemovePrefix(this string o, string prefix)
        {
            if (prefix == null) return o;
            return !o.StartsWith(prefix) ? o : o.Remove(0, prefix.Length);
        }

        public static string RemoveSuffix(this string o, string suffix)
        {
            if(suffix == null) return o;
            return !o.EndsWith(suffix) ? o : o.Remove(o.Length - suffix.Length, suffix.Length);
        }

    }

#9


1  

Try this:

试试这个:

return( (str).Remove(str.Length-1) );

#10


0  

I took the path of writing an extension using the TrimEnd just because I was already using it inline and was happy with it... i.e.:

我选择使用TrimEnd来编写扩展,因为我已经内联地使用它了,我对它很满意……例如:

static class Extensions
{
        public static string RemoveLastChars(this String text, string suffix)
        {            
            char[] trailingChars = suffix.ToCharArray();

            if (suffix == null) return text;
            return text.TrimEnd(trailingChars);
        }

}

Make sure you include the namespace in your classes using the static class ;P and usage is:

确保您使用静态类在类中包含名称空间;P和用法是:

string _ManagedLocationsOLAP = string.Empty;
_ManagedLocationsOLAP = _validManagedLocationIDs.RemoveLastChars(",");          

#11


-3  

If you want to remove the '!' character from a specific expression("world" in your case), then you can use this regular expression

如果你想删除!“字符从一个特定的表达式(在您的例子中是“world”),然后您可以使用这个正则表达式

string input = "Hello! world!";

string output = Regex.Replace(input, "(world)!", "$1", RegexOptions.Multiline | RegexOptions.Singleline);

// result: "Hello! world"

the $1 special character contains all the matching "world" expressions, and it is used to replace the original "world!" expression

$1特殊字符包含所有匹配的“world”表达式,它被用来替换原来的“world!”表达式。

#1


213  

"Hello! world!".TrimEnd('!');

read more

阅读更多

EDIT:

编辑:

What I've noticed in this type of questions that quite everyone suggest to remove the last char of given string. But this does not fulfill the definition of Trim method.

我在这类问题中注意到,每个人都建议删除给定字符串的最后一个字符。但这并不能满足Trim方法的定义。

Trim - Removes all occurrences of white space characters from the beginning and end of this instance.

从这个实例的开头和结尾删除所有出现的空白字符。

MSDN-Trim

MSDN-Trim

Under this definition removing only last character from string is bad solution.

在这个定义下,只从字符串中删除最后一个字符是不好的解决方案。

So if we want to "Trim last character from string" we should do something like this

如果我们想要“从字符串中删除最后一个字符”,我们应该这样做

Example as extension method:

示例扩展方法:

public static class MyExtensions
{
  public static string TrimLastCharacter(this String str)
  {
     if(String.IsNullOrEmpty(str)){
        return str;
     } else {
        return str.TrimEnd(str[str.Length - 1]);
     }
  }
}

Note if you want to remove all characters of the same value i.e(!!!!)the method above removes all existences of '!' from the end of the string, but if you want to remove only the last character you should use this :

注意,如果您想删除所有相同值的字符,例如(!!!! !),上面的方法将删除'!“如果你想只删除最后一个字符,你应该这样做:

else { return str.Remove(str.Length - 1); }

#2


52  

String withoutLast = yourString.Substring(0,(yourString.Length - 1));

#3


7  

if (yourString.Length > 1)
    withoutLast = yourString.Substring(0, yourString.Length - 1);

or

if (yourString.Length > 1)
    withoutLast = yourString.TrimEnd().Substring(0, yourString.Length - 1);

...in case you want to remove a non-whitespace character from the end.

…如果您想从最后删除一个非空白字符。

#4


5  

string helloOriginal = "Hello! World!";
string newString = helloOriginal.Substring(0,helloOriginal.LastIndexOf('!'));

#5


5  

        string s1 = "Hello! world!";
        string s2 = s1.Trim('!');

#6


4  

The another example of trimming last character from a string:

另一个从字符串中删除最后一个字符的例子:

string outputText = inputText.Remove(inputText.Length - 1, 1);

You can put it into an extension method and prevent it from null string, etc.

可以将它放入扩展方法中,并防止它出现空字符串等。

#7


2  

string s1 = "Hello! world!"
string s2 = s1.Substring(0, s1.Length - 1);
Console.WriteLine(s1);
Console.WriteLine(s2);

#8


1  

you could also use this:

你也可以用这个:

public static class Extensions
 {

        public static string RemovePrefix(this string o, string prefix)
        {
            if (prefix == null) return o;
            return !o.StartsWith(prefix) ? o : o.Remove(0, prefix.Length);
        }

        public static string RemoveSuffix(this string o, string suffix)
        {
            if(suffix == null) return o;
            return !o.EndsWith(suffix) ? o : o.Remove(o.Length - suffix.Length, suffix.Length);
        }

    }

#9


1  

Try this:

试试这个:

return( (str).Remove(str.Length-1) );

#10


0  

I took the path of writing an extension using the TrimEnd just because I was already using it inline and was happy with it... i.e.:

我选择使用TrimEnd来编写扩展,因为我已经内联地使用它了,我对它很满意……例如:

static class Extensions
{
        public static string RemoveLastChars(this String text, string suffix)
        {            
            char[] trailingChars = suffix.ToCharArray();

            if (suffix == null) return text;
            return text.TrimEnd(trailingChars);
        }

}

Make sure you include the namespace in your classes using the static class ;P and usage is:

确保您使用静态类在类中包含名称空间;P和用法是:

string _ManagedLocationsOLAP = string.Empty;
_ManagedLocationsOLAP = _validManagedLocationIDs.RemoveLastChars(",");          

#11


-3  

If you want to remove the '!' character from a specific expression("world" in your case), then you can use this regular expression

如果你想删除!“字符从一个特定的表达式(在您的例子中是“world”),然后您可以使用这个正则表达式

string input = "Hello! world!";

string output = Regex.Replace(input, "(world)!", "$1", RegexOptions.Multiline | RegexOptions.Singleline);

// result: "Hello! world"

the $1 special character contains all the matching "world" expressions, and it is used to replace the original "world!" expression

$1特殊字符包含所有匹配的“world”表达式,它被用来替换原来的“world!”表达式。