Funzione StrComp

Restituisce un valore che indica il risultato di un confronto di stringhe.

Sintassi

StrComp(string1, string2[, compare])

Argomenti:

  • string1: obbligatorio. Qualsiasi espressione stringa valida.
  • string2: obbligatorio. Qualsiasi espressione stringa valida.
  • Compare: facoltativo. Valore numerico che indica il tipo di confronto da utilizzare per la valutazione delle stringhe. Se l'argomento viene omesso, viene eseguito un confronto binario. Per i valori, vedere la sezione Impostazioni.
  • Impostazioni: l'argomento compare può avere i seguenti valori.

    Tabella 11-21 Costanti di confronto e descrizioni

    Costante Valore Descrizione
    vbBinaryCompare 0 Esegue un confronto binario
    vbTextCompare 1 Esegue un confronto testuale

Valore restituito

La funzione StrComp restituisce i valori seguenti:

Tabella 11-22 Valori restituiti dalla funzione StrComp

Se La funzione StrComp restituisce
string1 è minore di string2 -1
string1 è uguale a string2 0
string1 è maggiore di string2 1

Note

Nell'esempio seguente viene utilizzata la funzione StrComp per restituire i risultati di un confronto di stringhe. Se il terzo argomento è 1, viene eseguito un confronto testuale; se il terzo argomento è 0 o omesso, viene eseguito un confronto binario. Se il terzo argomento viene omesso, viene eseguito il confronto predefinito del sistema.

Nell'esempio seguente viene illustrato l'uso della funzione StrComp.

Esempio 1

Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD": MyStr2 = "abcd"   ' Define variables.
MyComp = StrComp(MyStr2, MyStr1)   
' Output: 1

MyComp = StrComp(MyStr1, MyStr2, 1)   
' Output: 0
MyComp = StrComp(MyStr1, MyStr2, 0)   
' Output: -1

Esempio 2

Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD"
MyStr2 = "XYZ"
MyComp = StrComp(MyStr1, MyStr2)   
' Output: -1

Esempio 3

Dim MyStr1, MyStr2, MyComp
MyStr1 = "XYZ"
MyStr2 = "ABCD"
MyComp = StrComp(MyStr1, MyStr2)   
' Output: 1