![]() |
VOOZH | about |
In C#, a string is a sequence of Unicode characters (U+0000 to U+FFFF) used to represent text. It is an object of the System.String class. The keyword string and the class name String are both aliases for System.String, so they can be used interchangeably.
Example:
// creating the string using string keyword
string s1 = “GeeksforGeeks”;// creating the string using String class
String s2 = “GFG”;// creating the string using String class
System.String s3 = “Pro Geek”;
System.String (defined in the .NET base class library) is a collection of char objects. The maximum length of a string depends on system memory and runtime limitations rather than a fixed size. Strings are immutable, meaning their value cannot be changed after creation.
Name: Geek Id: 33 Marks: 97 Rank: 1
A string can be read out from the user input. The Console.ReadLine() method is used to read a string from user input. Example:
Input:
Hello Geeks !
Output:
User Entered: Hello Geeks !
Method | Syntax / Example |
|---|---|
Create a string from a literal | string str = "GeeksforGeeks"; |
Create a string using concatenation | string str = str1 + "data"; |
Create a string using a constructor | // Create a string from a character array |
Create a string using a property or a method | // start and end are the index for str index |
Create a string using formatting | string str = string.Format("{0} {1} Cars color " + "are {2}", no.ToString(), cname, clr); |
Example:
Method 1: Geeks Method 2: GeeksofGeeks Method 3: GEEKS Method 4: For Method 5: Addition of 1 with 2 is 3
There are multiple String Operations which we can perform in String in C#. Let us demonstrate the operations using the example as mentioned below:
Example 1: For performing string operation of interpolation
GeeksforGeeks is the Organisation Name. Length: 39
Example 2: For performing trim, replace and concatenate operation
Element at index 2: E GeeksforGeeks
In the above two examples, we explored several string methods. Below is a list of commonly used string methods in C#.
Method | Description | Return Type | Example |
|---|---|---|---|
| Finds the index of the first occurrence of a specified character or substring. | Integer | text.IndexOf("World"); | |
| Checks if a string starts with a specified substring. | Boolean | text.StartsWith("Hello"); | |
| Checks if a string ends with a specified substring. | Boolean | text.EndsWith("World!"); | |
| Converts a string to uppercase. | String | text.ToUpper(); | |
| Converts a string to lowercase. | String | text.ToLower(); | |
| Splits a string into an array based on a specified delimiter. | String Array | fruits.Split(','); | |
| Combines an array of strings into a single string with a specified delimiter. | String | string.Join(" - ", fruitArray); | |
| Checks if a string contains a specified substring. | Boolean | text.Contains("World"); | |
| Pads a string with spaces or a specified character to a certain length. | String | text.PadLeft(20, '*'); | |
| Pads a string on the right with spaces or a specified character to a certain length. | String | text.PadRight(20, '*'); | |
| Removes characters from a string starting at a specified index. | String | text.Remove(5, 7); | |
| Inserts a string at a specified index. | String | text.Insert(5, " Beautiful"); | |
| Removes leading and trailing whitespaces. | String | text.Trim(); | |
| Replaces occurrences of a substring with another substring. | String | text.Replace("fun", "awesome"); |
We can also create the array of string and assigns values to it. The string arrays can be created as follows:
String [] array_variable = new String[Length_of_array]
value at Index position 0 is Geeks value at Index position 1 is For value at Index position 2 is Geeks
| Aspects | string (Keyword) | System.String (Class) |
|---|---|---|
| Definition | Alias for System.String. | Fully qualified class name in .NET. |
| Performance | No difference in performance. | No difference in performance. |
| Usage | Commonly used for declaring variables, fields and properties. | Used for accessing static methods or fully qualifying types. |
| Ease of Use | Provides a shorthand for writing code. | More verbose but functionally identical to string. |
| Accessing Methods | Methods are accessed via the System.String class. | Static methods like String.Substring, String.IndexOf, etc., are accessed directly. |
| Keyword or Class | C# keyword. | .NET class. |
Note: In .NET, the text is stored as a sequential collection of the Char objects so there is no null-terminating character at the end of a C# string. Therefore a C# string can contain any number of embedded null characters (‘\0’).