Replacing characters in a string

About Basics Of Visual Basic 6

Page 1 of 1

9 Replies - 9899 Views - Last Post: 06 September 2007 - 09:25 AM Rate Topic: -----

#1 bharath2yk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-June 07

Replacing characters in a string

Posted 28 August 2007 - 11:57 AM

Hey , I am in small prob guys , Need your help.

Prob:

I am developing a program which is GUI on VB6.

It has 2 text boxes and 1 command button .

Some text is entered into the first text box , If i click on command button, that text must be appear on to the second box with different format ..

Eg: I have entered Wow in first text box , as soon as i click on command button , this text must appear in second text box as &#& That means here "W" is replaced by "&" and "o" is replaced by "#"


I tried to do this using following code:

  Private Sub cmdreplace_click()

	txtOutput = Replace(txtInput.Text, "W", "&")

	End Sub


If i do this , I can change only letter W as & .. But i cant change o ..

Even i tried to do this :

  Private Sub cmdreplace_click()

	  txtOutput = Replace(txtInput.Text, "W", "&")
	  txtOutput = Replace(txtInput.Text, "o", "#")

	End Sub


If i do this , yes it is getting executed but not whole stuff ..
Only last part is getting executed .. That is , "o" is replaced by "#" but "W" is not replaced by "&"

I need to replace each and every word in whole sentence ..
Please give me few hints to do this ...
Hope u guys will help me ..
Tc ..

MOD EDIT: modified title ~ jayman9

Is This A Good Question/Topic? 0
  • +

Replies To: Replacing characters in a string

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5224
  • View blog
  • Posts: 26,992
  • Joined: 10-May 07

Re: Replacing characters in a string

Posted 28 August 2007 - 12:05 PM

Can you make a function that takes arguments?

Private Sub cmdreplace_click() <-- argument 1 & argument 2 get fed into the function.

	txtOutput = Replace(txtInput.Text, Argument1, Argument2)

	End Sub



I don't know VB, but that's how I would do it in C.
Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Replacing characters in a string

Posted 28 August 2007 - 12:49 PM

In each call to Replace, you are only working with the original word entered and then overwriting the results in the txtOutput box. Therefore, you are only going to see the result of the last change made.

What you need to do is in the first call to Replace use the original word, then in any subsequent calls you need to work with the word that is currently stored in the txtOutput box.

Also, unless txtOutput is a variable you forgot to add the '.Text' to each control.

  Private Sub cmdreplace_click()

	  txtOutput.Text = Replace(txtInput.Text, "W", "&")
	  txtOutput.Text = Replace(txtOutput.Text , "o", "#")

	End Sub


Was This Post Helpful? 0
  • +
  • -

#4 bharath2yk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-June 07

Re: Replacing characters in a string

Posted 28 August 2007 - 07:35 PM

@no2pencil Not that doesnt work .

syntax  replace(txtInput.text   ,argument1 , argument2)

Will replace the word argument1 as argument2.


@jayman Can u give me one example plss ..

I really need it buddy !!
Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Replacing characters in a string

Posted 28 August 2007 - 09:10 PM

See the code in my previous post, I made changes to it in three places.
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5224
  • View blog
  • Posts: 26,992
  • Joined: 10-May 07

Re: Replacing characters in a string

Posted 28 August 2007 - 09:16 PM

Well, my suggestion was to create a function. You missed that part...

If you create a function that accepts two arguments, you can feed it set 1, then set 2, & even a set 3 if you like.

Follow Jayman's suggestion.
Was This Post Helpful? 0
  • +
  • -

#7 bharath2yk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-June 07

Re: Replacing characters in a string

Posted 29 August 2007 - 07:20 AM

@jay No that doesnt work dude !! Please try to suggest me some thing ..

@no2pencil .. Hey can u give me one example of such kind ??
Pls..
Was This Post Helpful? 0
  • +
  • -

#8 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: Replacing characters in a string

Posted 29 August 2007 - 08:49 AM

View Postjayman9, on 28 Aug, 2007 - 10:49 PM, said:


:blink: Umm, he's right you know !?

Anyhow, you probably just mixed some letters. Try using the output of the first function as an argument of the second function, this time it should work.
txtOutput.Text =  Replace(Replace(txtInput.Text, "W", "&"), "o", "#")


Was This Post Helpful? 0
  • +
  • -

#9 bharath2yk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-June 07

Re: Replacing characters in a string

Posted 29 August 2007 - 10:09 AM

@m2s87 Dude , It rocks ... it works dude ... Thanks alot buddy >:D< .. >:D< >:D< >:D< hehehe .. I am really happy
Was This Post Helpful? 0
  • +
  • -

#10 strod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 05-September 07

Re: Replacing characters in a string

Posted 06 September 2007 - 09:25 AM

View Postbharath2yk, on 28 Aug, 2007 - 11:57 AM, said:

Hey , I am in small prob guys , Need your help.

Prob:

I am developing a program which is GUI on VB6.

It has 2 text boxes and 1 command button .

Some text is entered into the first text box , If i click on command button, that text must be appear on to the second box with different format ..

Eg: I have entered Wow in first text box , as soon as i click on command button , this text must appear in second text box as &#& That means here "W" is replaced by "&" and "o" is replaced by "#"


I tried to do this using following code:

  Private Sub cmdreplace_click()

	txtOutput = Replace(txtInput.Text, "W", "&")

	End Sub


If i do this , I can change only letter W as & .. But i cant change o ..

Even i tried to do this :

  Private Sub cmdreplace_click()

	  txtOutput = Replace(txtInput.Text, "W", "&")
	  txtOutput = Replace(txtInput.Text, "o", "#")

	End Sub


If i do this , yes it is getting executed but not whole stuff ..
Only last part is getting executed .. That is , "o" is replaced by "#" but "W" is not replaced by "&"

I need to replace each and every word in whole sentence ..
Please give me few hints to do this ...
Hope u guys will help me ..
Tc ..

MOD EDIT: modified title ~ jayman9


From your code, you are assigning txtOutput with the Replace(txtinput) twice so the second replace does not take into account the first replace as you have not modified txtInput.text at any time.

Think about it.

Easy fix is below

 
Private Sub cmdreplace_click()

   txtOutput = Replace(txtInput.Text, "W", "&")
   txtOutput = Replace(txtOuput.Text, "o", "#")

End Sub



Another method would be to use the mid$ function to examine each character in turn and use some sort of conversion array to change each char whil in a loop.

Hope this helps
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1