Skip to content

C# System.String API

C# String — an immutable sequence of UTF-16 code units, the primary text type in .NET.

1 class · 8 methods

String

8 methods

Represents text as a sequence of UTF-16 code units. String is immutable and reference type.

String.Length -> int

Gets the number of characters in the current String object (UTF-16 code units).

Returns

int

Example

csharp
string s = "hello";
int len = s.Length;  // 5
String.Substring(int startIndex, int length) -> string

Retrieves a substring from this instance starting at startIndex with the specified length.

Parameters

NameTypeDescription
startIndexintZero-based starting character position.
lengthintNumber of characters to take.

Returns

string

Example

csharp
string s = "Hello, World";
string sub = s.Substring(7, 5);  // "World"
String.IndexOf(string value) -> int

Reports the zero-based index of the first occurrence of value, or -1 if not found.

Parameters

NameTypeDescription
valuestringString to seek.

Returns

int

Example

csharp
string s = "Hello, World";
int i = s.IndexOf("World");  // 7
int j = s.IndexOf("xyz");    // -1
String.Replace(string old, string new) -> string

Returns a new string in which all occurrences of old are replaced with new.

Parameters

NameTypeDescription
oldstringString to be replaced.
newstringString to replace all occurrences of old.

Returns

string

Example

csharp
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

NameTypeDescription
separatorchar[]Character array that delimits substrings.

Returns

string[]

Example

csharp
string s = "a,b,c";
string[] parts = s.Split(',');
// parts == ["a", "b", "c"]
String.ToUpper() -> string

Returns a copy of this string converted to uppercase using the current culture's casing rules.

Returns

string

Example

csharp
string s = "hello";
string upper = s.ToUpper();  // "HELLO"
String.Trim() -> string

Removes all leading and trailing white-space characters from the current string.

Returns

string

Example

csharp
string s = "  hello  ";
string t = s.Trim();  // "hello"
String.Concat(string a, string b) -> string

Concatenates two specified string instances.

Parameters

NameTypeDescription
astringFirst string to concatenate.
bstringSecond string to concatenate.

Returns

string

Example

csharp
string r = string.Concat("Hello, ", "World");  // "Hello, World"