Replacement for VB6 Mid() in .NET?

S

Steve S.

I am looking for something in .NET that is functionally
equivalent to the VB6 Mid() function, which allows me to
replace a portion of a string at a specific position with
another string. What I am trying to do is build a fixed
length data record by starting with a string of blanks and
then dropping in data at specific points within the
string. Neither the .NET "String" or "StringBuilder"
classes seem to have this functionality, unless I do it as
a two-step process of Remove and then Insert. Any
suggestions?

-- Steve
 
W

William Ryan

If you know what you are trying to replace then string.Replace may work for
you.
 
G

Guest

I'm not near my compiler but try:

public shared sub MID(byref Str as String, byval Start as
Integer, ByVal Length as integer, byval Value as String,
optional byval Trunc as boolean = false)

if Trunc then
str = str.substring(0,s-1).concat(Value.substring
(0,l),str.Substring(s+l-1)
else
str = str.substring(0,s-1).concat(Value,str.Substring(s+l-
1)
end if

end sub
 
C

Cor

Steve,
Use what you are always did, the functions you are talkin about are almost
all from VB6 (not all) still existing in VB.net, but you can use others too
now. But why would you?

( I don't use them, but I'am not so used to them).

Cor
 
R

Robert Jacobson

You're right, there isn't a directly-equivalent method in .Net. However, if
you want to, you can keep using the Mid() statement. It's still supported
in VB.Net:

ms-help://MS.MSDNQTR.2003FEB.1033/vblr7/html/vastmmid.htm

Also, check out this Dr. Gui article on string manipulation:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnguinet/html/drguinet5_update.asp

Since you're just replacing the characters in a fixed-length string, though,
you could get the same result fairly easily by creathing your own method.
(It would have better performance than the suggestions in the Dr. Gui
column, which must accomodate variable-length strings.) Start by creating a
stringbuilder that contains X number of space characters. Then loop through
each character in the replacement string, and change the corresponding
character in the stringbuilder to the new character.

Instead of a stringbuilder, you could also do this with a fixed-size array
of characters. (It's easy to convert a string to an array of characters,
and vice versa.)

Keep in mind that strings are immutable. Any time you modify a string,
including by using the Mid statement, you're really creating a new string
and marking the old one for garbage collection. It's usually faster to use
a stringbuilder or -- if the string is a fixed size -- an array of
characters.
 
H

Herfried K. Wagner [MVP]

Hello,

William Ryan said:
If you know what you are trying to replace then string.Replace
may work for you.

This will only replace occurances of a specific string. You cannot tell
'Replace' to replace, for example, characters from position 2 to 4 with
"bla".
 
H

Herfried K. Wagner [MVP]

Hello,

Steve S. said:
I am looking for something in .NET that is functionally
equivalent to the VB6 Mid() function, which allows me to
replace a portion of a string at a specific position with
another string. What I am trying to do is build a fixed
length data record by starting with a string of blanks and
then dropping in data at specific points within the
string. Neither the .NET "String" or "StringBuilder"
classes seem to have this functionality, unless I do it as
a two-step process of Remove and then Insert. Any
suggestions?

'Mid' is still available in VB.NET. If you don't want to use VB.NET
commands with VB.NET source code, you should maybe turn to an other
programming language like C#...

;-)
 
I

Ian Henderson

I experienced a similar problem. I'm currently learning VS2002. What I
found is that you either need to use Microsoft.VisualBasic.Mid, or include
the line "Imports Microsoft.VisualBasic" at the very top of your code.

Incidentally, what I also found is that you can't use the Mid function to
replace a character outright (wait for the replies telling me I'm wrong
here). The way I managed to get around that would be as follows (assume
that myString is the field that contains the value you want to work with,
that it is 20 characters long and you want to replace the "a" in position 10
with a "b"):

myString = Left(myString,9) & "b" & Left(myString, Len(myString)-10)

HTH

Ian Henderson
Access97 developer, novice .NET user
 
R

Robert Jacobson

Substring only retrieves the characters from the string. He wants to use
Mid to set the characters. (To modify the string.)
 
C

Cor

Robert,
You can set the string with a connection from substrings too.
But in this case Steve is used to Mid, so why substring.
Cor
 
R

Robert Jacobson

Perhaps I misunderstood what Aman was trying to say. You can't use, e.g.

Dim TestString As String = "Foobar"
TestString.Substring(3, 3) = "foo"

Even though you can use Mid for that:

Mid(Teststring, 4, 3) = "foo"

You can, of course, concat strings using String.Substring(a,b) & xyz &
String.substring(y,z). This will be a performance dog, though, since it
will have to create two new strings before concating them.

If Steve's primary consideration is short code and ease of use, he should
use the Mid function. If performance is a bigger concern, it would be
faster to modify a character array or StringBuilder, particularly since the
string has a fixed length.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top