• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

Inserting characters in between a string

This is not a assignment nor homework. It is just a little programming exercise.

Here's the task i am stucked with for a week. So i think it is time to seek help from others.

Create two textbox and a command button.

The first textbox will contain a word of any length. Once clicking on the command button, the second textbox will show the altered required output.

Let's say i key in "HELP" in textbox 1, on clicking the command button, textbox 2 will appear "H*E*L*P".

Thanks in advance.
0
dream_thief
Asked:
dream_thief
  • 2
  • 2
  • 2
  • +2
4 Solutions
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
For x = 1 to Len(SourceTextbox.Text)
    DestTextBox.Text = DestTextBox.Text & Mid(SourceTextbox.text, x, 1) & "*"
Next x
0
 
ColosseoCommented:
Hi

here his an alterted version of leew suggestion.
It will reset the destination box each time so that the it wont have the results of the last action.
Also it won't add an asterix to the end of the string (it looks like you only want them to appear in between the current characters)


  ' Get the first character in the source box
  DestTextBox = Mid(SourceTextbox, 1, 1)

  ' For every other character in the source box
  For x = 2 To Len(SourceTextbox)
    ' Add an asterix and then the charactter to the destination box
    DestTextBox = DestTextBox & "*" & Mid(SourceTextbox, x, 1)
  Next x

Cheers

Scott
0
 
dream_thiefAuthor Commented:
Thanks for the fast response.. i check them out.. and let you guys know.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Shiju SasidharanCommented:
Hi
Try this
'Dont forget to include reference
' Project ->Reference -> Microsoft VBscript Regular Expressions
'-------------------------------------------------------------------------------
Private Sub Command1_Click()
    Dim objReg As New RegExp
    Dim objMatCol As MatchCollection
    With objReg
        .Global = True
        .IgnoreCase = True
        .Pattern = "([\w])"
    End With
    Text2.Text=""
    Set objMatCol = objReg.Execute(Trim(Text1.Text))
    If objMatCol.Count > 0 Then
        Text2.Text = objReg.Replace(Text1.Text, "$1" & "*")
        If Right(Text2.Text, 1) = "*" Then
           Text2.Text = Left(Text2.Text, Len(Text2.Text) - 1)
        End If
    End If
   
End Sub

'---------------------------------
;-)
Shiju
0
 
Dave BrettCommented:
Hi dream_thief,

If you went down a RegExp path then a Replace method is simplest

But for a simple string I'd go with leew's example. The RegEx is useful when the pattern is more complex

Private Sub CommandButton1_Click()
    Dim RegEx, NewString As String
    Set RegEx = CreateObject("vbscript.regexp")
    With RegEx
        .Global = True
        .Pattern = "([a-zA-Z])"
    End With
    NewString = RegEx.Replace(TextBox1.Text, "$1*")
    TextBox2.Text = Left(NewString, Len(NewString) - 1)
End Sub

Cheers

Dave
0
 
dream_thiefAuthor Commented:
Thanks everyone. I think i should split the points with each of you guys. Since all the answers helped.

Colosseo would have the largest portion since it is the one i was looking for. just minor modifications needed. Thanks.
0
 
Shiju SasidharanCommented:
Hi dream_thief

 Thanks for the Points

;-)
Shiju
0
 
ColosseoCommented:
Glad to have helped

Cheers

Scott
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now