×

C# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and many more.
In C#, using String and string doesn't make any difference. both are the same except that the thing that String is a class while string is a keyword which is an alias for System.String class.

Defining String:

 

In C#, String can be defined by two ways:

  1. By using new keyword:
  2. By string literal

By using New keyword:

char[] ch={ 'C ', 'S ', 'H ', 'A ', ' R', ' P'}  //array of characters

string s=new String(ch);

By String Literal:

String s= "CSHARP "

C# String Example: Getting the string from the user

 

using System;
public class StringExample
{
            public static void Main (string[] args)
            {
                        Console.WriteLine("Enter The name?");
                        string name=Console.ReadLine();
                        Console.WriteLine("Your Name is "+name);
            }
}

Output

Enter The name?
Vishal
Your Name is Vishal

C# String Example: using new keyword

using System;
public class StringExample
{
            public static void Main (string[] args)
            {
                        char[] ch={'v','i','s','h','a','l'};
                        string s=new string(ch);
                        Console.WriteLine("Character Array converted to string");
                        Console.WriteLine(s);                                            
            }
}

Output

Character Array converted to string
vishal 

C# String class Methods:

C# String class provides various methods to deal with the string related operations.

 
Method Name Description
Clone() it makes the exact copy of the string
Compare(String,String) Compares two strings. Returns an integer which indicates their relative position in sort order
CompareOrdinal(String,String) Compare two string by comparing their relative char object numerically.
CompareTo(String) It is used to compare this instance with a specified String object. It indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified string.
Concat(string,string) It is used to concatenate two specified instances of String.
Contains(string) It checks whether the string object contains specified string. Returns Boolean value.
Copy(string) It makes the copy of the specified string.
CopyTo(Int32, Char[], Int32, Int32) It is used to copy a specified number of characters from a specified position in this instance to a specified position in an array of Unicode characters.
EndsWith(string) It is used to check that the end of this string instance matches the specified string.
Equals(string,string) It is used to determine that two specified String objects have the same value.
Format(string,Object) It is used to replace one or more format items in a specified string with the string representation of a specified object.
GetEnumerator() It is used to retrieve an object that can iterate through the individual characters in this string.
GetType() It is used to get the Type of the current instance.
GetHashCode() It returns the hash code for this string.
GetTypeCode() It is used to return the TypeCode for class String.
IndexOf(String) It is used to report the zero-based index of the first occurrence of the specified string in this instance.
Insert(Int32,String) It is used to return a new string in which a specified string is inserted at a specified index position.
Intern(String) It is used to retrieve the system's reference to the specified String.
IsInterned(String) It is used to retrieve a reference to a specified String.
IsNormalized() It is used to indicate that this string is in Unicode normalization form C.
IsNullOrEmpty(string) It is used to indicate that the specified string is null or an Empty string.
IsNullOrWhiteSpace(string) It is used to indicate whether a specified string is null, empty, or consists only of white-space characters.
Join(string,string[]) It is used to concatenate all the elements of a string array, using the specified separator between each element.
LastIndexOf(char) It is used to report the zero-based index position of the last occurrence of a specified character within String.
LastInsexOfAny(char[]) It is used to report the zero-based index position of the last occurrence in this instance of one or more characters specified in a Unicode array.
Normalize() It is used to return a new string whose textual value is the same as this string, but whose binary representation is in Unicode normalization form C.
PadLeft(int32) It is used to return a new string that right-aligns the characters in this instance by padding them with spaces on the left.
PadRight(int32) It is used to return a new string that left-aligns the characters in this string by padding them with spaces on the right.
Remove(int32) It is used to return a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.
Replace(string,string) It is used to return a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
Split(char[]) It is used to split a string into substrings that are based on the characters in an array.
Startswith(string) It is used to check whether the beginning of this string instance matches the specified string.
Substring(int32) It is used to retrieve a substring from this instance. The substring starts at a specified character position and continues to the end of the string.
ToCharArray() It is used to copy the characters in this instance to a Unicode character array.
ToLower() It is used to convert String into lowercase.
ToLowerInvarient() It is used to return convert String into lowercase using the casing rules of the invariant culture.
ToUpper() It is used to convert String into uppercase.
ToString() It is used to return instance of String.
TrimEnd(char[]) It Is used to remove all trailing occurrences of a set of characters specified in an array from the current String object.
TrimStart[char[]) It is used to remove all leading occurrences of a set of characters specified in an array from the current String object.

Related Topics

C# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

1 minute read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

Ref and Out in C#

 Ref in c# The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways; In a programming code, in that...

4 minutes read.

C# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used...

1 minute read.

C# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 minutes read.

C# Access Modifiers

C# provides various keywords to control or restrict accessibility or scope of the data. We can apply access modifiers to functions and variables. There are five access modifiers in C#: Private ...

10 minutes read.

C# Command Line Arguments

In C#, Arguments to the main method can be sent by command line. Those arguments are called command line arguments. The string[] args variable holds the command line arguments Let's see...

1 minute read.

C# Destructor

Destructors works in the opposite way. They basically destruct the objects. We can’t send parameter in a destructor. We also can’t apply any modifier to the destructor. Likewise constructor, destructor...

1 minute read.

C# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.C# Jagged Arrays...

3 minutes read.

C# Jump Statements

Break is a jump statement which is used to break the current loop or current flow of program and transfer the program control to the next block. It is used...

3 minutes read.

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes read.

C# If Statements

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false...

2 minutes read.

C# for Loop

For loop is used to iterate a part of the code multiple times.  We must use for loop instead of using while and do while if the number of iteration...

4 minutes read.

C# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain...

3 minutes read.

C# Aggregation

In C#, Aggregation Represents Has-A Relationship between two objects. A field of the class defines another class to reuse this in the form of association. The class is used as...

3 minutes read.

C# Method Overriding

Method overriding is the mechanism where derived class defines the same method as parent class with more functionality in the chilled class. It provides runtime polymorphism in C#. It provides...

1 minute read.

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 minutes read.

C# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

3 minutes read.