|
| Author |
Message |
Swamy
Joined: 04 Oct 2007 Posts: 11
|
Posted: Mon Mar 05, 2007 8:12 pm Post subject: Access string as a single dimension array |
|
|
Hi,
In VB6, is it possible to access parts of a string variable using the index.
Here is an example.
Dim myString as String
myString="This is the content"
if (myString[3] = "i") then ... else...
Basically, use an index to extract a character.
Regards,
Swamy
Archived from group: microsoft>public>vb>syntax |
|
| Back to top |
|
 |
"Rick Rothstein \
Joined: 04 Oct 2007 Posts: 1584
|
Posted: Tue Mar 06, 2007 12:12 am Post subject: Re: Access string as a single dimension array |
|
|
> In VB6, is it possible to access parts of a string variable using the
> index.
>
> Here is an example.
> Dim myString as String
> myString="This is the content"
> if (myString[3] = "i") then ... else...
>
> Basically, use an index to extract a character.
You are looking for the Mid$ function...
If Mid$(myString, 3, 1) = "i" Then...
Rick |
|
| Back to top |
|
 |
Swamy
Joined: 04 Oct 2007 Posts: 11
|
Posted: Mon Mar 05, 2007 9:56 pm Post subject: Re: Access string as a single dimension array |
|
|
Rick,
I want to use the logic in a loop. Do you think using Mid or Left or Right
functions are too expensive to loop through the characters in a string that
could be as long as 100 characters in it?
I was thinking the response will be much better if
Thanks for your help.
Swamy
"Rick Rothstein (MVP - VB)" wrote:
> > In VB6, is it possible to access parts of a string variable using the
> > index.
> >
> > Here is an example.
> > Dim myString as String
> > myString="This is the content"
> > if (myString[3] = "i") then ... else...
> >
> > Basically, use an index to extract a character.
>
> You are looking for the Mid$ function...
>
> If Mid$(myString, 3, 1) = "i" Then...
>
> Rick
>
>
> |
|
| Back to top |
|
 |
"Rick Rothstein \
Joined: 04 Oct 2007 Posts: 1584
|
Posted: Tue Mar 06, 2007 1:45 am Post subject: Re: Access string as a single dimension array |
|
|
> I want to use the logic in a loop. Do you think using Mid or Left or
> Right
> functions are too expensive to loop through the characters in a string
> that
> could be as long as 100 characters in it?
No problem... use them in a loop... a 100 characters won't strain the system
at all. Just out of curiosity, though, what are you doing that you need to
look at each character separately? (I ask because there may be other
approaches to consider depending on what you are doing.) Oh, if you are
worried about String manipulations that are "expensive", the one to keep an
eye on is repeated concatenations... depending on the number of iterations
and the size of the String values produced, it can really bog down a system.
Rick |
|
| Back to top |
|
 |
Swamy
Joined: 04 Oct 2007 Posts: 11
|
Posted: Mon Mar 05, 2007 11:41 pm Post subject: Re: Access string as a single dimension array |
|
|
Rick,
That is exactly what I am doing. I need to mask some data based on the
format provided. For example, assume mask format is XXXX9999 and the actual
data is 56781345 then the first four characters need to be replaced as XXXX
with the result as XXXX1345. The logic needs to be generic because the X in
the format field could be subsituted anywhere in any number.
I feel the system will slow down if I use Mid$ along with Left and Right for
concatention within a loop.
any thoughts on how to solve like using a simple one dimensional array in C
or C++
Swamy
"Rick Rothstein (MVP - VB)" wrote:
> > I want to use the logic in a loop. Do you think using Mid or Left or
> > Right
> > functions are too expensive to loop through the characters in a string
> > that
> > could be as long as 100 characters in it?
>
> No problem... use them in a loop... a 100 characters won't strain the system
> at all. Just out of curiosity, though, what are you doing that you need to
> look at each character separately? (I ask because there may be other
> approaches to consider depending on what you are doing.) Oh, if you are
> worried about String manipulations that are "expensive", the one to keep an
> eye on is repeated concatenations... depending on the number of iterations
> and the size of the String values produced, it can really bog down a system.
>
> Rick
>
>
> |
|
| Back to top |
|
 |
