msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Access string as a single dimension array

 
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax
Author Message
Swamy



Joined: 04 Oct 2007
Posts: 11

PostPosted: Mon Mar 05, 2007 8:12 pm    Post subject: Access string as a single dimension array Reply with quote

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
View user's profile Send private message
"Rick Rothstein \



Joined: 04 Oct 2007
Posts: 1584

PostPosted: Tue Mar 06, 2007 12:12 am    Post subject: Re: Access string as a single dimension array Reply with quote

> 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
View user's profile Send private message
Swamy



Joined: 04 Oct 2007
Posts: 11

PostPosted: Mon Mar 05, 2007 9:56 pm    Post subject: Re: Access string as a single dimension array Reply with quote

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
View user's profile Send private message
"Rick Rothstein \



Joined: 04 Oct 2007
Posts: 1584

PostPosted: Tue Mar 06, 2007 1:45 am    Post subject: Re: Access string as a single dimension array Reply with quote

> 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
View user's profile Send private message
Swamy



Joined: 04 Oct 2007
Posts: 11

PostPosted: Mon Mar 05, 2007 11:41 pm    Post subject: Re: Access string as a single dimension array Reply with quote

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
View user's profile Send private message
Jeff Johnson



Joined: 04 Oct 2007
Posts: 1327

PostPosted: Tue Mar 06, 2007 2:09 pm    Post subject: Re: Access string as a single dimension array Reply with quote

"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
View user's profile Send private message
"Rick Rothstein \



Joined: 04 Oct 2007
Posts: 1584

PostPosted: Tue Mar 06, 2007 3:20 pm    Post subject: Re: Access string as a single dimension array Reply with quote

> 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
View user's profile Send private message
Swamy



Joined: 04 Oct 2007
Posts: 11

PostPosted: Tue Mar 06, 2007 2:29 pm    Post subject: Re: Access string as a single dimension array Reply with quote

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
View user's profile Send private message
"Rick Rothstein \



Joined: 04 Oct 2007
Posts: 1584

PostPosted: Tue Mar 06, 2007 7:29 pm    Post subject: Re: Access string as a single dimension array Reply with quote

> 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
View user's profile Send private message
Swamy



Joined: 04 Oct 2007
Posts: 11

PostPosted: Tue Mar 06, 2007 4:44 pm    Post subject: Re: Access string as a single dimension array Reply with quote

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
View user's profile Send private message
"Rick Rothstein \



Joined: 04 Oct 2007
Posts: 1584

PostPosted: Tue Mar 06, 2007 7:49 pm    Post subject: Re: Access string as a single dimension array Reply with quote

> 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
View user's profile Send private message
Swamy



Joined: 04 Oct 2007
Posts: 11

PostPosted: Tue Mar 06, 2007 5:23 pm    Post subject: Re: Access string as a single dimension array Reply with quote

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
View user's profile Send private message
Karl E. Peterson



Joined: 04 Oct 2007
Posts: 4836

PostPosted: Tue Mar 06, 2007 5:23 pm    Post subject: Re: Access string as a single dimension array Reply with quote

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
View user's profile Send private message
Ralph



Joined: 04 Oct 2007
Posts: 4148

PostPosted: Mon Mar 12, 2007 11:29 am    Post subject: Re: Access string as a single dimension array Reply with quote

"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
View user's profile Send private message
Display posts from previous:   
Related Topics:
how to get the dimension of an array because of using dynamic array and for some specific reason, I can't know when my array is 1 dimension or when it is 2 dimension , so I have to know the function of getting the dimension of an array.

hwo to convert a char string into an array Hi How to convert character into array like a(5) = "apple" ... a(0) = "a" , a(1) = "p" , a(2) = "p" , a(3) = "l" , a(4) = "e" Thanks

Load a character string into an array I have an array defined a follows: name (50) as byte. I want to load a string of 50 characters into the array. I conintue to get the message "Can't assign to an array" when I try to following: Dim CustName(50) as byte Dim CustomerName CustName = Custome

Referencing an object from a string within an array I would like to pre-apologize for the length of this post. I have tried to be as clear and concise as possible. Here is a quick breakdown of what I am looking to do… the rest is more of an explanation to be sure I am headed in the right direction. I nee

Converting a Hex string or byte array to a double I have read a load of data from a file into memory in a series of bytes. I know that 8 of these bytes represent an IEEE 754 double. Is there a simple way of turning 8 bytes into a double? Thanks
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group