PC Review


Reply
Thread Tools Rate Thread

Accessing substrings quickly

 
 
Robert Crandal
Guest
Posts: n/a
 
      13th Dec 2009
Do I need to use the "Mid" function each time I want
to access a substring??

For example, rather than using the following:

mysubstring = Mids(src, 5, 1) ' get 5th character/string

can't I use some other array/index notation
like the following:

mysubstring = src(5)


I get the impression that my code would run a lot faster
if I could access my substrings by an index, rather than
making calls to functions such as Mids(), Instr(), etc....

thank u



 
Reply With Quote
 
 
 
 
Rick Rothstein
Guest
Posts: n/a
 
      13th Dec 2009
The Mid function is one of VB's fastest executing functions... you won't
find a faster way to extract a character from a string than it.

--
Rick (MVP - Excel)


"Robert Crandal" <(E-Mail Removed)> wrote in message
news:LrXUm.79828$(E-Mail Removed)...
> Do I need to use the "Mid" function each time I want
> to access a substring??
>
> For example, rather than using the following:
>
> mysubstring = Mids(src, 5, 1) ' get 5th character/string
>
> can't I use some other array/index notation
> like the following:
>
> mysubstring = src(5)
>
> I get the impression that my code would run a lot faster
> if I could access my substrings by an index, rather than
> making calls to functions such as Mids(), Instr(), etc....
>
> thank u
>
>
>


 
Reply With Quote
 
Charles Williams
Guest
Posts: n/a
 
      13th Dec 2009
If you need to access several of the individual characters in the string, or
handle the characters as numbers, it can be faster to use a Byte array:

Private Sub somebytes()

Dim aBytes() As Byte
Dim StrLongString As String
Dim strChar As String

StrLongString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
aBytes = StrLongString
strChar = Chr(aBytes(8))
MsgBox strChar

End Sub

Charles
___________________________________
The Excel Calculation Site
http://www.decisionmodels.com

"Robert Crandal" <(E-Mail Removed)> wrote in message
news:LrXUm.79828$(E-Mail Removed)...
> Do I need to use the "Mid" function each time I want
> to access a substring??
>
> For example, rather than using the following:
>
> mysubstring = Mids(src, 5, 1) ' get 5th character/string
>
> can't I use some other array/index notation
> like the following:
>
> mysubstring = src(5)
>
> I get the impression that my code would run a lot faster
> if I could access my substrings by an index, rather than
> making calls to functions such as Mids(), Instr(), etc....
>
> thank u
>
>
>
>



 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      13th Dec 2009
This may be a duplicate posting as I can't see the first one I sent
=====================================================================

I think you need to convert this statement...

aBytes = StrLongString

to this...

aBytes = StrConv(StrLongString, vbFromUnicode)

in order to get your method to work. Also note that the Byte array that is
produced will always be zero-based even if you have your Option Base set to
1, so if you want the 8th character in the text, you have to remember to use
7 as the index to the array.

--
Rick (MVP - Excel)


"Charles Williams" <(E-Mail Removed)> wrote in message
news:4b25087d$0$2526$(E-Mail Removed)...
> If you need to access several of the individual characters in the string,
> or handle the characters as numbers, it can be faster to use a Byte array:
>
> Private Sub somebytes()
>
> Dim aBytes() As Byte
> Dim StrLongString As String
> Dim strChar As String
>
> StrLongString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
> aBytes = StrLongString
> strChar = Chr(aBytes(8))
> MsgBox strChar
>
> End Sub
>
> Charles
> ___________________________________
> The Excel Calculation Site
> http://www.decisionmodels.com
>
> "Robert Crandal" <(E-Mail Removed)> wrote in message
> news:LrXUm.79828$(E-Mail Removed)...
>> Do I need to use the "Mid" function each time I want
>> to access a substring??
>>
>> For example, rather than using the following:
>>
>> mysubstring = Mids(src, 5, 1) ' get 5th character/string
>>
>> can't I use some other array/index notation
>> like the following:
>>
>> mysubstring = src(5)
>>
>> I get the impression that my code would run a lot faster
>> if I could access my substrings by an index, rather than
>> making calls to functions such as Mids(), Instr(), etc....
>>
>> thank u
>>
>>
>>
>>

