C# - Strings


Advertisements

Previous Page Next Page

C# Strings

Strings are one of the most common data types in C#. It is used for storing text. It can be created by enclosing characters in double quotation marks. It can be assigned to a variable using = sign.

string MyString = "Hello World!";

Access character of a String

A character (also called element) of a string can be accessed with it's index number. In C#, index number starts with 0 in forward direction. The below figure and example describe the indexing concept of a string.

String Indexing:

C# String Indexing

using System;

namespace MyApplication { 
  class MyProgram {
    static void Main(string[] args) {
      string MyString = "HELLO";
      Console.WriteLine(MyString[1]); 
      Console.WriteLine(MyString[4]); 
    }
  }
}

Output

E
O   

String Length

A String in C# is an object and the String object has a method called Length which can be used to find the total number of characters in the String.

using System;

namespace MyApplication { 
  class MyProgram {
    static void Main(string[] args) {
      string MyString = "Hello World!";
      Console.WriteLine(MyString.Length);  
    }
  }
}

Output

12

Check a character in the String

C# String has IndexOf() method which returns index position of first occurrence of specified text in the string. Please note that, C# counts index position from 0.

using System;

namespace MyApplication { 
  class MyProgram {
    static void Main(string[] args) {
      string MyString = "Hello World!";
      Console.WriteLine(MyString.IndexOf("Wor"));  
    }
  }
}

Output

6

String Concatenation

In C#, two strings can be joined using + operator. Along with this, C# String has Concat() method which can also be used to combine two strings.

using System;

namespace MyApplication { 
  class MyProgram {
    static void Main(string[] args) {
      string text_1 = "Learn ";
      string text_2 = "C# ";
      string text_3 = "with AlphaCodingSkills.com";
      Console.WriteLine(text_1 + text_2 + text_3); 
      Console.WriteLine(string.Concat(text_1, text_2, text_3)); 
    }
  }
}

Output

Learn C# with AlphaCodingSkills.com
Learn C# with AlphaCodingSkills.com

String Interpolation

Another way of joining two strings in C# is string interpolation. It places value of the variable into its placeholder in the string. It automatically places a whitespace between placeholders.

using System;

namespace MyApplication { 
  class MyProgram {
    static void Main(string[] args) {
      string text_1 = "C#";
      string text_2 = "with AlphaCodingSkills.com";
      string MyString = $"I learn {text_1} {text_2}";
      Console.WriteLine(MyString); 
    }
  }
}

Output

I learn C# with AlphaCodingSkills.com

Special Strings in String

The backslash \ escape character is used to convert special character like single quote, double quote, new line, etc. into the string character. The below mentioned table describes special characters in C#:

Escape CharacterResultExample
\' ' "\'C Sharp\'" is converted into: 'C Sharp'
\" " "\"World\"" is converted into: "World"
\\ \ "A\\C" is converted into: A\C
\n new line "Hello\nJohn" is converted into:
Hello
John
\t Tab "Hello\tMarry" is converted into: Hello    Marry
\b Backspace "Hello C\bSharp" is converted into: Hello Sharp

String Methods

For complete list of string methods, please refer to string methods page. Here, few very common string methods are discussed.

  • ToLower(): Returns string in lowercase
  • ToUpper(): Returns string in uppercase
  • Trim(): Removes whitespaces from start and end of the string
  • Replace(): replace specified character(s) with another specified character(s)

Example: ToLower() and ToUpper() String Methods


using System;

namespace MyApplication { 
  class MyProgram {
    static void Main(string[] args) {
      string MyString = "Learn Programming";
      Console.WriteLine(MyString.ToLower()); 
      Console.WriteLine(MyString.ToUpper()); 
    }
  }
}

Output

learn programming
LEARN PROGRAMMING

Example: Trim() and Replace() String Methods


using System;

namespace MyApplication { 
  class MyProgram {
    static void Main(string[] args) {
      string MyString = "  Learn Programming  ";
      Console.WriteLine(MyString.Trim()); 
      Console.WriteLine(MyString.Replace("Programming", "C#")); 
    }
  }
}

Output

Learn Programming
  Learn C# 

C# String Methods
MethodDescription
Clone() Make clone of string.
CompareTo() Compare two strings and returns integer value as output. It returns 0 for true and 1 for false.
Contains() The C# Contains method checks whether specified character or string is exists or not in the string value.
EndsWith() This EndsWith Method checks whether specified character is the last character of string or not.
Equals() The Equals Method in C# compares two string and returns Boolean value as output.
GetHashCode() This method returns HashValue of specified string.
GetType() It returns the System.Type of current instance.
GetTypeCode() It returns the Stystem.TypeCode for class System.String.
IndexOf() Returns the index position of first occurrence of specified character.
ToLower() Converts String into lower case based on rules of the current culture.
ToUpper() Converts String into Upper case based on rules of the current culture.
Insert() Insert the string or character in the string at the specified position.
IsNormalized() This method checks whether this string is in Unicode normalization form C.
LastIndexOf() Returns the index position of last occurrence of specified character.
Length It is a string property that returns length of string.
Remove() This method deletes all the characters from beginning to specified index position.
Replace() This method replaces the character.
Split() This method splits the string based on specified value.
StartsWith() It checks whether the first character of string is same as specified character.
Substring() This method returns substring.
ToCharArray() Converts string into char array.
Trim() It removes extra whitespaces from beginning and ending of string.

Previous Page Next Page
Advertisements