If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: MID Take two

> Ok what am I doing wrong?

Jay: I'd say this is your problem:

s = s & Chr(b.GetValue(x))

It would be much more efficient to use the StringBuilder's .Append method.
It would be even more efficient to use the String's .CopyTo method to copy
the desired characters in a single operation. Or simply use .SubString,
which I suspect is what VB.NET does under the hood:

Re: MID Take two

Hi,
If you need to get access at a char level (not byte) your example can be
rewritten as
Private Function sMid2(ByVal Str As String, ByVal Position As Int32, ByVal ByValLength As Int32) As String
Return New String(Str.ToCharArray(Position - 1, ByValLength))
End Function

This is much faster than the code you posted, but nowhere near the built-in
Mid() or SubString() functions.

Using a longer string exasperates the problems of user defined versions:
ret = sMid("This is a test, this is a test, and this is a test, and this is a test ", 17, 14) = 8.36 secs
ret = Mid$("This is a test, this is a test, and this is a test, and this is a test ", 17, 14) = 0.12 secs
ret = "This is a test, this is a test, and this is a test, and this is a test ".Substring(17, 14) = 0.12 secs
ret = sMid2("This is a test, this is a test, and this is a test, and this is a test ", 17, 14) = 0.46 secs
ret = New String("This is a test, this is a test, and this is a test, and this is a test ".ToCharArray(17 - 1, 14)) = 0.46 secs

As you can see, the loop in your code increases processing time in
proportion to the length of the substring being requested, but nothing
comes close to the built-in framework functions.

Re: MID Take two

How would I use append to attach two strings...

"Phil Weber" <pweber@nospam.fawcette.com> wrote in message
news:3e329ecb$1@tnews.web.devx.com...
| > Ok what am I doing wrong?
|
| Jay: I'd say this is your problem:
|
| s = s & Chr(b.GetValue(x))
|
| It would be much more efficient to use the StringBuilder's .Append method.
| It would be even more efficient to use the String's .CopyTo method to copy
| the desired characters in a single operation. Or simply use .SubString,
| which I suspect is what VB.NET does under the hood:
|
| Private Function sMid(ByVal Str As String, ByVal Position As Int32, _
| ByVal Length As Int32) As String
| Return Str.Substring(Position, Length)
| End Function
|
| --
| Phil Weber
|
|

Re: MID Take two

Jay,

There is no need for a substitute for the Mid function, it works fine. The
problem is with the mid statement. That is to say, the one that substitutes
a series of characters for another series of characters within a string.

Re: MID Take two

It looks like you are just extracting a string. The problem in .NET is
actually with *updating* strings. Each time you are updating a string, you
actually create a brand new string. A StringBuilder may solve this by
working directly on a string buffer that is allocated one time only for all
these updates...

Depends on the size of the string. If you have large strings then it
is painfully slow. Instead you can ovsersize Mystring and start using
Instr to insert String1 and String2. The performance is improved
greatly then - but readability can suffer.

Re: MID Take two

On Tue, 28 Jan 2003 08:30:09 +0100, "Eddie Burdak"
<eburdak@pilatus-aircraft.com> wrote:
>Depends on the size of the string. If you have large strings then it
>is painfully slow. Instead you can ovsersize Mystring and start using
>Instr to insert String1 and String2. The performance is improved
>greatly then - but readability can suffer.

Absolutely, Eddie. But in my experience of writing business-type apps
most strings are quite short - names, addresses, parts, descriptions,
invoice details and so on. I don't agree (a) that strings should be
immutable and (b) that strings should be objects.

Re: MID Take two

"Mike Mitchell" <kylix_is@yahoo.co.uk> wrote in message
news:4kdd3v0bd237hamsq13i09rfqrifsjju0k@4ax.com...
> On Tue, 28 Jan 2003 08:30:09 +0100, "Eddie Burdak"
> <eburdak@pilatus-aircraft.com> wrote:
>
> >Depends on the size of the string. If you have large strings then it
> >is painfully slow. Instead you can ovsersize Mystring and start using
> >Instr to insert String1 and String2. The performance is improved
> >greatly then - but readability can suffer.
>
> Absolutely, Eddie. But in my experience of writing business-type apps
> most strings are quite short - names, addresses, parts, descriptions,
> invoice details and so on. I don't agree (a) that strings should be
> immutable and (b) that strings should be objects.

First, that may be your experience - but not everyone has the same so it is
good to be aware of the limitations of VB.CLASSIC string handling and have
techniques for working around them. As for your other 2 disagreements - B)
is a matter of religion, and so I'm not going to try to convince you
otherwise, but there are very good reasons for A. Because the compiler
knows that a string can not be changed, it is possible to make storage
optimizations that are not possible for mutable strings - things like
interning. It does cause opertions that modify strings to suffer - since it
requires a new allocation with operation - but those are the cases that
StringBuilder was ment for (StringBuffer in java).