String positions

Position of strings

Posted 26 March 2009 - 02:42 PM

Hi guys,

i have a problem in trying to solve out how i can find the position of a letter(let's say in a textbox of 50 letters) so then when its position is found i can change that letter by the position??i tried the indexOf method but if someone can help me in achieving this i 'll be really grateful as its hours i'm trying out

Replies To: Position of strings

Re: Position of strings

Posted 26 March 2009 - 03:30 PM

Here is the code:

Dim intFirst As Integer
intFirst = TextBox1.Text.IndexOf("a") 'This finds the first occurence of the character a
Dim newChar As Char = "b" 'This is the character that a will be replaced with. In this case, b.
Dim strBuild As String = ""
For intPos As Integer = 0 To (TextBox1.Text.Length - 1)
If (Not intPos = intFirst) Then
strBuild &= TextBox1.Text(intPos)
Else
strBuild &= newChar
End If
Next
TextBox1.Text = strBuild

Re: Position of strings

Posted 27 March 2009 - 01:12 AM

Tnx mate for that as it was useful,,but is it possibile so that instead of changing a letter with a letter,,i give a position and the letter changes in the textbox--for eg position 5 and the letter in position 5 (in textbox)changes with what i want it.

Re: Position of strings

Posted 28 March 2009 - 09:54 AM

mil1234, on 27 Mar, 2009 - 01:12 AM, said:

Tnx mate for that as it was useful,,but is it possibile so that instead of changing a letter with a letter,,i give a position and the letter changes in the textbox--for eg position 5 and the letter in position 5 (in textbox)changes with what i want it.

Yea, just set intIndex manually like this:

Dim intIndex As Integer = 5 'This is the index you select
Dim newChar As Char = "b" 'This is the character that a will be replaced with. In this case, b.
Dim strBuild As String = ""
For intPos As Integer = 0 To (TextBox1.Text.Length - 1)
If (Not intPos = intIndex) Then
strBuild &= TextBox1.Text(intPos)
Else
strBuild &= newChar
End If
Next
TextBox1.Text = strBuild

Re: Position of strings

Posted 29 March 2009 - 02:28 PM

Hi Lemon Man,

with the code below i went very close to solve my problem as i m entering the message i need and put it into the block of random letters....but to enter eacl letter were appropriate i can't with indexOf.is this the problem or i'm doing s'thing wrong;;;;eg;the message is 'Hi mate u ok'---this msg i m entering as a whole in the block but not like i need eg:
fhrjgfhjhgjhfjghjgbfgbfgbfdjgbdsfnvbdfgbhrfbgnfgbvfgbvfngvbfnvfdsbvnfsv

and H goes in pos5, i in pos12 etc etc......any help is greatly appreciated man---below is what i did till now

Dim intIndex As Integer = 5 'This is the index where my msg(fully will go)
Dim newChars As String 'this is the variable my msg is stored in
newChars = txtEnterMsg.Text
Dim strBuild As String = ""
For intPos As Integer = 0 To (txtLetters.Text.Length - 1)
If (Not intPos = intIndex) Then
strBuild &= txtLetters.Text(intPos)
Else
strBuild &= newChars
End If
Next
txtLetters.Text = strBuild
End Sub
End Class

Re: Position of strings

Posted 02 April 2009 - 08:36 AM

Try this:

Dim reps As New Dictionary(Of Integer, String)
'For each substituion, add a value to reps like this: reps.Add(4,"n"). In this case, n will be placed in position 4. So lets do that now:
reps.Add(4,"n")
Dim strBuild As String = ""
For intPos As Integer = 0 To (TextBox1.Text.Length - 1)
If reps.ContainsKey(intpos) Then
strBuild &= TextBox1.Text(intPos)
Else
strBuild &= apps(intPos)
End If
Next
TextBox1.Text = strBuild

This is one option. If you need to subsitute multiple letters for each occurence of another, try this:

Dim reps As New Dictionary(Of String, String)
'For each substituion, add a value to reps like this: reps.Add("a","n"). In this case, n's will be subsituted for a's. So lets do that now:
reps.Add("a","n")
Dim strBuild As String = ""
For intPos As Integer = 0 To (TextBox1.Text.Length - 1)
If reps.ContainsKey(TextBox1.Text(insPos)) Then
strBuild &= TextBox1.Text(intPos)
Else
strBuild &= apps(TextBox1.Text(intPos))
End If
Next
TextBox1.Text = strBuild