Visualizzazione dei risultati da 1 a 3 su 3

Discussione: splittare da destra C#

  1. #1
    Utente di HTML.it L'avatar di jet
    Registrato dal
    Dec 2002
    residenza
    Chieti
    Messaggi
    866

    splittare da destra C#

    Ho una stringa tipo: nome cognome email password con la possibilità però che il cognome sia assente.
    Come faccio a partire da destra per spittare con gli spazi?

  2. #2
    Moderatore di ASP.net L'avatar di djciko
    Registrato dal
    Nov 2002
    Messaggi
    6,887

    Re: splittare da destra C#

    Originariamente inviato da jet
    Ho una stringa tipo: nome cognome email password con la possibilità però che il cognome sia assente.
    Come faccio a partire da destra per spittare con gli spazi?
    codice:
    Dim ArrayStringhe() as string = StringaInformazioniUtente.Split(" ")
    poi fai un ciclo in ArrayStringhe da 0 ad Ubound(ArrayStringhe) e ricostruisci, "reincollando" i pezzi. Rozzo, ma dovrebbe andare

  3. #3
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    Edmondo De Amicis posta@tiscali.it xyzabc
    Edmondo Amicis posta@tiscali.it xyzabc
    Stefano Carlo Nicola De Amicis posta@tiscali.it xyzabc
    :quote:

    A parte il fatto che questa serializzazione è un poco discutibile, il codice di sotto parte dal presupposto che i campi siano:

    1° il nome (uno solo)
    2° opzionale cognome, semplice o composto
    3° penultimo campo, indirizzo
    4° ultimo campo, password

    codice:
    <%@ Page Language="VB" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        Protected Sub LinkButton_separa_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            Try
                Dim s As String = Me.TextBox_stringa.Text
                Dim a As String() = s.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
                
                If a.Length < 3 Then Throw New Exception("Errore: la stringa deve avere Nome, Cognome, Indirizzo, Password (cognome opzionale)")
    
                Me.Label_nome.Text = a(0)
                Me.Label_password.Text = a(a.Length - 1)
                Me.Label_indirizzo.Text = a(a.Length - 2)
                Me.Label_cognome.Text = cognome(a)
                
            Catch ex As Exception
                Me.Label_errore.Text = Server.HtmlEncode(ex.Message).Replace(vbNewLine, "
    ")
            End Try
            
        End Sub
        
    
        Private Function cognome(ByVal a As String()) As String
            Dim s As String = ""
            If a.Length = 3 Then Return s
            
            For i As Integer = 1 To a.Length - 3
                s &= " " & a(i)
            Next
            s = s.Substring(1)
            Return s
        End Function
        
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
        <style type="text/css">
            body { font-family: Verdana;}
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:Label ID="Label1" runat="server" AssociatedControlID="TextBox_stringa" Font-Bold="True" Text="Stringa da separare: "></asp:Label>
            <asp:TextBox ID="TextBox_stringa" runat="server" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" Width="329px">Edmondo De Amicis posta@tiscali.it xyzabc</asp:TextBox>
            <asp:LinkButton ID="LinkButton_separa" runat="server" OnClick="LinkButton_separa_Click">Separa</asp:LinkButton>
            
    
            
    
            <table>
                <tr>
                    <td>Nome:</td>
                    <td><asp:Label ID="Label_nome" runat="server" Text="" EnableViewState="false"></asp:Label></td>
                </tr>
                <tr>
                    <td>Cognome:</td>
                    <td><asp:Label ID="Label_cognome" runat="server" Text="" EnableViewState="false"></asp:Label></td>
                </tr>
                <tr>
                    <td>Indirizzo:</td>
                    <td><asp:Label ID="Label_indirizzo" runat="server" Text="" EnableViewState="false"></asp:Label></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><asp:Label ID="Label_password" runat="server" Text="" EnableViewState="false"></asp:Label></td>
                </tr>
            </table>
            <asp:Label ID="Label_errore" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="Red"></asp:Label>
        </form>
    </body>
    </html>
    Pietro

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.