String对象的方法

时间:2021-07-17 20:18:14

Function

Description

Example

CompareTo()

Compares one string to another

("Hello").CompareTo("Hello")

Contains()

Returns "True" if a specified comparison string is in a string or if the comparison string is empty

("Hello").Contains("ll")

CopyTo()

Copies part of a string to another string

$a = ("Hello World").toCharArray()
("User!").CopyTo(0, $a, 6, 5)
$a

EndsWith()

Tests whether the string ends with a specified string

("Hello").EndsWith("lo")

Equals()

Tests whether one string is identical to another string

("Hello").Equals($a)

IndexOf()

Returns the index of the first occurrence of a comparison string

("Hello").IndexOf("l")

IndexOfAny()

Returns the index of the first occurrence of any character in a comparison string

("Hello").IndexOfAny("loe")

Insert()

Inserts new string at a specified index in an existing string

("Hello World").Insert(6, "brave ")

GetEnumerator()

Retrieves a new object that can enumerate all characters of a string

("Hello").GetEnumerator()

LastIndexOf()

Finds the index of the last occurrence of a specified character

("Hello").LastIndexOf("l")

LastIndexOfAny()

Finds the index of the last occurrence of any character of a specified string

("Hello").LastIndexOfAny("loe")

PadLeft()

Pads a string to a specified length and adds blank characters to the left (right-aligned string)

("Hello").PadLeft(10)

PadRight()

Pads string to a specified length and adds blank characters to the right (left-aligned string)

("Hello").PadRight(10) + "World!"

Remove()

Removes any requested number of characters starting from a specified position

("Hello World").Remove(5,6)

Replace()

Replaces a character with another character

("Hello World").Replace("l", "x")

Split()

Converts a string with specified splitting points into an array

("Hello World").Split("l")

StartsWith()

Tests whether a string begins with a specified character

("Hello World").StartsWith("He")

Substring()

Extracts characters from a string

("Hello World").Substring(4, 3)

ToCharArray()

Converts a string into a character array

("Hello World").toCharArray()

ToLower()

Converts a string to lowercase

("Hello World").toLower()

ToLowerInvariant()

Converts a string to lowercase using casing rules of the invariant language

("Hello World").toLowerInvariant()

ToUpper()

Converts a string to uppercase

("Hello World").toUpper()

ToUpperInvariant()

Converts a string to uppercase using casing rules of the invariant language

("Hello World").ToUpperInvariant()

Trim()

Removes blank characters to the right and left

(" Hello ").Trim() + "World"

TrimEnd()

Removes blank characters to the right

(" Hello ").TrimEnd() + "World"

TrimStart()

Removes blank characters to the left

(" Hello ").TrimStart() + "World"

Chars()

Provides a character at the specified position

("Hello").Chars(0)