使用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:

请参阅以下输出:

PowerShell Array

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

以下是脚本的输出

PowerShell script to split string into an array using .ToCharArray()

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
Server

微软
SQL
服务器

Following is the output of the script:

以下是脚本的输出:

PowerShell script to split string into an array using .split()

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:

以下是输出:

PowerShell script to save the output of array in variable

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:

见下图:

PowerShell script to save the output of array in variable

例子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:

以下是输出:

PowerShell script to split string into array: Array of Multiple substrings

例子3 (Example 3)

If you want to convert the string into an array of the multiple characters based on the arbitrary characters, then you must specify “\d” OR “\D”. If you specify “\d”, then it returns the array of the characters, and if you specify “\D” then, it returns the array of the numbers. For example, if we want to extract the “1987” from the “b1i9r8t7h” input string, then the code should be written as follows:

如果要将字符串转换为基于任意字符的多个字符组成的数组,则必须指定“ \ d ”或“ \ D ”。 如果指定“ \ d ”,则返回字符数组,如果指定“ \ D”,则返回数字数组。 例如,如果我们要从“ b1i9r8t7h ”输入字符串中提取“ 1987 ”,则代码应编写如下:

PowerShell Script:

PowerShell脚本:

$string = "b1i9r8t7h"
$array= $string -split "\D"
$array

Output:

输出:

1
9
8
7

1个
9
8
7

Following is the output:

以下是输出:

PowerShell script to split string into array: Array of numbers from arbitrary string

If you want to extract “birth” then the code should be written as following:

如果要提取“出生”,则代码应编写如下:

PowerShell Script:

PowerShell脚本:

$string = "b1i9r8t7h"
$array= $string -split "\d"
$array

Output:

输出:

b
i
r
t
h

b
一世
[R
Ť
H

Following is the output:

以下是输出:

PowerShell script to split string into array: Array of characters from arbitrary string

摘要 (Summary)

In this article, I have explained the PowerShell scripting and array variable in PowerShell. Additionally, I also covered PowerShell scripts to split a string into an array using .toCharArray() and .split() functions.

在本文中,我已经解释了PowerShell中的PowerShell脚本数组变量。 此外,我还介绍了PowerShell脚本,这些脚本使用.toCharArray()和.split()函数将字符串拆分为数组。

翻译自: https://www.sqlshack.com/powershell-split-a-string-into-an-array/

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值