From the previous chapters, we know that PowerShell stores everything in objects, and that these objects contain a series of instructions called methods. The default text is stored in a string object, which contains a number of very useful commands for working with text. For example, to determine the extension of a file, you can use LastIndexOf () to get the last character "." , continue using substring () to get the extension substring.
ps> $path = "C:\prefs.js"
ps> $path. Substring ($path. LastIndexOf (".") +1)
Js
Another approach, using the split method, splits the full name of the file, gets an array of strings, takes the last element, and PowerShell the last element in the array by index.
Ps> $path. Split (".") [-1]
Js
The following table gives all the methods for a string object:
Function |
Describe |
Example |
CompareTo () |
Comparison with another string |
("Hello"). CompareTo ("Hello") |
Contains () |
Whether to include a set substring |
("Hello"). Contains ("ll") |
CopyTo () |
Copy substring to new string |
$a = ("HelloWorld"). ToCharArray () ("user!"). CopyTo (0, $a, 6, 5) $a |
EndsWith () |
Whether to end a substring |
("Hello"). EndsWith ("Lo") |
Equals () |
Is the same as another string |
("Hello"). Equals ($a) |
IndexOf () |
Returns the index of the first match |
("Hello"). IndexOf ("L") |
IndexOfAny () |
Returns the first matching index of any character in a string |
("Hello"). IndexOfAny ("Loe") |
Insert () |
Inserts a string at the specified location |
("HelloWorld"). Insert (6, "brave") |
GetEnumerator () |
Enumerating all characters in a string |
("Hello"). GetEnumerator () |
LastIndexOf () |
The last matching position of the character |
("Hello"). LastIndexOf ("L") |
Lastindexofany () |
The last matching position of any character |
("Hello"). Lastindexofany ("Loe") |
PadLeft () |
Padded left padding is a string to a specified length |
("Hello"). PadLeft (10) |
PadRight () |
Right padding is a string to a specified length |
("Hello"). PadRight (a) + "world!" |
Remove () |
Removes the specified length starting at the specified location |
("Pstips"). Remove (2,2) |
Replace () |
Replace the specified string |
("Pstips"). Replace ("Ps", "Ps1″") |
Split () |
To cut a string with the specified delimiter |
("HelloWorld"). Split ("L") |
StartsWith () |
Whether to start with a specified substring |
("HelloWorld"). StartsWith ("he") |
Substring () |
To fetch a specified length substring from a specified location |
"HelloWorld"). Substring (4,3) |
ToCharArray () |
Convert to character array |
("HelloWorld"). ToCharArray () |
ToLower () |
Convert to lowercase |
("HelloWorld"). ToLower () |
Tolowerinvariant () |
Convert to lowercase with regional rules |
("HelloWorld"). Toupperinvariant () |
ToUpper () |
Convert to uppercase |
("HelloWorld"). ToUpper () |
Toupperinvariant () |
Convert to uppercase with regional rules |
("HelloWorld"). Toupperinvariant () |
Trim () |
Remove a space before and after a string |
("HelloWorld"). Trim () |
TrimEnd () |
Remove whitespace from end of string |
("HelloWorld"). TrimEnd () |
TrimStart () |
Remove a space from the beginning of a string |
("HelloWorld"). TrimStart () |
Chars () |
Returns the character at the specified position |
("Hello"). Chars (0) |
Taking the split () as an example to analyze the method
In the previous chapters, we already know that we can see through Get-member that an object contains methods that can be invoked. Just the most simple review to see the definition of split.
PS c:\> ("Jb51.net" | Get-member split). Definition
string[] Split (Params char[] separator), string[] Split (char[) separator, int count), String[] Split (char[] separator, system.stringsplitoptions options), string[] Split (char[) separator, int count, System.stringsplitoptions options), string[] Split (string[] separator, system.stringsplitoptions options), string[] Split (string[] Sepa
rator, int count, system.stringsplitoptions options)
The Define property can get a method parameter definition, but it is more readable than a pit dad. We still use the Replace method in the table above to replace the separator slightly to enhance readability.
PS c:\> ("Jb51.net" | Get-member Split). Definition. Replace ("),", ") ' n")
string[] Split (Params char[] separator)
string[] Split (char[] separator, int count)
String[] Split (char[] separator, system.stringsplitoptions options)
string[] Split (char[) separator, int count, System.stringsplitoptions options)
string[] Split (string[] separator, system.stringsplitoptions options)
String[] Split (string[] separator, int count, system.stringsplitoptions options)
Previously said inverted quotes, similar to the escape character backslash in a high-level language.
From the output above you can see that split has 6 different calling methods, which may have been used more than one parameter before. PowerShell when working with text, you may encounter multiple delimiters, and the split method call can be done only once.
PS c:\> "Http://www.jb51.net". Split (":./")
http
www
pstips
net
There is a blank in the middle, I can remove, stringsplitoptions light:
PS c:\> "Http://www.jb51.net". Split (":./", [stringsplitoptions]::removeemptyentries)
http
www
Pstips
Net
There is a small algorithm to remove the adjacent duplicate spaces in the string. Without considering the efficiency of the premise, you can use split first, split, and then the resulting elements to the specified separator splicing. However, the join method used for stitching is not part of a string object, but belongs to the string class, which is exactly what is said below.
Text and Regular Expressions
Original: http://www.jb51.net/string-object-methods.html