Answered Use a SubString or a indexof?

  • Sunday, April 01, 2012 9:48 PM
     
      Has Code

    Hi, 

    I have a text label, split half-and-half with a "/".

    This is an example:

    label1.text = "I like dogs/I like cats"	

    This will not be the same text (and not the same text length) every times. I need to delete everything before the "/" with only a button. (for example, delete "I like dogs")

    I already tried to make a SubString, and a indexOf, but it didn't work... 

    I realy need help,

    Thank you,


    Dominic



    • Edited by Chod3479 Sunday, April 01, 2012 9:49 PM
    • Edited by Chod3479 Sunday, April 01, 2012 9:50 PM
    •  

All Replies

  • Sunday, April 01, 2012 10:01 PM
     
     
    Show what you've already tried. :)

    Armin

  • Sunday, April 01, 2012 10:10 PM
     
     Answered Has Code
    Dim myString As String = "I like dogs/I like cats"
    Dim newString As String
    
    newString = myString.Substring(myString.IndexOf("/") + 1)

    Something like that.

    Best regards,

    - Jordan


    Jordan St. Godard| Microsoft® Community Contributor 2011




  • Sunday, April 01, 2012 10:11 PM
     
      Has Code

    I found this code in some forums, but they didn't work..

    And the label1.text was: "I like dogs/I like cats"

            Dim s As String = Label1.Text
            Dim i As Integer = s.IndexOf("/"c)
            Do While (i <> -1)
                Label2.Text = (s.Substring(i))
                i = s.IndexOf("/"c, i + 1)
            Loop
            Label2.Text = i

    and also try this: 

            Dim s As String = label1.text
    
            Dim i As Integer = s.IndexOf("/"c)
    
            Dim part As String = s.Substring(i)
            Label2.Text = part
    Label2 should be the result.  (I should get: "I like cats")


    Dominic



    • Edited by Chod3479 Sunday, April 01, 2012 10:11 PM
    • Edited by Chod3479 Sunday, April 01, 2012 10:13 PM
    •  
  • Sunday, April 01, 2012 10:15 PM
     
     Answered
    You're almost there. In the second version use i+1 instead of only i. As the index of the "/" is in i, the part after it is starts at i + 1.

    Armin

    • Proposed As Answer by Jordan St. Godard Sunday, April 01, 2012 10:16 PM
    • Marked As Answer by Chod3479 Sunday, April 01, 2012 10:40 PM
    •  
  • Thursday, May 24, 2012 9:52 PM
     
     

    Ok this is good! Thank you!

    But now, if I want to only keep "I like dogs" and delete "/I like cats"

    Is it possible? 


    Dominic

  • Thursday, May 24, 2012 11:18 PM
     
     Answered Has Code

    Hi Dominic,

    sure, then use

    Dim part As String = s.Substring(0, i)
    Label2.Text = part


    Armin

    • Marked As Answer by Chod3479 Friday, May 25, 2012 12:54 AM
    •