in

DotNetSide

Dot Net South Italy Developers User Group

Tips

Invertire una stringa

Autore: Vito Arconzo

Questa funzione restituisce la stringa passata come parametro invertita.

1    Public Function ReverseString(ByVal stringToReverse As String) As String
2 ' dichiarazione variabili

3 Dim s As String = ""
4 Dim lunghezza As Integer = 0
5 If stringToReverse = "" Then
6 ' se stringa vuota restituiscila
7 s = ""
8 Else
9 ' altrimenti esegui l'inversione della stringa
10 lunghezza = stringToReverse.Length
11 For i As Integer = (lunghezza - 1) To 0 Step -1
12 s = String.Concat(s, stringToReverse.Substring(i, 1))
13 Next
14 End If
15 Return
(s)
16 End Function
 
Only published comments... Jun 09 2006, 09:55 AM by VitoA
Filed under:

Comments

 

Marcello said:

ciao Vito,

una semplice variante senza ciclo for-next

Public Function ReverseString(ByVal stringToReverse As String) As String
       If stringToReverse.Length > 0 Then
           Dim _arr As Byte() = System.Text.UTF8Encoding.UTF8.GetBytes(stringToReverse)
           Array.Reverse(_arr)
           Return System.Text.UTF8Encoding.UTF8.GetString(_arr)
       Else
           Return ""
       End If
   End Function
June 27, 2006 12:18 AM
Powered by Community Server (Commercial Edition), by Telligent Systems