Progress Monitor for IdealProgrammer.com
Take Swift, Intelligent, Massive, Planned, Loving, Effective (S.I.M.P.L.E.) Action to Transform Self into Ideal
Skip Repetitive Navigational Links
Home
News
Presenters
Register
Contact
Categories
Titles
Converter
Code Samples
C# ASP.NET
C# Console
HTML
JavaScript
SQL Server
VB ASP.NET
VB Console
Please login
C# String Examples
Description:
Illustrates using String Properties and String Methods in C-Sharp.
Example Class Main:
using System; using System.Text; public class clsString { public void Main() { //**************************************************************************************** // Example #1: StartsWith(string) // Example #2: EndsWith(string) // Example #3: IndexOf(string[, startIndex][,count]) // Example #4: LastIndexOf(string[, startIndex][,count]) // Example #5: Insert(startIndex, string) // Example #6: PadLeft(totalWidth [, paddingCharacter]) // Example #7: PadRight(totalWidth [, paddingCharacter]) // Example #8: Remove(startIndex, count) // Example #9: Replace(oldString, newString) // Example #10: Substring(startIndex[, length]) // Example #11: ToLower // Example #12: ToUpper // Example #13: Trim // Example #14: Length //**************************************************************************************** //**************************************************************************************** // Example #1: StartsWith(string) // Returns a Boolean value that indicates if the string starts with the specified string. //**************************************************************************************** Console.WriteLine("Example #1: StartsWith(string) "); string strStartsWithExample = "This is a test string"; Console.WriteLine(strStartsWithExample.StartsWith("This")); //Returns True //write blank line to make output easier to read Console.WriteLine(); //**************************************************************************************** // Example #2: EndsWith(string) // Returns a Boolean value that indicates if the string ends with the specified string. //**************************************************************************************** Console.WriteLine("Example #2: EndsWith(string) "); string strEndsWithExample = "This is a test string"; Console.WriteLine(strEndsWithExample.EndsWith("string")); //Returns True //write blank line to make output easier to read Console.WriteLine(); //**************************************************************************************** // Example #3: IndexOf(string[, startIndex][,count]) // Returns an integer that represents the position of the first occurrence of the // specified number of characters. If you specify startIndex, that specifies // where to start looking. If you specify count, that specifies how many characters to // to search past start. If the string is not found, this method returns -1 //**************************************************************************************** Console.WriteLine("Example #3: IndexOf(string[, startIndex][,count]) "); string strIndexOfExample = "This is a test string"; Console.WriteLine(strIndexOfExample.IndexOf("is")); //Returns 2 Console.WriteLine(strIndexOfExample.IndexOf("xyz")); //Returns -1 //write blank line to make output easier to read Console.WriteLine(); //**************************************************************************************** // Example #4: LastIndexOf(string[, startIndex][,count]) // Returns an integer that represents the position of the last occurrence of the // specified number of characters. If you specify startIndex, that specifies // where to start looking. If you specify count, that specifies how many characters to // to search past start. If the string is not found, this method returns -1 //**************************************************************************************** Console.WriteLine("Example #4: LastIndexOf(string[, startIndex][,count]) "); string strLastIndexOfExample = "This is a test string"; Console.WriteLine(strLastIndexOfExample.LastIndexOf("is")); //Returns 5 Console.WriteLine(strLastIndexOfExample.LastIndexOf("xyz")); //Returns -1 //write blank line to make output easier to read Console.WriteLine(); //**************************************************************************************** // Example #5: Insert(startIndex, string) // Returns a string with the specified string inserted beginning at the specified position. //**************************************************************************************** Console.WriteLine("Example #5: Insert(startIndex, string) "); string strInsertExample = "This is a test string"; int intIndex = 0; // First find position of word test intIndex = strInsertExample.IndexOf("test"); // Insert "simple" before "test" Console.WriteLine(strInsertExample.Insert(intIndex, "simple ")); //write blank line to make output easier to read Console.WriteLine(); //**************************************************************************************** // Example #6: PadLeft(totalWidth [, paddingCharacter]) // Returns a string that is right-aligned and padded on left with character specified. // If no padddingCharacter is specified, space is used. totalWidth is total width of column // This allows you to create a nice straight column even though lengths of text vary. //**************************************************************************************** Console.WriteLine("Example #6: PadLeft(totalWidth [, paddingCharacter]) "); string strPadLeftExampleA = "This is a test string"; string strPadLeftExampleB = "short"; string strPadLeftExampleC = "longer"; // PadLeft "simple" before "test" Console.WriteLine(strPadLeftExampleA.PadLeft(35)); Console.WriteLine(strPadLeftExampleB.PadLeft(35)); Console.WriteLine(strPadLeftExampleC.PadLeft(35)); //write blank line to make output easier to read Console.WriteLine(); //**************************************************************************************** // Example #7: PadRight(totalWidth [, paddingCharacter]) // Returns a string that is left-aligned and padded on right with character specified. // If no padddingCharacter is specified, space is used. totalWidth is total width of column // This allows you to create a nice straight column even though lengths of text vary. //**************************************************************************************** Console.WriteLine("Example #7: PadRight(totalWidth [, paddingCharacter]) "); string strPadRightExampleA = "This is a test string"; string strPadRightExampleB = "short"; string strPadRightExampleC = "longer"; // PadRight "simple" before "test" Console.WriteLine(strPadRightExampleA.PadRight(35) + "next column starts here"); Console.WriteLine(strPadRightExampleB.PadRight(35) + "next column starts here"); Console.WriteLine(strPadRightExampleC.PadRight(35) + "next column starts here"); //write blank line to make output easier to read Console.WriteLine(); //**************************************************************************************** // Example #8: Remove(startIndex, count) // Returns a string with the specified number of characters removed starting at startIndex. // If no Count is specified, the remainder of string is removed //**************************************************************************************** Console.WriteLine("Example #8: Remove(startIndex, count) "); string strRemoveExample = "This is a test string"; int intIndexRemove = 0; // First find position of word test intIndexRemove = strRemoveExample.IndexOf("test"); // Remove rest of string starting with word "test" Console.WriteLine(strRemoveExample.Remove(intIndex)); //write blank line to make output easier to read Console.WriteLine(); //**************************************************************************************** // Example #9: Replace(oldString, newString) // Returns a string with all occurences of oldString replaced by newString //**************************************************************************************** Console.WriteLine("Example #9: Replace(oldString, newString) "); string strReplaceExample = "This is a test string"; // Replace "This" with "That" Console.WriteLine(strReplaceExample.Replace("This", "That")); //write blank line to make output easier to read Console.WriteLine(); //**************************************************************************************** // Example #10: Substring(startIndex[, length]) // Returns the string that starts at the specified position and has the specified // length. If the length is not specified, all of the characters to the end are returned. //**************************************************************************************** Console.WriteLine("Example #10: Substring(startIndex[, length]) "); string strSubstringExample = "This is a test string"; int intIndexSubstring = 0; // First find position of word test intIndexSubstring = strRemoveExample.IndexOf("test"); // Get everything starting at "test" to end of string Console.WriteLine(strSubstringExample.Substring(intIndexSubstring)); //write blank line to make output easier to read Console.WriteLine(); //**************************************************************************************** // Example #11: ToLower // Returns the string in Lowercase //**************************************************************************************** Console.WriteLine("Example #11: ToLower"); string strToLowerExample = "This is a test string"; // Return Lowercase Console.WriteLine(strToLowerExample.ToLower()); //write blank line to make output easier to read Console.WriteLine(); //**************************************************************************************** // Example #12: ToUpper // Returns the string in Uppercase //**************************************************************************************** Console.WriteLine("Example #12: ToUpper "); string strToUpperExample = "This is a test string"; // Return Uppercase Console.WriteLine(strToUpperExample.ToUpper()); //write blank line to make output easier to read Console.WriteLine(); //**************************************************************************************** // Example #13: Trim // Returns the string with leading and trailing spaces removed //**************************************************************************************** Console.WriteLine("Example #13: Trim "); string strTrimExample = " ABC "; // Return Uppercase Console.WriteLine(strTrimExample.Trim().Length); //write blank line to make output easier to read Console.WriteLine(); //**************************************************************************************** // Example #14: Length // Returns the string with leading and trailing spaces removed //**************************************************************************************** Console.WriteLine("Example #14: Length "); string strLengthExample = " ABC "; // Return Uppercase Console.WriteLine(strLengthExample.Length); //write blank line to make output easier to read Console.WriteLine(); //Prevent console from closing before you press enter Console.ReadLine(); } }
Example Class Program:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharp_Syntax { class Program { static void Main(string[] args) { //LanguageBasics clsString myString = new clsString(); myString.Main(); } } }
Home
News
Presenters
Register
Contact
Categories
Titles
Converter
Code Samples
C# ASP.NET
C# Console
HTML
JavaScript
SQL Server
VB ASP.NET
VB Console