You are on page 1of 5

String in C sharp

Strings are used for storing text. A string variable contains a collection of
characters surrounded by double quotes:

string s = “Hello” ;
In C# programming, string is another kind of data type that represents Unicode
Characters. It is the alias of System.String, however, you can also write
System.String instead of a string. It is the sequence of character in which each
character is a Unicode character.

C# string function
A string in C# is actually an object, which contain properties and methods that
can perform certain operations on strings.

1- String Length

The length of a string can be found with the Length property:

Example
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Console.WriteLine("The length of the txt string is: " + txt.Length);

Output

The length of the txt string is: 26

2- ToUpper( ) and ToLower( )

Returns a copy of the string converted to uppercase or lowercase:

Example

string txt = "Hello World";

Console.WriteLine(txt.ToUpper()); // Outputs "HELLO WORLD"

Console.WriteLine(txt.ToLower()); // Outputs "hello world"

1
3- String Concatenation

The + operator can be used between strings to combine them. This is


called concatenation:

Example

string firstName = "Ali ";

string lastName = "Ahmed";

string name = firstName + lastName;

Console.WriteLine(name); // Outputs "Ali Ahmed"

Note: You can also use the string.Concat() method to concatenate two strings:

Example

string firstName = "Ali ";

string lastName = "Ahmed";

string name = string.Concat(firstName, lastName);

Console.WriteLine(name); // Outputs "Ali Ahmed"

4- Access Strings

You can access the characters in a string by referring to its index number inside
square brackets [].

This example prints the first character in myString:

Example

string myString = "Hello";

Console.WriteLine(myString[0]); // Outputs "H"

Note: String indexes start with 0: [0] is the first character. [1] is the second
character, etc.

2
5- Indexing of a specific character

You can also find the index position of a specific character in a string, by using
the IndexOf() method:

Example

string myString = "Hello";

Console.WriteLine(myString.IndexOf("e")); // Outputs "1"

6- Extract Characters from the String

Another useful method is Substring(), which extracts the characters from a


string, starting from the specified character position/index, and returns a new
string. This method is often used together with IndexOf() to get the specific
character position:

Example

// Full string

string name = "Visual Programming";

int charPos = name.IndexOf("s"); // charPos = 2

string lastName = name.Substring(7); //lastName = Programming

string lastName = name.Substring(7,4); //lastName = Prog

7- Insert( )
The responsible of this method is Insert the string or character in the specific
index.

Example

// Full string

string S = "University Baghdad";

string S1 = S.Insert(11, "of "); //S1 = University of Baghdad

3
8- Remove( )
This method deletes all the characters from the specific index to the end, or from
specific index to specified index position.

Example

// Full string

string S = "University Baghdad";

string S1 = S.Remove(11); //S1 = University

string S1 = S.Remove(11,4); //S1 = University dad

9- Replace( )
This method replaces the character with character, or replace sentence with
sentence

Example

// Full string

string S = "Good Luck in This Lecture";

string S1 = S.Replace("in", "IN"); // S1 = Good Luck IN This Lecture

string S1 = S.Replace("c", "C"); // S1 = Good LuCk in This LeCture

10- EndsWith( )
This EndsWith Method checks whether specified character is the last character
of string or not.

Example

// Full string

string S = "University of Baghdad";

string S1 = S.EndsWith(“Baghdad”).ToString() ; // S1 = True

string S1 = S.EndsWith(“Basra”).ToString() ; // S1 = False

4
11- StartsWith( )
It checks whether the first character of string is same as specified character.

Example

// Full string

string S = "University of Baghdad";

string S1 = S.StartsWith(“Baghdad”).ToString() ; // S1 = False

string S1 = S.StartsWith(“University”).ToString() ;// S1 = True

You might also like