34

I need to remove two characters from the end of the string.

So:

string = "Hello Marco !"

must be

Hello Marco

How can I do it?

0

9 Answers 9

52

You can do:

string str = "Hello Marco !";
str = str.Substring(0, str.Length - 2);
Sign up to request clarification or add additional context in comments.

4 Comments

@Alexander I don't know what language you are speaking of, but in .net c#, it is a valid method. It is also the appropriate case.
I am terribly sorry - i haven't noticed c# has been mentioned. I was just looking for the method signature and found this one :)
This solves the OP's case, but it doesn't actually answer the question of how you run a substring from the end of the string. Your answer cuts off two letters from the end of the string.
@Maritim The OP's question was a bit confusing, but his explanation says and i quote "I need to remove two characters from the end of the string".
33
s = s.Substring(0, Math.Max(0, s.Length - 2))

to include the case where the length is less than 2

Comments

21

C# 8 introduced indices and ranges which allow you to write

str[^2..]

This is equivalent to

str.Substring(str.Length - 2, 2)

In fact, this is almost exactly what the compiler will generate, so there's no overhead.

Note that you will get an ArgumentOutOfRangeException if the range isn't within the string.

4 Comments

9 years later, I'm one day late :)
This is the correct solution in modern C#
Even if it's what I was really looking for, the question wants the first n-2 chars, not the last 2 chars.
To get the first n - 2 chars, you can use str[..^2]. 2 doesn't even need to be a constant, so you can also write str[..^"test".Length]
8

What about

string s = "Hello Marco !";
s = s.Substring(0, s.Length - 2);

Comments

5

I will trim the end for unwanted characters:

s = s.TrimEnd(' ', '!');

To ensure it works even with more spaces. Or better if you want to ensure it works always, since the input text seems to come from the user:

Regex r = new Regex(@"(?'purged'(\w|\s)+\w)");
Match m = r.Match("Hello Marco   !!");
if (m.Success)
{
    string result = m.Groups["purged"].Value;
}

With this you are safer. A purge based on the fact the last two characters has to be removed is too weak.

Comments

2

Did you check the MSDN documentation (or IntelliSense)? How about the String.Substring method?

You can get the length using the Length property, subtract two from this, and return the substring from the beginning to 2 characters from the end. For example:

string str = "Hello Marco !";
str = str.Substring(0, str.Length - 2);

Comments

2

If it's an unknown amount of strings you could trim off the last character by doing s = s.TrimEnd('','!').Trim();

Have you considered using a regular expression? If you only want to allow alpha numeric characters you can use regex to replace the symbols, What if instead of a ! you get a %?

Comments

1

Try this:

var s = "Hello Marco !";

var corrected = s.Substring(0, s.Length - 2);

Comments

1
string s = "Hello Marco !";
s = s.Remove(s.length - 2, 2);

2 Comments

This only gets the last two characters.
@Yuck, yeah i was going for the Remove and by habit did substring.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.