Jeff Johnson
Joined: 04 Oct 2007 Posts: 1327
|
Posted: Tue Mar 06, 2007 2:09 pm Post subject: Re: Access string as a single dimension array |
|
|
"Swamy" wrote in message @microsoft.com...
> any thoughts on how to solve like using a simple one dimensional array in
> C
> or C++
If you're just DYING to take this course, see the StrConv() function to
transform the string into a byte array. |
|
| Back to top |
|
 |
"Rick Rothstein \
Joined: 04 Oct 2007 Posts: 1584
|
Posted: Tue Mar 06, 2007 3:20 pm Post subject: Re: Access string as a single dimension array |
|
|
> For example, assume mask format is XXXX9999 and the actual
> data is 56781345 then the first four characters need to be replaced as
> XXXX
> with the result as XXXX1345. The logic needs to be generic because the X
> in
> the format field could be subsituted anywhere in any number.
Combining the above with your first post, are you saying you can have up to,
say, a 100 character piece of data and a corresponding 100 character mask
made up of Xs and 9s in some combination? Or are you saying you can have up
to, say, a 100 character piece of data and a corresponding smaller mask
(say, 8 characters long) which will be applied to numerical values within
the 100 character piece of data? It would really help if you could show us a
sample piece of data you want to work with (say a 20 to 30 characters long
example) and a sample mask you want to apply to it; that is, show us one or
two real-world pieces of data, the corresponding masks that you want to
apply and the result you are trying to achieve.
Rick |
|
| Back to top |
|
 |
Swamy
Joined: 04 Oct 2007 Posts: 11
|
Posted: Tue Mar 06, 2007 2:29 pm Post subject: Re: Access string as a single dimension array |
|
|
Rick,
I don't have the code yet. The data we expect is as follows:
mask format: XXXX9999XXXX or XXXXXX9999 or 9999XXXXXXXXX etc.
The data provided to format using the mask could be of the same length or
less or more. The idea is to first normalize these two fields by padding
zeros at beginning if data is short and pad "9"s to mask if it is short.
After normalizing, the idea is to iterate through each character in mask
format and start the masking until the end.
hope I explained it better now.
Swamy
"Rick Rothstein (MVP - VB)" wrote:
> > For example, assume mask format is XXXX9999 and the actual
> > data is 56781345 then the first four characters need to be replaced as
> > XXXX
> > with the result as XXXX1345. The logic needs to be generic because the X
> > in
> > the format field could be subsituted anywhere in any number.
>
> Combining the above with your first post, are you saying you can have up to,
> say, a 100 character piece of data and a corresponding 100 character mask
> made up of Xs and 9s in some combination? Or are you saying you can have up
> to, say, a 100 character piece of data and a corresponding smaller mask
> (say, 8 characters long) which will be applied to numerical values within
> the 100 character piece of data? It would really help if you could show us a
> sample piece of data you want to work with (say a 20 to 30 characters long
> example) and a sample mask you want to apply to it; that is, show us one or
> two real-world pieces of data, the corresponding masks that you want to
> apply and the result you are trying to achieve.
>
> Rick
>
>
> |
|
| Back to top |
|
 |
"Rick Rothstein \
Joined: 04 Oct 2007 Posts: 1584
|
Posted: Tue Mar 06, 2007 7:29 pm Post subject: Re: Access string as a single dimension array |
|
|
> I don't have the code yet. The data we expect is as follows:
>
> mask format: XXXX9999XXXX or XXXXXX9999 or 9999XXXXXXXXX etc.
>
> The data provided to format using the mask could be of the same length or
> less or more. The idea is to first normalize these two fields by padding
> zeros at beginning if data is short and pad "9"s to mask if it is short.
>
> After normalizing, the idea is to iterate through each character in mask
> format and start the masking until the end.
Does this function do what you are looking for?
Function ProcessData(ByVal DataIn As Variant, _
ByVal Mask As String) As String
Dim Index As Long
If Len(DataIn) > Len(Mask) Then
Mask = Mask & String$(Len(DataIn) - Len(Mask), "9")
ElseIf Len(DataIn) < Len(Mask) Then
DataIn = Format$(DataIn, String$(Len(Mask), "0"))
End If
ProcessData = DataIn
For Index = 1 To Len(Mask)
If Mid$(Mask, Index, 1) = "X" Then Mid$(ProcessData, Index) = "X"
Next
End Function
Rick |
|
| Back to top |
|
 |
