Thursday, October 18, 2018 20:07

Removing unnecessary characters

Sometimes, and specially when working with validating data such as registration forms, you want to get rid of any characters that are “parasitic”, what the programmers refer to as white spaces. White spaces are characters that are not observable at a first glance, like tabs, a space or a new line character. These characters usually exist at the beginning or the end of the strings, and they do have a character representation when viewed inside the code. If you remember the lesson about escape sequences, I was explaining there that some characters cannot be directly represented in a string, and you need to use special characters to represent them in the code behind. Some of these escape sequences are sometimes not directly observable in the user interface, but since they exist in code, they could create problems when working with those strings. For example, if you would ask your users to enter their email address, and they press Enter after writing down their email account address, that Enter character would not be visible in the graphical user interface of your application, but it would be represented by one or more escape sequences at the end of the string. It is not a good idea to try and store or use that email address that contains escape sequences at the end of it.

So, sometimes we need to remove additional characters at the beginning or the end of strings, and the process of doing this is called trimming, and can be accomplished using the Trim() function. Let’s consider an example:

If we try to print the address variable, we would get some tab, an email address, a space and another tab, followed by two new lines. We can easily correct that by trimming our string:

Now, if we try to print the content of the variable again, we will only get the email address, without any weird characters at the beginning or the end of it.

We can also perform a multi character trimming, by specifying an array of characters that we want to remove:

Additionally, if you want to remove white spaces only at the beginning or the end of the string, you can use the TrimStart() and TrimEnd() functions instead.

Finally, a third method of removing a portion of your string, not necessarily containing white spaces, you can use the Remove() function. It removes a range of characters starting at a certain offset index, optionally followed by a character count to remove. Example:

This is the ouput:

C# Remove

In the first case, we specified only the index parameter, which removed all the text that followed after that index. In the second, we also specified a character count, which indicated how many characters we want to remove from the starting index. As with many other string functions, because Remove() accepts numerical indexes arguments, we can use it in conjunction with functions such as IndexOf(), LastIndexOf(), etc, which provide numeric indexes of the characters searched.

Comments

comments

Tags: , , , , ,

Leave a Reply