473,948 Members | 18,274 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find And Replace String

19 New Member
Hi goo day! I'am Darrel, a newbie with visual basic, i have a problem with my recent project.

the projecti have is to replace the text from one letter to another. I dont know how to code it.

I'am using text box for my (input), another textbox for the letter to be replace, another text box for the letter to be place. and display the whole text in a label..

can some give me some hint or sample codes. Thank you so much!!!

Hoping for an immediate reponse for it!!!
Mar 6 '07 #1
10 5675
samycbe
83 New Member
Dear Friend, Try this.......

1. I have one text box named TxtA and command button CmdProcess
2. I want to replace the text "a" with "z" in TxtA on cmdProcess - click
3. Please look the code on cmdProcess_clic k
4. Instead of "a" you can use other textbox.text

Expand|Select|Wrap|Line Numbers
  1. Dim a As String
  2. a = TxtA.Text
  3. a = ""
  4. Dim x As String
  5. x = ""
  6. For i = 1 To Len(TxtA)
  7.     If Mid(TxtA, i, 1) = "a" Then
  8.         x = x + "b"
  9.     Else
  10.         x = x + Mid(TxtA, i, 1)
  11.     End If
  12. Next
  13. MsgBox x
give a feed back

samy
Mar 6 '07 #2
Killer42
8,435 Recognized Expert Expert
Actually, while the details may depend on what version of VB you are using, in VB6 you can use the Replace() function to do it in one statement.
Mar 6 '07 #3
pa2ratczi
19 New Member
that's not the output that i have to achieve, the program is like this

the user must enter a word in a textbox1(of any length) and must enter/assign a letter in textbox2 which is the letter to be replace from textbox1 and the user must enter a letter in textbox3 which is the letter to be substituted. and display the whole text in a label.

its like this if the user enters the word "paragraph" and the user wanted to change all letter "a" with letter "b" and the result must be "pbrbgrbph" .

hope you can help me sir.... its very difficult for me.... thank you
Mar 6 '07 #4
Killer42
8,435 Recognized Expert Expert
That's what the code did, that samycbe posted. It's just that some of it was hard-coded rather than being taken from a textbox. After all, you can't expect us to do everything for you - you need to make a serious attempt at the work yourself.

Anyway, like I said, check out the Replace function. It's in the manual, and will do it all in one statement.
Mar 6 '07 #5
vijaydiwakar
579 Contributor
that's not the output that i have to achieve, the program is like this

the user must enter a word in a textbox1(of any length) and must enter/assign a letter in textbox2 which is the letter to be replace from textbox1 and the user must enter a letter in textbox3 which is the letter to be substituted. and display the whole text in a label.

its like this if the user enters the word "paragraph" and the user wanted to change all letter "a" with letter "b" and the result must be "pbrbgrbph" .

hope you can help me sir.... its very difficult for me.... thank you
try this code
replace("<thy string>","<repl ace char>","<replac ed by>")
lbl.caption=rep lace(txt1.text, txt2.text,txt3. text)
Mar 6 '07 #6
pa2ratczi
19 New Member
yes i know that, that's why ive tried using mid.

the code its like this

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. Dim str1 As String
  3. Dim str2 As String
  4. Dim str3 As String
  5.  
  6.  
  7. str1 = Text1.Text
  8. str2 = Text2.Text
  9. str3 = Text3.Text
  10.  
  11. If Mid(str1, 1, 1) = str2 Then
  12.     Mid(str1, 1, 1) = str3
  13.     Label4.Caption = Mid(str1, 1)
  14.  
  15. End If
  16. End Sub
and its working...but the problem is wat if the text is to long, i dont know how to loop my condition..
Mar 6 '07 #7
Killer42
8,435 Recognized Expert Expert
Well, you could use a For loop with the length of the original string as the upper limit.

Also, when posting code, please use appropriate tags around it for readability. Details can be found here.
Mar 6 '07 #8
pa2ratczi
19 New Member
can help me how to construct a loop using "FOR".
Mar 6 '07 #9
pa2ratczi
19 New Member
guys, ive done it am finished with my project, thanks a lot for all the help, to contribute here the code....

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. Dim str1 As String
  3. Dim str2 As String
  4. Dim str3 As String
  5. Dim Counter As Integer
  6.  
  7.  
  8. str1 = Text1.Text
  9. str2 = Text2.Text
  10. str3 = Text3.Text
  11.  
  12. For i = 1 To Len(str3)
  13.     Counter = i + 1
  14.     If Mid$(str3, i, 1) = str2 Then
  15.         Mid$(str3, i, 1) = str1
  16.         Label4.Caption = Mid$(str3, 1)
  17.     End If
  18. Next
  19.  
  20. End Sub
Hope you'll still help me guys sooner or later.. thanks!
Mar 6 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

10
33711
by: hokieghal99 | last post by:
import os, string print " " setpath = raw_input("Enter the path: ") def find_replace(setpath): for root, dirs, files in os.walk(setpath): fname = files for fname in files: find = string.find(file(os.path.join(root,fname), 'rb').read(), 'THIS') print find
1
3739
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
2
14966
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data in the format that I need it in order to create the reports that we use. So far this has proven to be successful for the reports that I am doing and the data that I am pulling into it. I just have one challenge that may require a lot of work and I...
25
4095
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
2
10024
by: scorpion53061 | last post by:
This excel find/replace code works great under Excel 2003 but bombs with an error (pasted below the code) in Excel 2000. Can anyone suggest an alternative to make both happy? I am using vb.net windows application. I also tried creating an object and assigning the find/replace event to it but I also received errors. I am a little under the gun on this one.... WSheet.Application.Selection.Replace(What:="NONE", Replacement:="",...
0
3090
by: Xah Lee | last post by:
Interactive Find and Replace String Patterns on Multiple Files Xah Lee, 2006-06 Suppose you need to do find and replace of a string pattern, for all files in a directory. However, you do not want to replace all of them. You need to look at it in a case-by-case basis. What can you do? Answer: emacs.
5
3410
by: peter | last post by:
Hello all, I'm looking for an advice. Example (one block in ascii file): $------------------------ NAME='ALFA' CODE='x' $------------------------
3
16949
by: TOXiC | last post by:
Hi everyone, First I say that I serched and tryed everything but I cannot figure out how I can do it. I want to open a a file (not necessary a txt) and find and replace a string. I can do it with: import fileinput, string, sys fileQuery = "Text.txt" sourceText = '''SOURCE'''
9
3493
by: JoeP | last post by:
Hi All, How can I find the reason for such an error: Failure sending mail. Some Code... oMailMessage.IsBodyHtml = False oMailMessage.Body = cEmailBody Dim oSMTP As New SmtpClient oSMTP.Send(oMailMessage) (in this line I am getting the above err)
3
14607
by: mouac01 | last post by:
Newbie here. How do I do a find and replace in a binary file? I need to read in a binary file then replace a string "ABC" with another string "XYZ" then write to a new file. Find string is the same length as Replace string. Here's what I have so far. I spent many hours googling for sample code but couldn't find much. Thanks... public static void FindReplace(string OldFile, string NewFile) { string sFind = "ABC"; //I probably need...
0
10166
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11582
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10695
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9898
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7432
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6120
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6340
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.