VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Duplicate characters

by Vesa Piittinen (20 Submissions)
Category: String Manipulation
Difficulty: Advanced
Date Added: Wed 3rd February 2021
Rating: (2 Votes)

This is quite a rare need, but here is a very fast solution for finding out whether there is a duplicate of any specific character within a string. Some people use InStr or InStrB as a solution for finding out a character duplicate, but the drawback is that InStr gets slow very fast if the first few characters are not duplicate characters. Also, if there are no duplicates then InStr loops the whole string the amount of length of the string, which can mean hundreds of times at worst case scenarios.


The project provides two functions: HasDuplicates and HasDuplicatesM, the latter is class wrapped. The first one is a straightforward InStrB version. The second uses bitmasking in a Long array as well as safe array tricks to get Integer array access to the characters of the given string. Both functions return a character position of the first duplicate they find, however the results between the functions are always different, because InStrB version returns the first character while the safe array version returns the duplicate. InStrB version can be changed to work the same way.


This code was requested at VBForums, I don't know why, but I made it anyway. The code should have enough comments for people to learn some advanced VB6 memory tricks.

Rate Duplicate characters

Download Duplicate characters

Download Duplicate characters (4 KB)

Duplicate characters Comments

No comments have been posted about Duplicate characters. Why not be the first to post a comment about Duplicate characters.

Post your comment

Subject:
Message:
0/1000 characters