Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substring a string from the end of the string

Tags:

c#

.net

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?

like image 478
markzzz Avatar asked Jun 24 '11 12:06

markzzz


People also ask

What substring () and substr () will do?

The difference between substring() and substr() The two parameters of substr() are start and length , while for substring() , they are start and end . substr() 's start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0 .

What is the use of substr () in string?

A string's substr() method extracts length characters from the string, counting from the start index. If start >= str. length , an empty string is returned.

How do you trim the last 3 characters of a string?

To remove the last three characters from the string you can use string. Substring(Int32, Int32) and give it the starting index 0 and end index three less than the string length. It will get the substring before last three characters.


9 Answers

You can do:

string str = "Hello Marco !";
str = str.Substring(0, str.Length - 2);
like image 158
Alex Mendez Avatar answered Oct 16 '22 16:10

Alex Mendez


s = s.Substring(0, Math.Max(0, s.Length - 2))

to include the case where the length is less than 2

like image 40
Armen Tsirunyan Avatar answered Oct 16 '22 17:10

Armen Tsirunyan


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.

like image 40
Kirill Rakhman Avatar answered Oct 16 '22 17:10

Kirill Rakhman


What about

string s = "Hello Marco !";
s = s.Substring(0, s.Length - 2);
like image 40
Øyvind Bråthen Avatar answered Oct 16 '22 17:10

Øyvind Bråthen


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.

like image 24
Felice Pollano Avatar answered Oct 16 '22 15:10

Felice Pollano


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);
like image 24
Cody Gray Avatar answered Oct 16 '22 16:10

Cody Gray


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 %?

like image 30
all2neat Avatar answered Oct 16 '22 17:10

all2neat


Try this:

var s = "Hello Marco !";

var corrected = s.Substring(0, s.Length - 2);
like image 37
Yuck Avatar answered Oct 16 '22 16:10

Yuck


string s = "Hello Marco !";
s = s.Remove(s.length - 2, 2);
like image 44
bdparrish Avatar answered Oct 16 '22 17:10

bdparrish