Replace string, every 4th letter not every letter?
Replace string, every 4th letter not every letter?
Replace string, every 4th letter not every letter?
Replace string, every 4th letter not every letter?
Replace string, every 4th letter not every letter?
Replace string, every 4th letter not every letter? Replace string, every 4th letter not every letter? Replace string, every 4th letter not every letter? Replace string, every 4th letter not every letter? Replace string, every 4th letter not every letter? Replace string, every 4th letter not every letter? Replace string, every 4th letter not every letter? Replace string, every 4th letter not every letter?
Replace string, every 4th letter not every letter? Replace string, every 4th letter not every letter?
Replace string, every 4th letter not every letter?
Go Back  Xtreme Visual Basic Talk > > > Replace string, every 4th letter not every letter?


Reply
 
Thread Tools Display Modes
  #1  
Old 07-27-2016, 08:14 AM
Benjagan91 Benjagan91 is offline
Regular
 
Join Date: Jul 2009
Posts: 55
Default Replace string, every 4th letter not every letter?


Hello.

I have a text box and I have been using the following line of code to change the letter "a" into "b".

Text1 = Replace(Text1.Text, Chr(97), Chr(98))

But what I would like to do is replace every 4th instance of the letter "a" in Text1 and not every single letter "a" in the textbox.

Could somebody please show me the easiest way to do this? I've tried searching for this but I'm not having any luck.

Thank You.
Reply With Quote
  #2  
Old 07-27-2016, 07:47 PM
Benjagan91 Benjagan91 is offline
Regular
 
Join Date: Jul 2009
Posts: 55
Default

No one knows how to do this?
Reply With Quote
  #3  
Old 07-27-2016, 08:47 PM
Kluz's Avatar
KluzReplace string, every 4th letter not every letter? Kluz is offline
Sapience.Aquire

Super Moderator
* Expert *
 
Join Date: Oct 2003
Location: Lake Bluff, Ill., U.S.
Posts: 3,424
Default

I would loop through the Len() of the text using the Instr() function and maintain a counter. Increment it at every "a". When you get to 4: replace and reset the counter.
__________________
No the other right mouse click
Reply With Quote
  #4  
Old 07-27-2016, 08:59 PM
Benjagan91 Benjagan91 is offline
Regular
 
Join Date: Jul 2009
Posts: 55
Default

Yes I was thinking the same and I've found the following code:

Code:
Locate nth occurrence of a string

If you want to find the nth occurrence of a string, not simply the first one, set n to the desired number and run the following code:

position = 0
Do
    position = InStr(position + 1, "avbprogramvb", "vb")
    n = n - 1
Loop Until position = 0 Or n = 0
The loop keeps searching until the nth occurrence is found or there was no match. position tells the result.

To search for the nth occurrence after a certain start position, let position = start - 1.
I'm just having trouble getting it to work myself. I'm very rusty with my VB and I was never that fluent to begin with. Could you please show me an example of the routine you've suggested? It sounds slightly different to the above code I've posted.

Thank You.
Reply With Quote
  #5  
Old 07-28-2016, 05:41 PM
passel's Avatar
passelReplace string, every 4th letter not every letter? passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,015
Default

Extra code added for illustration.
Shows input string, position counts, output string.
Code:
Option Explicit

Private Sub Command1_Click()
  Dim s As String
  Dim o As String
  Dim p As Integer, cnt As Integer
  Dim x As String  ' for illustration
  
  s = "abacadaeafagahaiajakalamanaoapaqarasatauavawaxayaza"
  x = Space$(Len(s))  ' for illustration
  o = s        'o is the output string
  p = InStr(o, "a")  'find the first "a"
  cnt = 0

  Do While p <> 0                        'Do while we've found an "a"
    cnt = cnt + 1                        '  count how many "a" found
    Mid$(x, p, 1) = Right$(Str$(cnt), 1)  ' for illustration, insert cnt into the space string
    If cnt = 4 Then                      '  If we've found a fourth "a" then
      Mid$(o, p, 1) = "b"                '    Overwrite the "a" with a "b"
      cnt = 0                            '    reset our counter
    End If                               '  End If
    p = InStr(p + 1, o, "a")             '  Find the next "a"
  Loop

  Debug.Print s                          'Print the source string
  Debug.Print x                          'Print the position of "a"s in the source string
  Debug.Print o                          'print the output string
End Sub
Results
Code:
abacadaeafagahaiajakalamanaoapaqarasatauavawaxayaza
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2
abacadbeafagahbiajakalbmanaoapbqarasatbuavawaxbyaza
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 07-28-2016 at 06:08 PM.
Reply With Quote
  #6  
Old 07-28-2016, 06:44 PM
Benjagan91 Benjagan91 is offline
Regular
 
Join Date: Jul 2009
Posts: 55
Default

Awesome! That looks exactly like what I'm after. I'll test it tonight (GMT 10+) and let you know how I go, but it looks spot on from a glance. Thank you very, very much for helping me out with this, I really appreciate it

The commenting and results field were nice touches also. Thank you for going out of your way.

Have a great day! (it's 9:45AM here in Eastern Australia).
Reply With Quote
  #7  
Old 07-29-2016, 05:58 AM
Benjagan91 Benjagan91 is offline
Regular
 
Join Date: Jul 2009
Posts: 55
Default

This is excellent. It works perfectly. Thank you very much, passel! I really appreciate it a lot
Reply With Quote
Reply

Tags
4th, letter, replace, single, text1, string


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Replace string, every 4th letter not every letter?
Replace string, every 4th letter not every letter?
Replace string, every 4th letter not every letter? Replace string, every 4th letter not every letter?
Replace string, every 4th letter not every letter?
Replace string, every 4th letter not every letter?
Replace string, every 4th letter not every letter? Replace string, every 4th letter not every letter? Replace string, every 4th letter not every letter? Replace string, every 4th letter not every letter? Replace string, every 4th letter not every letter? Replace string, every 4th letter not every letter? Replace string, every 4th letter not every letter?
Replace string, every 4th letter not every letter?
Replace string, every 4th letter not every letter?
 
Replace string, every 4th letter not every letter?
Replace string, every 4th letter not every letter?
 
-->