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”

Advertisements
About

Over 20 years programming experience 5 years Oracle SQL/Stored Procedure programming Over 10 years SQL 2000/2005/2008 Server SQL/Stored Procedure programming Over 18 years Visual Basic 3.0/4.0/5.0/6.0/.Net/2003/2005/2008/2010/2015 Over 10 years ASP.Net Over 7 years Visual C# Over 20 years Microsoft Office and VBA HTML JAVA COBOL EASYTRIEVE FORTRAN

Tagged with: , , , , ,
Posted in Visual Studio Code Examples

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: