Change a string, one char at a time.

newguy

Well-known member
Joined
Jun 30, 2008
Messages
611
Location
Denver Co, USA
Programming Experience
1-3
How do you take a string and strip the first character and place it at the end of the same string?
 
It's three operations. First, you need to index the string, just like an array, to get the first character. Next, you need to call Substring and specify the start index as 1, so you get everything starting at the second character. Finally, you need to concatenate those two result together, which you do in exactly the same way as you join any other strings, i.e. using the & operator. Armed with that knowledge, why don't you give it a go and show us what you come up with?
 
How about:

Dim txt as string = textbox1.text
Dim result as string = txt.substring(1) & txt(0)
textbox1.text = result

I put this into a tick event and it looks like it scrolls, I couldn't get it right with all the elements you were talking about, string manipulation is not one of my strong points. Would still like to see your version...
 
Um, your version is my version. I said:
First, you need to index the string, just like an array, to get the first character.
You did:
VB.NET:
txt(0)
I said:
Next, you need to call Substring and specify the start index as 1, so you get everything starting at the second character.
You did:
VB.NET:
txt.substring(1)
I said:
Finally, you need to concatenate those two result together, which you do in exactly the same way as you join any other strings, i.e. using the & operator.
You did:
VB.NET:
txt.substring(1) & txt(0)
You did exactly what I said. I can't give you full marks if you didn't know that's what you were doing though. ;)
 
Sorry I thought you wanted me to use the Join method in there, my bad...
Either way I am still aa learnin'...
Waz I being graded on this...:D

Close counts with:
Horseshoes,
Handgranades, and
Nuclear Missiles!:eek:
 
Last edited:
And consequently, the opposite direction would be:

VB.NET:
  Dim txt As String = textbox1.Text
        Dim txtindex As String = txt(txt.Count - 1)
        txt = txt.Remove(txt.Count - 1, 1)
        Dim result As String = txt(txt.Count - 1) & txt
        textbox1.Text = result

How about that?
 
Back
Top