Array de Strings em PowerShell
- Array de Strings no PowerShell
- Método 1: Declarar Array de String Usando Método Integrado
-
Método 2: Declarar Array de String Usando
System.Collections.Arraylist
- Obter o Comprimento do Array de Strings
- Obter o Tipo de Array de Strings
- Adicionar Strings ao Array de Strings
- Encontrar uma String Dentro de um Array de Strings
- Mudar o Caso das Strings Dentro do Array de Strings
- Obter Todos os Métodos Existentes para um Array de Strings

Este tutorial demonstra como criar um array de strings no PowerShell.
Array de Strings no PowerShell
Existem diferentes métodos para declarar um array de strings no PowerShell. Este tutorial demonstra diferentes métodos para declarar um array de strings e como acessar o array de strings.
Método 1: Declarar Array de String Usando Método Integrado
O primeiro método é declarar um array de strings pelo método integrado do PowerShell, onde declaramos um array de strings em uma linha. Veja o comando:
$DemoArray = @("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
echo $DemoArray
O código acima criará um array de strings e, em seguida, o imprimirá. Veja a saída:
This is Delftstack1
This is Delftstack2
This is Delftstack3
This is Delftstack4
Método 2: Declarar Array de String Usando System.Collections.Arraylist
O System.Collections.Arraylist
é uma classe do PowerShell usada para criar arrays e listas de arrays. Para usar essa classe, primeiro, precisamos criar um objeto dessa classe.
Veja os comandos:
New-Object -TypeName System.Collections.Arraylist
$DemoArray = [System.Collections.Arraylist]@("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
echo $DemoArray
O código acima criará um ArrayList
de strings usando a classe System.Collections.Arraylist
do PowerShell. Veja a saída:
This is Delftstack1
This is Delftstack2
This is Delftstack3
This is Delftstack4
Obter o Comprimento do Array de Strings
Como declaramos o array de strings na variável $DemoArray
, temos que usar o método Length
com a variável para obter esse comprimento. Veja o comando:
$DemoArray.Length
Certifique-se de executar este método Length
no método integrado. Veja a saída:
19
19
19
19
Usar este método Length
no array de strings System.Collections.Arraylist
mostrará o comprimento de cada string. Veja o exemplo:
New-Object -TypeName System.Collections.Arraylist
$DemoArray = [System.Collections.Arraylist]@("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
$DemoArray.Length
O código acima imprimirá o comprimento de cada string no array. Veja a saída:
19
19
19
19
Obter o Tipo de Array de Strings
Podemos usar o método GetType()
do PowerShell para obter um tipo de array. Vamos tentar obter o tipo de array de strings para ambos os métodos acima.
Método 1:
$DemoArray = @("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
$DemoArray.GetType()
O código acima obterá o tipo de array de strings usando o primeiro método. Veja a saída:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
Método 2:
$DemoArray = [System.Collections.Arraylist]@("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
$DemoArray.GetType()
O código acima obterá o tipo de array de strings usando o método System.Collections.Arraylist
. Veja a saída:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True ArrayList System.Object
O nome do tipo de dado é armazenado na coluna Name
, então para o primeiro método, o tipo de dado é Object
, e para o segundo método, o tipo de dado é ArrayList
. A coluna BaseType
mostra o tipo de dado no sistema.
Para um array, é sempre System.Array
, e para strings ou ArrayList
, será System.Object
.
Adicionar Strings ao Array de Strings
Adicionar ou anexar mais strings a um array de strings pode ser feito usando o operador de concatenação +=
. Por exemplo, se quisermos adicionar algumas strings a mais ao array, então usamos o seguinte comando:
$DemoArray += @("This is Delftstack5", "This is Delftstack6", "This is Delftstack7", "This is Delftstack8")
Vamos tentar um exemplo onde adicionamos mais strings ao array, verificamos o comprimento e imprimimos o array. Veja o exemplo:
$DemoArray = @("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
echo $DemoArray
$DemoArray += @("This is Delftstack5", "This is Delftstack6", "This is Delftstack7", "This is Delftstack8")
$DemoArray.Length
echo $DemoArray
O código acima criará um array de strings e imprimirá isso e, em seguida, adicionará quatro strings a mais, verificará o comprimento do array e, finalmente, imprimirá o array. Veja a saída:
This is Delftstack1
This is Delftstack2
This is Delftstack3
This is Delftstack4
8
This is Delftstack1
This is Delftstack2
This is Delftstack3
This is Delftstack4
This is Delftstack5
This is Delftstack6
This is Delftstack7
This is Delftstack8
Encontrar uma String Dentro de um Array de Strings
Encontrar uma string dentro de um array de strings no PowerShell é fácil. Precisamos usar o método Contains()
, onde o parâmetro será a string que queremos encontrar.
O método retornará true
se encontrar a string; caso contrário, retornará false
. Vamos tentar um exemplo com nosso array:
$DemoArray = @("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
$DemoArray.Contains("This is Delftstack1")
$DemoArray.Contains("This is Delftstack5")
O método procurará as strings dadas no método contains()
e retornará true
se encontrar a string e retornará false
se não conseguir. Veja a saída:
True
False
Mudar o Caso das Strings Dentro do Array de Strings
Também podemos mudar o caso das strings dentro do array de strings. Para esse propósito, usamos os métodos toLower()
e toUpper()
, que convertem o caso para minúsculas e maiúsculas, respectivamente.
Vamos tentar um exemplo:
$DemoArray = @("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
$DemoArray = $DemoArray.toLower()
echo $DemoArray
$DemoArray = $DemoArray.toUpper()
echo $DemoArray
Os comandos acima converterão a string do array para minúsculas e maiúsculas. Veja as saídas:
this is delftstack1
this is delftstack2
this is delftstack3
this is delftstack4
THIS IS DELFTSTACK1
THIS IS DELFTSTACK2
THIS IS DELFTSTACK3
THIS IS DELFTSTACK4
Obter Todos os Métodos Existentes para um Array de Strings
Podemos usar os métodos Get-Member
e -MemberType
para obter todos os métodos disponíveis para o array de strings. Para obter todos os métodos disponíveis para o array de string, execute o seguinte comando:
$DemoArray | Get-Member -MemberType Method
O código acima obterá todos os métodos disponíveis para o array $DemoArray
. Veja a saída:
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn FacebookArtigo relacionado - PowerShell Array
- Array de Bytes no PowerShell
- Arrays Multidimensionais do PowerShell
- Como Adicionar Objetos a um Array de Objetos no PowerShell
- Como Converter um Objeto de Array em uma String no PowerShell
- Como Criar um Array Vazio de Arrays no PowerShell