Greg Owen
Over 27 Years of Programming Experience
Programming Examples, Portfolio, and More
Home

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
Some words from the sponsors...

Add a C# Dropdown to a Bootstrap Modal Dialog

This example combines C# and Bootstrap to create a bootstrap modal dialog containing an asp dropdown and an asp button to retrieve...

Continue Reading

Use C# to Populate Dropdown Lists With XML

The code below opens an xml file, reads the data from the xml file, dynamically adds a label for the dropdown list, and adds the dropdown list to the asp.net page...

Continue Reading

Add Style Properties to ASP.Net Controls to Improve Appearance

The default formatting for ASP.Net controls is very dated.  The default formatting is basically the same as the formatting in Visual Basic 1.0 ...

Continue Reading

Export To A Text File From A DataGridView Control

Add a button control to Form1.
Add a DataGridView Control to Form1....

Continue Reading