Learn About String In C#

Introduction 

In C#, we have a primitive type specifically for string. You don’t have to use char array as in C++. We also have a class “String” in C# for extra string operations.

Note
String is a primitive type and String is a class. For better understanding, know that String class is bold.

You can create a string in any of the below methods:

  1. string firstName;  
  2. firstName = null;  
  3. firstName = string.Empty;  
  4. firstName = "";  
  5. firstName = "Hello";  
  6. char[] letters = { 'C'' ''S''h''a''r''p' };  
  7. string objString = new string(letters);  
C#

Concatenation

  1. string lastName = "World";  
  2. string output = firstName + lastName;  
  3. Console.WriteLine("Output = " + output);  
  4. output = firstName + " " + lastName;  
  5. Console.WriteLine("Output = " + output);  
C#

Join String Array

Syntax : String.Join(delimiter, string_Array);

  1. string[] joinArray = { "String""Join""Done""Successfully" };  
  2. string joinOutput = String.Join("-", joinArray);  
  3. Console.WriteLine("Output = " + joinOutput);  
  4. string joinOutput2 = String.Join(" ", joinArray);  
  5. Console.WriteLine("Output = " + joinOutput2);  
  6. string joinOutput3 = String.Join(" ", joinArray) + ".";  
  7. Console.WriteLine("Output = " + joinOutput3);  
C#

Split String Data into Array

  1. string splitableContent = "Hello from Karthi";  
  2. string[] splitContent = splitableContent.Split(' ');  
  3. foreach (string word in splitContent)  
  4. {  
  5.     Console.WriteLine(word);  
  6. }  
C#

Length & Count

  1. string abc = "Hello";  
  2. char[] lettersArray = { 'C'' ''S''h''a''r''p' };  
  3. Console.WriteLine(abc.Length);  
  4. Console.WriteLine(lettersArray.Count());  

Length is a property of string & Array.

Output

Length = 5

Count = 7

Format String Value

Format method of String Class is used to format string value based on your needs.

  1. DateTime todayDate = DateTime.Now;  
  2. string formattedVal = String.Format("Time: {0:t} and Date : {0:D}", todayDate);  
  3. Console.WriteLine("Format String = " + formattedVal);  
C#

Copy String

  1. string a = "Hai";  
  2. string b = string.Copy(a);  
  3. Console.WriteLine("A = " + a);  
  4. Console.WriteLine("B = " + b);  
C#

Compare String

Compare method of string compares two string and returns the 0, 1 or -1.

If both are the same, it returns 0.

If the first one is bigger, it returns 1.

If the second one is bigger, it returns -1.

Syntax

  1. int Compare (string a, string b);  
  2. int Compare(string a, string b, true);  

The third parameter is used and set to true when you want to compare two strings ignoring case.

  1. //Compare  
  2. string sa1 = "Hello";    
  3. string sa2 = "hello";  
  4. Console.WriteLine("Compare = " + string.Compare(sa1, sa2));  
  5. Console.WriteLine("Compare = " + string.Compare(sa1, sa2, true));  
C#

String Equality

Equals method checks whether two strings are the same and returns true or false.

  1. string se1 = "hello";  
  2. string se2 = "hello";  
  3. Console.WriteLine("Equals = " + string.Equals(se1, se2));  
  4. Console.WriteLine("Equals 2 = " + se1.Equals(se2));  
C#

Contains & EndsWith

Contains method checks whether the string contains the specified string.

EndsWith method checks whether the string ends with the specified string.

  1. string stringObj = "this is a big sentence with number one";  
  2. Console.WriteLine("Contains 'one' = " + stringObj.Contains("one"));  
  3. Console.WriteLine("EndsWith 'one' = " + stringObj.EndsWith("one"));  
C#
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now