Featured Post

A Hole in Mars

What created this unusual hole in Mars? The hole was discovered by chance on images of the dusty slopes of Mars ... http://p.ost.im/p/druPPq

Monday, March 19, 2018

String Processing Functions in C #


First of all, it is essential to know the structure of a string. Each character in a string has an index value starting at 0. The length of a string is not the value of the last index, the length of a string counts the number of characters. Huh? That means if the string Length property is 10, then the index for the last letter is 9 (10-1).

To go through each character in a string there are two ways, both of which involve an for loop:

for (int i = 0; i

  • string currentLetter = variable.Substring (i, 1);
  • char currentLetter = variable [i];

The difference is one will return a single letter as a string and the second will return it as a character. It all depends on what kind of string processing function one is writing.

Now, a key part of writing string processing function is piecing strings together. The most common way to do it is with the addition (+) operator, for example:

stringVar + = "new text";

That works very well and very fast for small to medium strings. But what if stringVar is a huge string? All of the sudden the string object is sluggish.

For large strings, the built-in class StringBuilder is extremely handy. To add text to a StringBuilder one does the following:

stringBuilderVar.Append ("new text");

Both may seem like the same thing, and they are in a way, but when it comes to handling large strings StringBuilder is the way to go.

With those simple fundamentals, writing any sort of string processing function is quite doable.

If you would like to see a list of common string processing functions that are not included in the .Net Framework visit the C # String Functions Library page. All the functions include commented source code.



Source by Armando Pensado

The post String Processing Functions in C # appeared first on IrresistibleMT.



http://ift.tt/2DCQoCY
via IFTTT

No comments: