We will be exploring today ‘Manipulating Strings’ in many aspects.

Escape double quotes

string myString = "This is a double quote: \".";

Accessing a specific character

myString[2];

StartsWith(), EndsWith(), Contains()

Check to see if a given string has a set of characters beginning , end or somewhere inside

Return true/false

IndexOf()

Find the index for one string inside of another string

int myIndex = myString.IndexOf("How you are doing");

Insert(), Remove()

Insert: add characters starting at a given index

Remove: remove characters starting at a given index

Index, and all the way through the length you input.

Substring() 

Retrieve characters beginning at a given index all the way through the length you input.

Trim(), TrimStart(), TrimEnd()

Remove space characters both, or just the start or the need of the string.

PadLeft(), PadRight()

Allow you specify a length for a string  and a character to pad the string with if its length is less than the specified length.

myString = someValue.PadLeft(10, '#');

Notice that we are inputting a char, not a string therefore we have to use a single quote ‘no double quote

ToUpper(), ToLower()

Important! Compare two strings regardless of the case. Because in C#, two strings with different cases are NOT equal.

Replace()

Replace every occurance of one string with some other string.

myString.Replace("@@@", myValue);

Split()

Take a string and solit it into many strings and store them in a string array.

string[] names = myString.Split(';');

Concatenating strings, immutability

StringBuilder – memory efficient way of concatenating strings.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.