Monday, September 24, 2018 01:33

Archive for the ‘Strings And Text Processing’ Category

String Builder

Friday, April 21st, 2017

I was explaining at some point that string is an immutable type. That means that once you assign a value to a string variable, you cannot directly modify it anymore. This also means that any string operation using any function such as Trim(), Replace(), ToUpper(), etc, will actually create a new string in memory where the resulting value will be stored, and it will delete the old, initial value. … Read more

Other string methods

Friday, April 21st, 2017

There are a number of other string methods that you might find useful. They are:

Compare(), CompareTo()CompareOrdinal() – determines the sort order of strings. It checks if one string is ordered before another when in alphabetical order, whether it is ordered after, or is equivalent.… Read more

Replacing a string inside another string

Friday, April 21st, 2017

Whenever you want to replace a string inside another string, .NET Framework offers the Replace() function. It is a function that accepts two string parameters, the string to be replaced and the string to replace with. Its usage is very simple:

Removing unnecessary characters

Friday, April 21st, 2017

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.… Read more

Splitting a string by a separator

Friday, April 21st, 2017

There are many cases when we have a string that contains some elements separated by a separator, and we would need to get these elements. For this, we can use the Split() function, which returns an array of strings. The alternative would be to manually search for the separator character using IndexOf() function, then to retrieve the individual substring, and all the extra horrendous work that comes with it.… Read more

Substring

Friday, April 21st, 2017

A substring is a string located inside another string. To extract a portion of a string (substring), we can use the Substring() method, with some additional parameters, such as the starting index and the character count or length. Whenever the length parameter is omitted, the function will simply return the substring that starts at the specified index until the end of the string.… Read more

Searching for a string within another string

Friday, April 21st, 2017

Another very useful operation when dealing with text is the searching of a certain string or letter inside another string. There are multiple ways of accomplishing this, each behaving in a different way.

The first function that we can use to perform a search is Contains().… Read more

UPPERCASE and lowercase

Friday, April 21st, 2017

There are times when we need to convert the letters of a string to either uppercase or lowercase. Fortunately, C# offers us two methods for this: ToUpper() and ToLower().  As imagined, the first one will convert all letters of a string into capital letters, while the latter will do the opposite, by converting them to small ones.… Read more

Concatenation

Friday, April 21st, 2017

Since now we know the fundamentals about strings and their structure, it is time to learn about the various operations we can perform on them. The simplest of these operation is concatenation, or joining two or more strings together and obtaining a new string as a result.… Read more

Strings

Friday, April 21st, 2017

So far, we have often used Console.ReadLine() in our Console programs, in order to get some text input from the user. Whenever we needed to use that text (and even inspecting the Console.ReadLine() method signature), we needed to store that text in a string variable type.… Read more