Position of strings

String positions

Page 1 of 1

6 Replies - 800 Views - Last Post: 02 April 2009 - 08:36 AM Rate Topic: -----

#1 mil1234  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 109
  • Joined: 01-February 09

Position of strings

Post icon  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
Is This A Good Question/Topic? 0
  • +

Replies To: Position of strings

#2 LemonMan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 170
  • Joined: 22-August 05

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


Was This Post Helpful? 1
  • +
  • -

#3 mil1234  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 109
  • Joined: 01-February 09

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.
Was This Post Helpful? 0
  • +
  • -

#4 krum110487  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Re: Position of strings

Posted 27 March 2009 - 06:17 AM

Dim newChar As Char = "b" 'This is the character that a will be replaced with. In this case, b.



instead of "b" use a textbox.text, but beware Char will give you an error if your textbox has more than one Character.

and if you want to pick the letter to be changed, change the other line above the one shown....

try stuff out before ya ask! it's the only way you will learn!
Was This Post Helpful? 0
  • +
  • -

#5 LemonMan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 170
  • Joined: 22-August 05

Re: Position of strings

Posted 28 March 2009 - 09:54 AM

View Postmil1234, 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


Was This Post Helpful? 1
  • +
  • -

#6 mil1234  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 109
  • Joined: 01-February 09

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

Was This Post Helpful? 0
  • +
  • -

#7 LemonMan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 170
  • Joined: 22-August 05

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



I don't know which of these you need, but just try both. Good luck! :D

P.S. sorry for responding late

This post has been edited by LemonMan: 02 April 2009 - 08:37 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1