Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove first and last character of a string in C#?

Tags:

c#

String: "hello to the very tall person I am about to meet"

What I want it to become is this:

String: hello to the very tall person I am about to meet

I can only find code to trim the start?

like image 530
Beefo Avatar asked Mar 22 '17 22:03

Beefo


People also ask

How do you remove the first and last letter of a string?

The idea is to use the deleteCharAt() method of StringBuilder class to remove first and the last character of a string. The deleteCharAt() method accepts a parameter as an index of the character you want to remove.

How do you remove the last character of a string in C?

Every string in C ends with '\0'. So you need do this: int size = strlen(my_str); //Total size of string my_str[size-1] = '\0'; This way, you remove the last char.

How do you delete the first character in a string C?

To remove the first character of a string, we can use the char *str = str + 1 in C. it means the string starts from the index position 1. Similarly, we can also use the memmove() function in C like this.

How do I remove one 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.


4 Answers

Use the String.Substring method.

So, if your string is stored in a variable mystr, do as such:

mystr = mystr.Substring(1, mystr.Length - 2); 
like image 186
Anish Goyal Avatar answered Oct 21 '22 03:10

Anish Goyal


If you want to remove any first and last character from the string, then use Substring as suggested by Anish, but if you just want to remove quotes from beginning and the end, just use

myStr = myStr.Trim('"'); 

Note: This will remove all leading and trailing occurrences of quotes (docs).

like image 27
z m Avatar answered Oct 21 '22 05:10

z m


If you are trying to remove specific characters from a string, like the quotes in your example, you can use Trim for both start and end trimming, or TrimStart and TrimEnd if you want to trim different characters from the start and end. Pass these methods a character (or array of characters) that you want removed from the beginning and end of the string.

var quotedString = "\"hello\"";
var unQuotedString = quotedString.TrimStart('"').TrimEnd('"'); 

// If the characters are the same, then you only need one call to Trim('"'):
unQuotedString = quotedString.Trim('"');

Console.WriteLine(quotedString);
Console.WriteLine(unQuotedString);

Output:

"hello"

hello

Alternatively, you can use Skip and Take along with Concat to remove characters from the beginning and end of the string. This will work even for and empty string, saving you any worries about calculating string length:

var original = "\"hello\"";
var firstAndLastRemoved = string.Concat(original.Skip(1).Take(original.Length - 2));
like image 23
Rufus L Avatar answered Oct 21 '22 03:10

Rufus L


C# 8: myString[1..^1]
See Indices and ranges

like image 41
Jan Paolo Go Avatar answered Oct 21 '22 03:10

Jan Paolo Go