Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

737
Visualizações
How to remove first and last character of a string in 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?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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);
about 4 years ago · Santiago Trujillo Relatório

0

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).

about 4 years ago · Santiago Trujillo Relatório

0

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));
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda