Sunday, August 25, 2013

30

/*In programming, every character space in a string is a position, but we refer to that position as index beginning at zero*/
static void Main(string[] args)
{
  string foo = "Hello World";
  int index = foo.IndexOf('e');
  Console.WriteLine(index);
}
/*This returns '1' because the index of 'e' is the 2nd position in the string*/
int index = foo.IndexOf('el');
/*would return the same index because C# will look for the beginning of the string 'el'*/
int index = foo.IndexOf('e ');
/*would return -1 because there is no 'e' followed immediately by ' '*/

/*Find the second L
string foo = "Hello World";
int index = foo.IndexOf('L' foo.IndexOf("l")+1);
Console.WriteLine(index);

/*Find the third L
string foo = "Hello World";
int index = foo.IndexOf('L' foo.IndexOf("l")+2);
Console.WriteLine(index);
/*Or
string foo = "Hello World";
int index = foo.LastIndexOf('L');
Console.WriteLine(index);

Substring
string foo = "Hello World";
string bar = foo.Substring(7, 3);

Console.WriteLine(bar);
//Will output "orl"

Substring
string foo = "Hello World";
string bar = foo.Remove(0, 3);
string bar1 = bar.Remove(2, 3);
string bar2 = bar1.Remove(3, 1);

Console.WriteLine(bar2);
//This will output lord


Substring
string foo = "Hello World";
string bar = foo.Replace("Hello ", string.Empty);

Console.WriteLine(bar);
/*Will output Hello

string foo = "Hello World";
string bar = foo.Replace("o", "a");
string bar1 = bar.Replace("arl", "eir");
Console.WriteLine(bar1);
/*Will output Hella Weird












No comments:

Post a Comment