473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

replacment of characters

37
How do I replace the one of the characters in the string.
For eg:

If the users were to input a few characters into textbox1, i will replace the 1st character of the string with the character "A" and return the edited string to display in textbox2.
Aug 2 '07 #1
7 1237
use the instr function to determine the postion of the string

eg retvalue = instr(string to search, "what to search for")

This will tell you where the string is char you want to remove is

see example below i have used before in vb6 (may need a tweek in dot net)

*************Code goes here**************************************

Function RemoveChar(Name As String, Char As String)
'This function removes a Character from a string variable
' The character must be specified when calling the function
' eg call removechar(string1,",") removes all commas from a string
Dim x
Dim FIRSTPART
Dim SECONDPART

If InStr(1, Name, Char) Then
x = InStr(1, Name, Char)
FIRSTPART = Left(Name, x - 1)
SECONDPART = Mid(Name, x + 1, Len(Name))
Name = FIRSTPART + SECONDPART
End If

Do While InStr(1, Name, Char) > 0
x = InStr(1, Name, Char)
FIRSTPART = Left(Name, x - 1)
SECONDPART = Mid(Name, x + 1, Len(Name))
Name = FIRSTPART + SECONDPART
Loop

RemoveChar = Name
End Function

*********End Code goes here **********************************
Aug 2 '07 #2
Let's see if I understand your question:

User types 12345 in textbox1. You want to put A2345 into textbox2.

If I understood correctly, try this code (vb.net):

Dim S as string = TextBox1.Text
TextBox2.Text = "A" + S.Substring(1)

HTH

Jon Jacobs
Aug 3 '07 #3
if JonJacobs understanding is correct you may also want to try this for VB 6

retVal = "A" & right(Text1.Text,len(Text1.Text) -1)


But if you're trying to search for a word from a group of words then Instr is good solution...
Aug 3 '07 #4
Killer42
8,435 Expert 8TB
retVal = "A" & right(Text1.Text,len(Text1.Text) -1)
You can actually make it even simpler. Try this...

retVal = "A" & Mid(Text1.Text, 2)
Aug 3 '07 #5
You should use functions for repeatable code as much as possible. The function i have given you allows you to pass in whatever character replacement that you want.

This reduces the amount of code that you need to write hence reducing the size of your app (performance)
Aug 3 '07 #6
i have modified my original code to handle replacement, rather than remove

it will need to be tested
It will be called as

Dim StringIwanttodisplay as string

Stringiwanttodisplay = ChangeChar(Stringiwanttodisplay,1,2)

The will replace all 1s with 2s in your string



Function ChangeChar(Name As String, Char_to_Change As String, Char_to_Change_to as String )

Dim x
Dim FIRSTPART
Dim SECONDPART

If InStr(1, Name, Char) Then
x = InStr(1, Name, Char)
FIRSTPART = Left(Name, x - 1)
SECONDPART = Mid(Name, x + 1, Len(Name))
Name = FIRSTPART + Char_to_Change_to + SECONDPART
End If

Do While InStr(1, Name, Char) > 0
x = InStr(1, Name, Char)
FIRSTPART = Left(Name, x - 1)
SECONDPART = Mid(Name, x + 1, Len(Name))
Name = FIRSTPART + Char_to_Change_to + SECONDPART
Loop

RemoveChar = Name
End Function
Aug 3 '07 #7
Killer42
8,435 Expert 8TB
You should use functions for repeatable code as much as possible ... hence reducing the size of your app (performance)
Good advice there, brunzire.
Aug 3 '07 #8

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

Similar topics

17
by: Pikkel | last post by:
i'm looking for a way to replace special characters with characters without accents, cedilles, etc.
7
by: Roy W. Andersen | last post by:
I've been searching google about this for days but can't find anything, so I'm hoping someone here can help me out. I'm trying to create zip-files without needing the zip-file extension in PHP,...
9
by: Safalra | last post by:
The idea here is relatively simple: a java program (I'm using JDK1.4 if that makes a difference) that loads an HTML file, removes invalid characters (or replaces them in the case of common ones...
43
by: Vladimir | last post by:
Method UnicodeEncoding.GetMaxByteCount(charCount) returns charCount * 2. Method UTF8Encoding.GetMaxByteCount(charCount) returns charCount * 4. But why that? Look: /* Each Unicode character...
4
by: wob | last post by:
Many thanks for those who responded to my question of "putting greek char into C string". In searching for an solution, I noticed that there are more than one version of "Extended ASCII...
20
by: SMG | last post by:
Hi All, I have created an application which is working fine and is in about to launch, now suddenly my mgmt says there are chances that Scrip ID( a particular id and not prim key) may have special...
11
by: Ron L | last post by:
I have a barcode scanner which uses a "keyboard wedge" program so that the data it scans comes through as if it was typed on a keyboard. I am trying to have the data in the barcode be displayed in...
9
by: Nathan Sokalski | last post by:
I have several TextBoxes with TextMode="MultiLine" in which I want to limit the number of characters that can be entered using a RegularExpressionValidator. I have come up with the following...
3
KevinADC
by: KevinADC | last post by:
Purpose The purpose of this article is to discuss the difference between characters inside a character class and outside a character class and some special characters inside a character class....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.