>
>


 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      13th Dec 2009
This may be a triplicate posting as I can't see the first two I posted.
I removed your original message in case that was the problem.
=======================================================================

I think you need to convert this statement...

aBytes = StrLongString

to this...

aBytes = StrConv(StrLongString, vbFromUnicode)

in order to get your method to work. Also note that the Byte array that is
produced will always be zero-based even if you have your Option Base set to
1, so if you want the 8th character in the text, you have to remember to use
7 as the index to the array.

--
Rick (MVP - Excel)

 
Reply With Quote
 
Charles Williams
Guest
Posts: n/a
 
      13th Dec 2009
Rick,

Both methods work, but in a slightly different way:

aBytes = StrLongString

gives you a zero-based Byte array containing two bytes for each character
(character number, unicode code page number),
so Chr(aBytes(8)) gives you the 5th character (E) as in the OP's example

aBytes = StrConv(StrLongString, vbFromUnicode)

gives you a zero-based byte array containing one byte for each character
(character number in the current default code page)
so Chr(Abytes(4)) gives you the 5th character (E) as in the OP's example

I favour the 2-byte approach because its easier to recreate
strings/substrings by reassigning the Byte array to a string
strChar = aBytes
gives you back the string from the byte array

Private Sub somebytes()

Dim aBytes() As Byte
Dim StrLongString As String
Dim strChar As String

StrLongString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
aBytes = StrLongString
strChar = aBytes
MsgBox strChar

End Sub

Charles
___________________________________
The Excel Calculation Site
http://www.decisionmodels.com

"Rick Rothstein" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> This may be a duplicate posting as I can't see the first one I sent
> =====================================================================
>
> I think you need to convert this statement...
>
> aBytes = StrLongString
>
> to this...
>
> aBytes = StrConv(StrLongString, vbFromUnicode)
>
> in order to get your method to work. Also note that the Byte array that is
> produced will always be zero-based even if you have your Option Base set
> to 1, so if you want the 8th character in the text, you have to remember
> to use 7 as the index to the array.
>
> --
> Rick (MVP - Excel)
>
>
> "Charles Williams" <(E-Mail Removed)> wrote in message
> news:4b25087d$0$2526$(E-Mail Removed)...
>> If you need to access several of the individual characters in the string,
>> or handle the characters as numbers, it can be faster to use a Byte
>> array:
>>
>> Private Sub somebytes()
>>
>> Dim aBytes() As Byte
>> Dim StrLongString As String
>> Dim strChar As String
>>
>> StrLongString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
>> aBytes = StrLongString
>> strChar = Chr(aBytes(8))
>> MsgBox strChar
>>
>> End Sub
>>
>> Charles
>> ___________________________________
>> The Excel Calculation Site
>> http://www.decisionmodels.com
>>
>> "Robert Crandal" <(E-Mail Removed)> wrote in message
>> news:LrXUm.79828$(E-Mail Removed)...
>>> Do I need to use the "Mid" function each time I want
>>> to access a substring??
>>>
>>> For example, rather than using the following:
>>>
>>> mysubstring = Mids(src, 5, 1) ' get 5th character/string
>>>
>>> can't I use some other array/index notation
>>> like the following:
>>>
>>> mysubstring = src(5)
>>>
>>> I get the impression that my code would run a lot faster
>>> if I could access my substrings by an index, rather than
>>> making calls to functions such as Mids(), Instr(), etc....
>>>
>>> thank u
>>>
>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Accessing the correct record quickly John W. Vinson Microsoft Access Form Coding 1 9th Feb 2010 05:45 AM
Re: Accessing the correct record quickly Marshall Barton Microsoft Access Form Coding 2 9th Feb 2010 04:31 AM
RE: Accessing the correct record quickly KARL DEWEY Microsoft Access Form Coding 0 8th Feb 2010 09:24 PM
Re: Accessing the correct record quickly Jeff Boyce Microsoft Access Form Coding 0 8th Feb 2010 09:12 PM
how to quickly check for database connectivity quickly =?Utf-8?B?Qmx1ZW1lbA==?= Microsoft ADO .NET 1 17th Oct 2006 11:44 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:47 PM.