Swamy
Joined: 04 Oct 2007 Posts: 11
|
Posted: Tue Mar 06, 2007 4:44 pm Post subject: Re: Access string as a single dimension array |
|
|
That will work. I was trying to see if it is possible to avoid using Mid$.
Looks like it is not possible in VB.
Thanks for your support.
Swamy
"Rick Rothstein (MVP - VB)" wrote:
> > I don't have the code yet. The data we expect is as follows:
> >
> > mask format: XXXX9999XXXX or XXXXXX9999 or 9999XXXXXXXXX etc.
> >
> > The data provided to format using the mask could be of the same length or
> > less or more. The idea is to first normalize these two fields by padding
> > zeros at beginning if data is short and pad "9"s to mask if it is short.
> >
> > After normalizing, the idea is to iterate through each character in mask
> > format and start the masking until the end.
>
> Does this function do what you are looking for?
>
> Function ProcessData(ByVal DataIn As Variant, _
> ByVal Mask As String) As String
> Dim Index As Long
> If Len(DataIn) > Len(Mask) Then
> Mask = Mask & String$(Len(DataIn) - Len(Mask), "9")
> ElseIf Len(DataIn) < Len(Mask) Then
> DataIn = Format$(DataIn, String$(Len(Mask), "0"))
> End If
> ProcessData = DataIn
> For Index = 1 To Len(Mask)
> If Mid$(Mask, Index, 1) = "X" Then Mid$(ProcessData, Index) = "X"
> Next
> End Function
>
> Rick
>
>
> |
|
| Back to top |
|
 |
"Rick Rothstein \
Joined: 04 Oct 2007 Posts: 1584
|
Posted: Tue Mar 06, 2007 7:49 pm Post subject: Re: Access string as a single dimension array |
|
|
> That will work. I was trying to see if it is possible to avoid using
> Mid$.
Using Mid$ as a statement (not a function) is extremely fast in VB (and you
will note it avoids having to do concatenations).
Rick |
|
| Back to top |
|
 |
Swamy
Joined: 04 Oct 2007 Posts: 11
|
Posted: Tue Mar 06, 2007 5:23 pm Post subject: Re: Access string as a single dimension array |
|
|
Rick,
I plan to implement the solution you proposed and see if there is any impact
on the performance.
Thanks for your help and advice.
Swamy
"Rick Rothstein (MVP - VB)" wrote:
> > That will work. I was trying to see if it is possible to avoid using
> > Mid$.
>
> Using Mid$ as a statement (not a function) is extremely fast in VB (and you
> will note it avoids having to do concatenations).
>
> Rick
>
>
> |
|
| Back to top |
|
 |
Karl E. Peterson
Joined: 04 Oct 2007 Posts: 4836
|
Posted: Tue Mar 06, 2007 5:23 pm Post subject: Re: Access string as a single dimension array |
|
|
Swamy wrote:
> That will work. I was trying to see if it is possible to avoid using Mid$.
Why?
> Looks like it is not possible in VB.
It might indeed be, but it's hard to imagine a good reason, which makes a response
difficult.
--
..NET: It's About Trust!
http://vfred.mvps.org |
|
| Back to top |
|
 |
Ralph
Joined: 04 Oct 2007 Posts: 4148
|
Posted: Mon Mar 12, 2007 11:29 am Post subject: Re: Access string as a single dimension array |
|
|
"Swamy" wrote in message@microsoft.com...
> That will work. I was trying to see if it is possible to avoid using
Mid$.
> Looks like it is not possible in VB.
>
> Thanks for your support.
> Swamy
>
> "Rick Rothstein (MVP - VB)" wrote:
>
Another excellent example of why "pre-optimizing" - making assumptions
without actually testing - is a bad practice. Programmers invariably guess
wrong.
First make it work using simple logic, then investigate ways to make it
faster - if necessary.
-ralph
|
|
| Back to top |
|
 |
|