Visual Studio String Functions-CSharp
string sHold = “”;
int nIndex = 0;
//Replaces a string with a specified string.
sHold = “String Test”;
sHold = sHold.Replace(“String”, “Replace”);
//Returns “Replace Test”
//Removes a specified number of characters from a string
//The first number is the starting position, and the last
//number is the ending position.
sHold = “String Test”;
sHold = sHold.Remove(0, 3);
//Result = “ing Test”
//Returns the last index of a specified string.
sHold = “String Test”;
nIndex = sHold.LastIndexOf(“s”);
//Result = 10
//Inserts a string in the specified position.
sHold = “String Test”;
sHold = sHold.Insert(sHold.Length, ” IndexOf”);
//Result = “String Test IndexOf”
//Return the index of a specified string.
sHold = “String Test”;
nIndex = sHold.IndexOf(“r”);
//Result = 3
//Convert To Upper Case
sHold = “String Test”;
sHold = sHold.ToUpper();
//Result = “STRING TEST”
//Convert To Lower Case
sHold = “String Test”;
sHold = sHold.ToLower();
//Result = “string test”
//PadLeft adds specified characters to the beginning of the string.
//The number represents the total length of the resulting string
//including the number of padding characters.
sHold = “String Test”;
sHold = sHold.PadLeft(sHold.Length + 5, Convert.ToChar(” “));
//Result = ” String Test”
//PadRight adds specified characters to the end of the string.
//The number represents the total length of the resulting
string
//including the number of padding characters.
sHold = “String Test”;
sHold = sHold.PadLeft(6, Convert.ToChar(” “));
//Result = “String Test”
sHold = “String Test”;
sHold = sHold.PadRight(sHold.Length + 5, Convert.ToChar(” “));
//Result = “String Test ”
sHold = “String Test”;
sHold = sHold.PadRight(6, Convert.ToChar(” “));
//Result = “String Test”
//EndsWith Determines if a string ends with a specified string and returns
//true or false.
bool bTest = false;
sHold = “String Test”;
bTest = sHold.EndsWith(“Test”);
//Result = true
//StartsWith Determines if a string begins with a specified string and returns
//true or false.
bTest = false;
sHold = “String Test”;
bTest = sHold.StartsWith(“String”);
//Result = true
//Substring returns a string based on the starting index and ending index.
//If the starting or ending specified falls outside the length
of the string,
//an error will be generated.
sHold = “String Test”;
sHold = sHold.Substring(2, 4);
//Result = ring
//Contains determines if a string contains the specified string.
sHold = “String Test”;
bTest = sHold.Contains(“ring”);
//Result = true
//Trims all spaces from beginning and end of specified string.
sHold = ” String Test “;
sHold = sHold.Trim();
//Result = “String Test”
#CSharp #Reference