How to remove whitespace from a text?

Asked

Viewed 13,362 times

6

I was trying to get blanks out of a string, but the .Trim() does not work, it displays the white spaces yet:

 var teste = texto.Trim();
 txtConteudo.Text = teste;

For example, if he receives: "text ", continue: "text" even after going through Trim. Shouldn’t it show "texttext"? How to get this result?

  • 1

    Why doesn’t it work? Show, give details. Working: https://dotnetfiddle.net/kaKhfO

  • it shows in the textbox the text with spaces... I edited the question!

  • How do you know? Put a copy of the screen showing that has the spaces. Maybe the problem is elsewhere.

  • 2

    You ask why the Trim does not work to remove space from the beginning of the string but in the code you were using TrimEnd. I mean, the problem was just a typo?

  • no, the problem was how I was thinking the trim worked...

  • 1

    @Caffé is right. The title of the question does not match the code shown - if it hit, the whitespace would have been removed. Clearly it was a typo and the question must be closed.

  • @dcastro I edited the question, I had put the trimEnd in the question by mistake, but explained there the problem

  • The question is not a typo, it is the ignorance about the resource to be used to get what you wanted.

Show 3 more comments

3 answers

12


There are 3 methods to cut spaces, you can cut at the beginning, at the end, or both. You have to use the right one for what you want. It is still possible to cut all. The example shown in the question indicates the use of the wrong method:

using static System.Console;
                    
public class Program {
    public static void Main() {
        var texto = " Hello World ";
        WriteLine($"Corta o fim: |{texto.TrimEnd()}|");
        WriteLine($"Corta o início: |{texto.TrimStart()}|");
        WriteLine($"Corta ambos: |{texto.Trim()}|");
        WriteLine($"Corta tudo: |{texto.Replace(" ", "")}|");
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • 1

    its slight. I ask more questions than I answer, because when I answer already was (who tells me to be slow). Jokes aside, +1 by the answer.

  • 1

    @bigown, what does this "$" mean before String?

  • I was using the Trim the wrong way, I thought he tbm did what the replace does, thanks for the clarification!

  • 1

    @Jeangustavoprates opens a question that I answer :D will be useful for everyone.

  • @mustache, I’ll open it right away!

3

The Trim function only removes whitespace that is contained at the beginning and end of the string, i.e., whitespace that will exist inside the string will not be removed.

var texto = " Texto a ser removidos seus espaços. ";

//Trim() remove os espaços do inícil e do fim da string.
var trimTexto = texto.Trim();//Output: "Texto a ser removidos seus espaços."

//TrimStart() remove os espaços somente do inícil da string.
var trimTexto2 = texto.TrimStart();//Output: "Texto a ser removidos seus espaços. "

//TrimEnd() remove os espaços somente do fim da string.
var trimTexto3 = texto.TrimEnd();//Output: " Texto a ser removidos seus espaços."

-2

function getCdEspacosRetirar($valor){ 
    $array  = explode(" ", $valor);
    $valor  =   '';
    for ($i = 0; $i <= count($array); $i++) { $valor    .= ($array[$i])? 
$array[$i].' ':'';}
return trim($valor);}

$nomeAdmin  =   ' wanderley           ricardo          de                 
araujo  ';
$nomeAdmin  =   getCdEspacosRetirar($nomeAdmin);

There must be similar functions in another language. This is php.

  • That doesn’t answer the question the fulcrum is C#.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.