使⽤PowerShell将字符串拆分为数组
In this article, I am going to explain the PowerShell script to split a string into an array. Before I explain it, let me just give you a small introduction of the PowerShell and the PowerShell array.
在本⽂中,我将解释PowerShell脚本将字符串拆分为数组。 在解释它之前,让我先简要介绍⼀下PowerShell和PowerShell数组。
什么是Microsoft PowerShell? (What is Microsoft PowerShell?)
PowerShell is a command-line language provided by Microsoft. It is the replacement of the Windows batch scripting tool known as DOS. It is mainly designed for the IT administrators to automate and control the Windows operating systems and other applications.
PowerShell是Microsoft提供的⼀种命令⾏语⾔。 它替代了Windows批处理脚本⼯具DOS。 它主要是为IT管理员设计的,⽤于⾃动化和控制Windows操作系统和其他应⽤程序。
Following are the benefits of the PowerShell:
以下是PowerShell的优点:
1. It has a simple way to manipulate the operating system components and configurations
它具有操作操作系统组件和配置的简单⽅法
2. It is more secure than running VB and other application Scripts
它⽐运⾏VB和其他应⽤程序脚本更安全。
3. PowerShell allows complete access to the Microsoft.Net framework methods
PowerShell允许完全访问Microsoft.Net框架⽅法
PowerShell中的数组 (Arrays in PowerShell)
Arrays are the fixed-sized, sequentially ordered collection of the elements of any data types. It can be considered as the collection of the operators or variables. The arrays within the PowerShell can be processed using FOR loop and WHILE loop because all the elements in the array are of the same type, and the size of the array is known. The array in the PowerShell can be declared as follows:
数组是任何数据类型的元素的固定⼤⼩,顺序排序的集合。 可以将其视为运算符或变量的集合。 可以
使⽤FOR循环和WHILE循环来处理PowerShell中的数组,因为数组中的所有元素都是相同的类型,并且数组的⼤⼩是已知的。 PowerShell中的数组可以声明如下:
Array Syntax:
数组语法:
$Array=”Nisarg”, “Nirali”,”Dixit”,”Bharti”
$Array
Output:
输出:
Nisarg
Nirali
Dixit
Bharti
尼萨尔
尼拉⾥
迪克西特
巴蒂
See the following output:
请参阅以下输出:
In this article, I am going to explain a few methods, as well as the PowerShell string functions that are used to convert the string into an array. I have used the following functions of PowerShell.
在本⽂中,我将解释⼀些⽅法,以及⽤于将字符串转换为数组的PowerShell字符串函数。 我已经使⽤了PowerShell的以下功能。
1. .ToCharArray()
.ToCharArray()
2. .split()
。分裂()
.TocharArray()函数 (The .TocharArray() function)
The .TocharArray() function copies the characters of the string into the Unicode character array. .As I mentioned above, PowerShell can access all types of Microsoft.Net framework; hence we can use this function within the PowerShell script. Now, for example, we want to convert the string into the array of the word.
.TocharArray()函数将字符串的字符复制到Unicode字符数组中。 如上⽂所述,PowerShell可以访问所有类型的Microsoft.Net框架;因此我们可以在PowerShell脚本中使⽤此功能。 现在,例如,我们想将字符串转换为单词的数组。
Convert Input String in Char Array:
转换Char数组中的输⼊字符串:
$Inputstring ="SQL Server"
$CharArray =$InputString.ToCharArray()
$CharArray
Output
输出量
S
Q
L
S
e
r
v
e
r
⼩号
⼤号
⼩号
Ë
[R
v
Ë
[R
Following is the output of the script
以下是脚本的输出
As you can see, we store the input string in $Inputstring variable, convert the string into the character array by
using.TocharArray() function and store it in $CharArray variable and print each character within the array.
如您所见,我们将输⼊字符串存储在$ Inputstring变量中,使⽤.TocharArray()函数将字符串转换为
字符数组,然后将其存储在$ CharArray变量中,并打印数组中的每个字符。
.Split()函数 (The .Split() function)
The .Split() function splits the input string into the multiple substrings based on the delimiters, and it returns the array, and the array contains each element of the input string. By default, the function splits the string based on the whitespace characters like space, tabs, and line-breaks. If you want to split the string based on the specific character, then you must specify the character in the second argument. Following is the syntax of the .split() function.
的。 Split()函数根据定界符将输⼊字符串分成多个⼦字符串,并返回数组,该数组包含输⼊字符串的每个元素。 默认情况下,该函数根据空格字符(例如空格,制表符和换⾏符)分割字符串。 如果要根据特定字符分割字符串,则必须在第⼆个参数中指定字符。 以下
是.split()函数的语法。
For example, we want to split the following string into an array of multiple words by “-” character. Following is the code:例如,我们想⽤“-”字符将以下字符串分成多个单词的数组。 以下是代码:
Convert the input string into the array of the words:
将输⼊字符串转换为单词数组:
$Inputstring ="Microsoft-SQL-Server"
$CharArray =$InputString.Split("-")
$CharArray
Output:
输出:
Microsoft
SQL
shell代码
Server
微软
SQL
服务器
Following is the output of the script:
以下是脚本的输出:
The above code splits the string based on the “-” character and saves the output in the $CharArray variable.
上⾯的代码基于“-”字符分割字符串,并将输出保存在$ CharArray变量中。
例⼦1 (Example 1)
If you want to access the specific word or the character in the output array, you must use the index of the array. Please
note that the index of the array starts from 0. For example, in “Sonali Bhatt is Database Administrator” string, you want to print only “Sonali” and “Administrator” words. The index of the word “Sonali” within the array is zero, and the index of the word “Administrator” is four. The code should be written in the following way.
如果要访问输出数组中的特定单词或字符,则必须使⽤数组的索引。 请注意,数组的索引从0开始。例如,在“ Sonali Bhatt is Database Administrator ”字符串中,您只想打印“ Sonali ”和“ Administrator ”字样。 数组中单词“ Sonali ”的索引为零,单词“ 管理员”的索引为4。 该代码应以以下⽅式编写。
PowerShell Script:
PowerShell脚本:
$Inputstring ="Sonali Bhatt is Database Administrator"
$CharArray =$InputString.Split(" ")
$CharArray[0]
$CharArray[4]
Output:
输出:
Sonali
Administrator
索纳利
管理员
Following is the output:
以下是输出:
As you can see, we have assigned “Sonali Bhatt is a Database Administrator” string to $insputString variable, used .Split() function to split the string in multiple words and used index of the array to print the “Sonali” and “Administrator.”
如您所见,我们为$ insputString变量分配了“ Sonali Bhatt is a Database Administrator ”字符串,使⽤.Split()函数将字符串拆分为多个单词,并使⽤数组的索引打印“ Sonali ”和“ Administrator 。”
This method can be used to split the string into two separate variables. For example, if you want to save the word “Database” in the $String1 variable and “Administrator” in the $String2 variable, then you should write the code in the following way.
此⽅法可⽤于将字符串分成两个单独的变量。 例如,如果你想保存在$ String2的变量在$ String1中变量单词“ 数据库 ”和“ 管理员”,那么你应该写在下⾯的⽅法的代码。
PowerShell Script:
PowerShell脚本:
$Inputstring ="Sonali Bhatt is Database Administrator"
$CharArray =$InputString.Split(" ")
$String1= $CharArray[0]
$String2= $CharArray[4]
write-host $String1
write-host $String2
Output:
输出:
Sonali
Administrator
索纳利
管理员
See the following image:
见下图:
例⼦2 (Example 2)
If you want to convert the string into an array of the multiple substrings or words using the .split() function, then you must specify the additional argument named “number of the substring.” For example, we want to convert the string ”Nisarg, Nirali and Sonali are Database Administrators” in the array of the three substrings, and the entire string must be split with “,” delimiter then the code should be written in the following way:
如果要使⽤.split()函数将字符串转换为多个⼦字符串或单词的数组,则必须指定名为“ ⼦字符串编号 ”的附加参数。 例如,我们要在三个⼦字符串的数组中转换字符串“ Nisarg,Nirali和Sonali是Database Administrators ”,并且必须⽤“,”定界符来分隔整个字符串,然后应按以下⽅式编写代码:
PowerShell Script:
PowerShell脚本:
$string = "Nisarg,Nirali,Sonali are Database Administrators"
$array= $string.Split(",")
$array[0]
$array[1]
$array[2]
Output:
输出:
Nisarg Nirali Sonali are Database Administrators
Nisarg Nirali Sonali是数据库管理员
Following is the output:
以下是输出:
例⼦3 (Example 3)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。