Array di stringhe in PowerShell
- Array di Stringhe PowerShell
- Metodo 1: Dichiarare Array di Stringhe Utilizzando il Metodo Integrato
-
Metodo 2: Dichiarare Array di Stringhe Utilizzando
System.Collections.Arraylist
- Ottenere la Lunghezza dell’Array di Stringhe
- Ottenere il Tipo di Array di Stringhe
- Aggiungere Stringhe all’Array di Stringhe
- Trovare una Stringa Dentro un Array di Stringhe
- Cambiare il Caso delle Stringhe Dentro l’Array di Stringhe
- Ottenere Tutti i Metodi Esistenti per un Array di Stringhe

Questo tutorial dimostra come creare un array di stringhe in PowerShell.
Array di Stringhe PowerShell
Ci sono diversi metodi per dichiarare un array di stringhe in PowerShell. Questo tutorial dimostra diversi metodi per dichiarare un array di stringhe e come accedere all’array di stringhe.
Metodo 1: Dichiarare Array di Stringhe Utilizzando il Metodo Integrato
Il primo metodo è dichiarare un array di stringhe tramite il metodo integrato di PowerShell, dove dichiariamo un array di stringhe in una sola riga. Vedi il comando:
$DemoArray = @("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
echo $DemoArray
Il codice sopra creerà un array di stringhe e poi lo stamperà. Vedi l’output:
This is Delftstack1
This is Delftstack2
This is Delftstack3
This is Delftstack4
Metodo 2: Dichiarare Array di Stringhe Utilizzando System.Collections.Arraylist
Il System.Collections.Arraylist
è una classe PowerShell utilizzata per creare array e liste array. Per utilizzare questa classe, prima dobbiamo creare un oggetto di questa classe.
Vedi i comandi:
New-Object -TypeName System.Collections.Arraylist
$DemoArray = [System.Collections.Arraylist]@("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
echo $DemoArray
Il codice sopra creerà un ArrayList
di stringhe utilizzando la classe System.Collections.Arraylist
di PowerShell. Vedi l’output:
This is Delftstack1
This is Delftstack2
This is Delftstack3
This is Delftstack4
Ottenere la Lunghezza dell’Array di Stringhe
Poiché abbiamo dichiarato l’array di stringhe nella variabile $DemoArray
, dobbiamo utilizzare il metodo Length
con la variabile per ottenere questa lunghezza. Vedi il comando:
$DemoArray.Length
Assicurati di eseguire questo metodo Length
sul metodo integrato. Vedi l’output:
19
19
19
19
Utilizzare questo metodo Length
sull’array di stringhe System.Collections.Arraylist
mostrerà la lunghezza di ciascuna stringa. Vedi l’esempio:
New-Object -TypeName System.Collections.Arraylist
$DemoArray = [System.Collections.Arraylist]@("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
$DemoArray.Length
Il codice sopra stamperà la lunghezza di ciascuna stringa nell’array. Vedi l’output:
19
19
19
19
Ottenere il Tipo di Array di Stringhe
Possiamo utilizzare il metodo GetType()
di PowerShell per ottenere un tipo di array. Proviamo a ottenere il tipo di array di stringhe per entrambi i metodi sopra.
Metodo 1:
$DemoArray = @("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
$DemoArray.GetType()
Il codice sopra otterrà il tipo di array di stringhe utilizzando il primo metodo. Vedi l’output:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
Metodo 2:
$DemoArray = [System.Collections.Arraylist]@("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
$DemoArray.GetType()
Il codice sopra otterrà il tipo di array di stringhe utilizzando il metodo System.Collections.Arraylist
. Vedi l’output:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True ArrayList System.Object
Il nome del tipo di dati è memorizzato nella colonna Name
, quindi per il primo metodo, il tipo di dati è Object
, e per il secondo metodo, il tipo di dati è ArrayList
. La colonna BaseType
mostra il tipo di dati nel sistema.
Per un array, è sempre System.Array
, e per stringa o ArrayList
, sarà System.Object
.
Aggiungere Stringhe all’Array di Stringhe
Aggiungere o appending più stringhe a un array di stringhe può essere fatto utilizzando l’operatore di concatenazione +=
. Ad esempio, se vogliamo aggiungere alcune stringhe in più all’array, allora utilizziamo il seguente comando:
$DemoArray += @("This is Delftstack5", "This is Delftstack6", "This is Delftstack7", "This is Delftstack8")
Proviamo un esempio in cui aggiungiamo più stringhe all’array, controlliamo la lunghezza e stampiamo l’array. Vedi l’esempio:
$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
Il codice sopra creerà un array di stringhe e lo stamperà, poi aggiungerà quattro stringhe in più, controllerà la lunghezza dell’array e infine stamperà l’array. Vedi l’output:
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
Trovare una Stringa Dentro un Array di Stringhe
Trovare una stringa dentro un array di stringhe in PowerShell è facile. Dobbiamo utilizzare il metodo Contains()
, dove il parametro sarà la stringa che vogliamo trovare.
Il metodo restituirà true
se trova la stringa; altrimenti, restituisce false
. Proviamo un esempio con il nostro array:
$DemoArray = @("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
$DemoArray.Contains("This is Delftstack1")
$DemoArray.Contains("This is Delftstack5")
Il metodo cercherà le stringhe fornite nel metodo contains()
e restituirà true
se trova la stringa e restituisce false
se non riesce a trovarla. Vedi l’output:
True
False
Cambiare il Caso delle Stringhe Dentro l’Array di Stringhe
Possiamo anche cambiare il caso delle stringhe dentro l’array di stringhe. Per questo scopo, utilizziamo i metodi toLower()
e toUpper()
, che convertono il caso in minuscolo e maiuscolo, rispettivamente.
Proviamo un esempio:
$DemoArray = @("This is Delftstack1", "This is Delftstack2", "This is Delftstack3", "This is Delftstack4")
$DemoArray = $DemoArray.toLower()
echo $DemoArray
$DemoArray = $DemoArray.toUpper()
echo $DemoArray
I comandi sopra convertiranno la stringa dell’array in lettere minuscole e maiuscole. Vedi gli output:
this is delftstack1
this is delftstack2
this is delftstack3
this is delftstack4
THIS IS DELFTSTACK1
THIS IS DELFTSTACK2
THIS IS DELFTSTACK3
THIS IS DELFTSTACK4
Ottenere Tutti i Metodi Esistenti per un Array di Stringhe
Possiamo utilizzare i metodi Get-Member
e -MemberType
per ottenere tutti i metodi disponibili per l’array di stringhe. Per ottenere tutti i metodi disponibili per l’array di stringhe, esegui il seguente comando:
$DemoArray | Get-Member -MemberType Method
Il codice sopra otterrà tutti i metodi disponibili per l’array $DemoArray
. Vedi l’output:
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 FacebookArticolo correlato - PowerShell Array
- Array di Byte in PowerShell
- Array multidimensionali di PowerShell
- Come aggiungere oggetti a un array di oggetti in PowerShell
- Come convertire un oggetto array in una stringa in PowerShell
- Come creare un array vuoto di array in PowerShell
Articolo correlato - PowerShell String
- Come controllare se un file contiene una stringa specifica utilizzando PowerShell
- Come controllare se una stringa non è NULL o VUOTA in PowerShell
- Come convertire un oggetto array in una stringa in PowerShell
- Come convertire una stringa sicura in testo normale in PowerShell
- Come estrarre una sottostringa PowerShell da una stringa