Tuesday, November 25, 2008

String Manipulation

The First thing is you have to know that a string type variable can be treated as a read - only array of char variables.This means that you can access individual characters using syntax like the following:

string myString = "A string";
char myChar = myString[1];

*** However, you can ’ t assign individual characters in this way***

*** As with arrays, you can also get the number of elements using" myString.Length ". This gives you the number of characters in the string:

<> .ToLower() - These enable strings to be converted into lowercase.

<> .ToUpper() - These enable strings to be converted into uppercase.

<> .Trim() - if the user accidentally put an extra space at the beginning or end of their input? In this case, the preceding code won ’ t work.You need to trim the string entered, which you can do using the <> .Trim() command

<> .TrimStart() - will trim spaces from the beginning of a string

<> .TrimEnd() - will trim spaces from the end of a string

<>.PadLeft() - These enable you to add spaces to the left of a string to force it to the desired length.

<> .PadRight() - These enable you to add spaces to the right of a string to force it to the desired length.

You use these as follows

<> .PadX( <> );

eg:

myString = "Aligned";
myString = myString.PadLeft(10);


<> .Split() - converts a string into a string array by splitting it at the points specified.

No comments: