tags:

views:

83

answers:

7

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.

+2  A: 
String withoutLast = yourString.Substring(0,yourString.Length -1);
Nimrod Shory
Just be sure that yourString contains at least 1 character.
Nimrod Shory
+1  A: 
string helloOriginal = "Hello! World!";
string newString = helloOriginal.Substring(0,helloOriginal.LastIndexOf('!'));
Nissim
+1  A: 
string s1 = "Hello! world!"
string s2 = s1.Substring(0, s1.Length - 1);
Console.WriteLine(s1);
Console.WriteLine(s2);
Jonathan
+3  A: 
"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 - Removes all occurrences of white space characters from the beginning and end of this instance.

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]);
     }
  }
}
Vash
This is better as long as you always know what character you want to remove from the end of the String.
Nimrod Shory
As he sed "out the '!'", but if we don't know we should use, `s = s.TrimEnd(s[s.Length - 1]);`, removing last char from string do not full fill the definition of Trim.
Vash
Hi Vash, what do you think about my solution using a RegEx? It fulfills the Thqr's request and it is allows to remove the '!' char from any "world!" expression, anywhere the expression is placed, in just one code line.
Marcello Faga
I think that "Hello! World!", is only a example and in real app we don't know the exact word to use that. That approach require to have lot knowledge about the parsed string. So more readable in this case would be `"Hello! World!".Replace("World!","World");`; moreover you also make that mistake with replacing and not doing trim (reduce).
Vash
Yes you're quite right Vash, i defined a regex just for the reason you said: you have just to change (or making it dynamic) the expression being the target of the manipulation.Concerning the mistake, i think Thqr is not interested to trim white spaces from the original string, or maybe i did not understand what you wrote; anywhere thank you for the feedback
Marcello Faga
+1  A: 
        string s1 = "Hello! world!";
        string s2 = s1.Trim('!');
JDunkerley
+1  A: 

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

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

Marcello Faga
+1  A: 

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);
        }

    }
Omu