Monday, January 24, 2011

Handling strings in C#

We can deal with strings as an array of characters. This means that we can access the characters of the string separately using a formula as follows:
string m = "a world";
char c = m[1];
Reading the characters in this way is possible, but we can not use this method to assign characterss to any text string that is acceptable to code the following:
m[1] = 'w';
So that we can get a char array can write to it we use it ToCharArray () which returns a array characters represent the array codes the text string:
string m = "a world";
char[] c = m.ToCharArray();

You can treat the char array method that we want we can change the elements and write, including what we want.
We can also use text strings within foreach loops also, for example:
foreach (char ch in m)
{
    Console.WriteLine ("{0}", ch);
}
As in the arrays, we can make up the text string using the Length as follows:
string m = Console.ReadLine("you typed {0} characters.", m.Length);
There are similar commands for ToCharArray () and are what ToLower () and ToUpper () and two characters to convert the text string to all lowercase or large ..
For example: if we had the application is expected from the user's income and based on this income there code software will be implemented, for example, the user enters yes then we have converted the text string the user entered to lowercase, we will be able to handle the events in which the user where to enter Yes or yeS or YES and cases are included too.
string u = Console.ReadLine ();

if (u.ToLower () == "yes")
{
    // Act on response
}
Note that if we assume non-use of this function, we make sure that all cases in which the user can enter the word yes (all cases must be taken into account) has therefore become a complex code.
Follow-up to the previous example: if a user tries to put additional spaces to the beginning or end of the string and in this case will not be able to get answer yes from the user.
The solution is to use the Trim (), which can be applied to the string where the removal of all blanks from the text string.
and also to remove any characters from the text string we define a array of type char as parameter for this function are as follows:
char [] t = {' ', 'e', 's'};
string u = Console.ReadLine ();

u = u.ToLower ();
u = u.Trim (t);

if (u == "y")
{
    // Act on response
}
In this way we get rid of any extra characters were written in the wrong that this would lead to address the situation of entry such as:
"Yeeeeeeees"
"Y"
"Yeess"
We can use the functions TrimStart () and TrimEnd () to get rid of spaces at the beginning or the end of the text string and can accept an array of characters as parameter for them if we are to remove certain characters from the beginning or the end of the string ..
The function PadLeft () adds a number of spaces to the left of the text string.
string m = "huda";
m = m.PadLeft(7);
Will this code to put three spaces in the beginning of a word in the variable huda m: "huda"

These approaches can tell us about the alignment of text strings within the columns and is important when the chains and put the numbers under each other.
As in The function Trim (), these two functions accept another parameter for the character that we would like to put it in surplus areas of the text string instead of blanks, for example:
string m = "huda";
m = m.PadLeft(7, '-');
The result is that the variable 'm' will contain the text string: "--- huda"

The function Split () returns an array of strings resulting from the separation of a text string, according to a specific character.
string m = "This is a test";
char[] s = {' '};
string [] w = m.Split(s);

foreach (string a in w)
{
    Console.WriteLine ("{0}", a);
}
Split command removes the character interval of the text string as well.
You can apply the previous functions to the string values directly.
string x = "Ahmad".ToUpper();
The result is that the variable x will contain the text string: "AHMAD"

No comments:

Post a Comment