Come controllare se una stringa è vuota in PowerShell
-
Controlla se una stringa è
NON NULLoVUOTAin PowerShell -
Alternative a
IsNullOrEmptyper controllare se una stringa è null o vuota in PowerShell -
Usa l’operatore
-eqper controllare se una stringa è null o vuota in PowerShell - Usa RegEx per controllare se una stringa è null o vuota in PowerShell
Discuteremo in questo articolo i metodi che possono essere utilizzati per controllare se una stringa data è null o vuota in Powershell.
Controlla se una stringa è NON NULL o VUOTA in PowerShell
IsNullOrEmpty è un metodo comune di scripting/linguaggio di programmazione per controllare se una stringa data è vuota o null. Un null è un valore di stringa che non è stato assegnato, e una stringa vuota è una stringa con " " o il dato String.Empty.
Alternative a IsNullOrEmpty per controllare se una stringa è null o vuota in PowerShell
C’è un modo semplice per fare l’equivalente della funzione IsNullOrEmpty di PowerShell. I seguenti segmenti di codice possono essere utilizzati.
La stringa data nel comando è null. Pertanto, l’output del codice è come di seguito.
Codice di esempio 1:
PS C:\Users\Test> $str1 = $null
PS C:\Users\Test> if ($str1) { 'not empty' } else { 'empty' }
Output:
empty
Se la stringa è vuota, l’output è ancora vuoto.
Codice di esempio 2:
PS C:\Users\Test> $str2 = ''
PS C:\Users\Test> if ($str2) { 'not empty' } else { 'empty' }
Output:
empty
Se la stringa non è vuota e non è null, l’output è non vuota.
Codice di esempio 3:
PS C:\Users\Test> $str3 = ' '
PS C:\Users\Test> if ($str3) { 'not empty' } else { 'empty' }
Output:
not empty
Ci sono comandi per confrontare due stringhe e controllare se due o più sono vuote.
PS C:\Users\Agni> if ($str1 -and $str2) { 'neither empty' } else { 'one or both empty' }
Output:
one or both empty
Inoltre, né vuoto è un possibile comparativo utilizzato sopra per confrontare due stringhe dichiarate. Questo può essere identificato come il metodo più chiaro e conciso di utilizzare IsNullOrEmpty.
Oltre al metodo sopra, il metodo statico IsNullOrEmpty può essere utilizzato anche in PowerShell.
Usa l’operatore -eq per controllare se una stringa è null o vuota in PowerShell
L’operatore -eq confronta due valori per uguaglianza. Puoi confrontare una stringa con una stringa vuota per controllare se è vuota.
Codice:
$str1 = ""
if ($str1 -eq "") {
Write-Host "String is empty"
}
else {
Write-Host "String is not empty"
}
Output:
String is empty
Usa RegEx per controllare se una stringa è null o vuota in PowerShell
I modelli di una stringa possono essere abbinati utilizzando le espressioni regolari. Puoi utilizzare un modello regex che corrisponde a stringhe vuote o contenenti solo spazi bianchi.
Codice:
$str4 = " "
if ($str4 -match "^\s*$") {
Write-Host "String is empty"
}
else {
Write-Host "String is not empty"
}
Output:
String is empty
Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.
Articolo correlato - PowerShell String
- Array di stringhe in PowerShell
- 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
