C# FOR BEGINNERS CRASH COURSE (2014)
Chapter 10 Strings in C#
10.1 Creation of string
A string in C# is an array of characters. The string keyword is used for declaring the variable. The keyword is an alias for System.String class.
String Object creation
There are various methods used for string creation. They are:
· Retrieving a property or calling a method returning a string
· Use of string class constructor
· Use of string concatenation operator (+)
· Calling formatted method for converting a value or an object to the string representation
· Assigning a string literal to the string variable
Example:
Example 34: |
using System; namespace StringType { class Program { static void Main(string[] args) { //string literal and concatenation string name,location; name = "Harry"; location = "USA"; string value = name + location; Console.WriteLine("The value is:{0}",value); //string constructor char[] vowels = {'a','e','i','o','u'}; string item = new string(vowels); Console.WriteLine("The item contains values:{0}",item); //formatting method for value conversion DateTime dt = new DateTime(2014,12,10,15,34,1); string msg = string.Format("Message sent at {0:t} on {0:D}",dt); //method returning value string[] array = {"Welcome","User"}; string result = String.Join("",array); Console.WriteLine("Message is:{0}",result); } } } |
When the code is compiled and executed, the output is:
The value is: Harry USA
The item contains values: aeiou
Message sent at 2.30 PM on Tuesday, June 02, 2015
Message is: Welcome User
10.2 Properties and methods of string class
Properties of string class
· Chars: Gets the Char object at specific position in the String object
· Length: Gets the number of characters in the String object
Methods of string class
· public bool Equals(string value)
Checks whether the current String object and the specified object have same value
· public string Insert(int startIndex, string value)
Returns a new string in which the specified one is inserted at a specific index position
· public string Replace(char oldchar, string newValue)
All the occurrences of a specific character in the string object are replaced with Unicode character and a new string is returned.
· public int LastIndexOf(string value)
The zero based index position of the last occurrence of the Unicode character in the string object is returned
· public string Trim()
All leading and trailing white space characters from the object are returned.
· public static Compare(string str1, string str2, bool IgnoreCase)
Compares the two strings and an integer value stating the relative position in sort order
· public static string Concat(string str1, string str2)
Concatenates the two string objects
· public string[] Split (char[] separator, int count)
A string array containing the substrings in the current string object is returned. It is delimited by the elements of a specific Unicode character array. The number of substrings returned is specified by the int parameter.
· public bool Contains(string value)
Returns value stating the specified string object occurs in the string
· public static string Copy(string str)
Creates a new String object with the same value as the specified string
· public string ToUpper()
Copy of the string converted into uppercase is returned
· public int IndexOf(string value)
Returns the zero – based index of the first occurrence of the specified string in the instance
· public string Remove(int startindex)
It removes the characters in the current instance, beginning at the specified position and moving to the last one. It returns a string.
· public char[] ToCharArray()
A Unicode character array with all the characters in the current string object is returned
· public string ToLower()
Copy of the string converted into lowercase is returned
10.3 Examples demonstrating the string functionality
String Comparison
Example 35: |
using System; namespace string1 { class Program { static void Main(string[] args) { string string1 = "A string named as string1"; string string2 = "A string named as string2"; if(String.Compare(string1,string2) == 0) { Console.WriteLine(string1+"and"+string2+"equal"); } else { Console.WriteLine(string1+"and"+string2+" not equal"); } Console.Read(); } } } |
When the code is compiled and executed, the output is:
A string named as string1 and A string named as string2 are not equal
Joining strings
Example 36: |
using System; namespace string1 { class Program { static void Main(string[] args) { string[] str1 = new string[]{"Set your aims high", "Rome was not built in a day", "Save Time"}; string str = String.Join("\n",str1); Console.WriteLine(str); Console.Read(); } } } |
When the code is compiled and executed, the output is:
Set your aims high
Rome was not built in a day
Save Time
String containing value
Example 37: |
using System; namespace string1 { class Program { static void Main(string[] args) { string str = "A new user"; if(str.Contains("user")) { Console.WriteLine("The value 'user' is present"); } Console.Read(); } } } |
When the code is compiled and executed, the output is:
The value 'user' is present”