String
8 methodsRepresents text as a sequence of UTF-16 code units. String is immutable and reference type.
String.Length -> intGets the number of characters in the current String object (UTF-16 code units).
Returns
int
Example
string s = "hello";
int len = s.Length; // 5String.Substring(int startIndex, int length) -> stringRetrieves a substring from this instance starting at startIndex with the specified length.
Parameters
| Name | Type | Description |
|---|---|---|
| startIndex | int | Zero-based starting character position. |
| length | int | Number of characters to take. |
Returns
string
Example
string s = "Hello, World";
string sub = s.Substring(7, 5); // "World"String.IndexOf(string value) -> intReports the zero-based index of the first occurrence of value, or -1 if not found.
Parameters
| Name | Type | Description |
|---|---|---|
| value | string | String to seek. |
Returns
int
Example
string s = "Hello, World";
int i = s.IndexOf("World"); // 7
int j = s.IndexOf("xyz"); // -1String.Replace(string old, string new) -> stringReturns a new string in which all occurrences of old are replaced with new.
Parameters
| Name | Type | Description |
|---|---|---|
| old | string | String to be replaced. |
| new | string | String to replace all occurrences of old. |
Returns
string
Example
string s = "Hello, World";
string r = s.Replace("World", "C#"); // "Hello, C#"String.Split(char[] separator) -> string[]Splits the string into substrings based on the characters in separator.
Parameters
| Name | Type | Description |
|---|---|---|
| separator | char[] | Character array that delimits substrings. |
Returns
string[]
Example
string s = "a,b,c";
string[] parts = s.Split(',');
// parts == ["a", "b", "c"]String.ToUpper() -> stringReturns a copy of this string converted to uppercase using the current culture's casing rules.
Returns
string
Example
string s = "hello";
string upper = s.ToUpper(); // "HELLO"String.Trim() -> stringRemoves all leading and trailing white-space characters from the current string.
Returns
string
Example
string s = " hello ";
string t = s.Trim(); // "hello"String.Concat(string a, string b) -> stringConcatenates two specified string instances.
Parameters
| Name | Type | Description |
|---|---|---|
| a | string | First string to concatenate. |
| b | string | Second string to concatenate. |
Returns
string
Example
string r = string.Concat("Hello, ", "World"); // "Hello, World"