Need Help with String Manipulation!!!

  • Thread starter Thread starter Hexman
  • Start date Start date
H

Hexman

Well, getting to know VB.Net is shall we say challenging. When I was
coding in Vb (years ago) there used to be a "Right, Left, Mid" string
functions. Where did they all go?

I need to take a file path and change a couple of characters in it:

an example: (change R to J)

c:\Data\Finance\am051022.Rcc
to
c:\Data\Finance\am051022.Jcc

I've searched on the internet and found reference to the StringBuilder
class. Is that what I should be concentrating on?

I'm sure someone has already built a comprehensive class for strings.
Want to share it?

Any good examples out there? Recommendations please.

Hexman
 
Small easy sample ;)

Dim sPath As String = "c:\Data\Finance\am051022.Rcc"

sPath = sPath.Replace("R", "J")



Meelis
 
The Right, Left, Mid etc functions are still there. They now reside in the
Microsoft.VisualBasic namespace. I would recommend, however, that you
consider the functionality provided by the Framework classes to achieve your
aim.

If the 'source' and 'target' paths ALWAYS conform to the pattern you have
shown then String.Replace will work quite happily.

If, as I suspect, they don't then you need to be a little more adventurous.
Have a look at the System.IO.Path class where you will find a number of
methods for manipulating file names and paths.

Also have a good look at the System.String structure where you will find a
number of powerful string manipulation methods.
 
Hexman said:
Well, getting to know VB.Net is shall we say challenging. When I was
coding in Vb (years ago) there used to be a "Right, Left, Mid" string
functions. Where did they all go?

They are still there, in Microsoft.VisualBasic.Dll. Some people say the
use of this part of the Framework should be avoided, others disagree.
You will have to Imports it if you want to use it, and also qualify
Left if you want to use it in a form, because the Form's Left property
takes syntactic precedence.

If you want to move to the ".Net way" of strings, look up the methods
on the String class, notably Substring (which can replace all three)
and IndexOf (which serves for Instr).
I'm sure someone has already built a comprehensive class for strings.
Want to share it?

System.String :)
 
Hexman said:
Well, getting to know VB.Net is shall we say challenging. When I was
coding in Vb (years ago) there used to be a "Right, Left, Mid" string
functions. Where did they all go?

They're all still there. If you're working on code within a Form, though,
the Form's Left and Right properties will take precedence over the Left and
Right functions. In this case you can access them as Strings.Left() and
Strings.Right().
I need to take a file path and change a couple of characters in it:

an example: (change R to J)

c:\Data\Finance\am051022.Rcc
to
c:\Data\Finance\am051022.Jcc

If you want to change all "R" characters to "J", you could simply use the
Replace() function. This will also replace all letters within the path, of
course.

If you wanted to perform the replacement but without affecting the path, you
could do this:

\\\
Dim f As String = "c:\Data\Finance\am051022.Rcc"
f = System.IO.Path.GetDirectoryName(f) & "\" & _
Replace(System.IO.Path.GetFileName(f), "R", "J")
///

Or if you want to change that specific character, you could do it like this:

\\\
Dim f As String = "c:\Data\Finance\am051022.Rcc"
f = Strings.Left(f, Len(f)-3) & "J" & Strings.Mid(f,Len(f)-1)
///

It all really depends what you're trying to do.
I've searched on the internet and found reference to the StringBuilder
class. Is that what I should be concentrating on?

StringBuilder is a class that lets you make lots of additions to a string
much more quickly than using a simple String variable, and without using
nearly so much memory. If you're going to create a String variable and then
repeatedly append to it (perhaps to build up an HTML or SQL string), then
definitely use a StringBuilder instead of a String. For simple operations
such as this, a String is probably easier to use.

HTH,
 
Hexman said:
Well, getting to know VB.Net is shall we say challenging. When I
was coding in Vb (years ago) there used to be a "Right, Left, Mid"
string functions. Where did they all go?

They are still there. "Symbol search" for right, left, mid and you'll see
them. Even the Mid statement is still there (although it hides that strings
are immutable; see below).
I need to take a file path and change a couple of characters in it:

an example: (change R to J)

c:\Data\Finance\am051022.Rcc
to
c:\Data\Finance\am051022.Jcc

I've searched on the internet and found reference to the
StringBuilder class. Is that what I should be concentrating on?

You can. Mainly useful if you want to manipulate one string frequently, or,
like the name says, want to build a string.

Please note that strings are immutable in .Net. You can not even change a
single letter.
I'm sure someone has already built a comprehensive class for
strings. Want to share it?

Yes, MSFT did: System.String

See also:
http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconStringsInVisualBasic.asp
Any good examples out there? Recommendations please.

They also invented some other useful classes, e.g. System.IO.Path:

Dim p As String
Dim ext As String

p = "c:\Data\Finance\am051022.Rcc"
ext = System.IO.Path.GetExtension(p).Replace("R"c, "J"c)
p = System.IO.Path.ChangeExtension(p, ext)

I assume, you only want to replace the R's in the extension.


Armin
 
Hexman said:
Well, getting to know VB.Net is shall we say challenging. When I was
coding in Vb (years ago) there used to be a "Right, Left, Mid" string
functions. Where did they all go?

I need to take a file path and change a couple of characters in it:

an example: (change R to J)

c:\Data\Finance\am051022.Rcc
to
c:\Data\Finance\am051022.Jcc

I've searched on the internet and found reference to the StringBuilder
class. Is that what I should be concentrating on?

I'm sure someone has already built a comprehensive class for strings.
Want to share it?

Some people have recommended using sPath.Replace("R", "J") That is dandy
unless there is an "R" elsewhere in your path :-) Might I suggest first
extracting the extension....

Dim FileExtension As String = Path.GetExtension(sPath)
Dim FilePath As String = Path.GetPathRoot(sPath)
Dim FileName As String = Path.GetFileNameWithoutExtension(sPath)

FileExtension = FileExtension.Replace("R", "J")
sPath = String.Concat(FilePath, "\", FileName, FileExtension)

Path.GetExtension gets the extension of the file path, including the
leading "." character. Path.FileName returns the name of the file without
the extension and Path.GetPathRoot returns everything up to the file part
of the path, minus the trailing "\". You can do what you want with the
extension and *just* the extension, then reassemble the full path. The
Path object is part of the System.IO namespace and the methods given are
shared, so there is no need to create an instance of Path. The Concat
method of String supports up to four string parameters; if you need to use
more, put them in to a string array in the order of concatination (left to
right) and pass that instead. I think that, since Concat is shared, using
FilePath.Concat("\", FileName, FileExtension) will only invoke the base
String.Concat and will not include FilePath in the result.
 
Thanks to all that replied. It is much clearer now and I now able to
search better due to the help from this group (pointing me in the
right direction)..

The example I gave was just that - an example. In the past I have
done quite a bit of string manipulation and don't forsee an end to
that under vb.net. It was not to focus on the changing of a file
extension, but rather to find out how to work with strings.

I resolved my sting issues using the system.string class.

And yes, as one person put it, "I want to move to the ".Net way" of
doing thisngs".

Thanks for the quick response and help from all.

Hexman
 
Hexman said:
And yes, as one person put it, "I want to move to the ".Net way" of
doing thisngs".

I am curious why you are not using IL assembler instead of VB.NET if you
want to do everything the ".NET way".

SCNR
 
Hexman,

The Net way as is said is using the .Net namespace.

Mid, Left and Right are more of those are completely part of .Net.

(I don't like them because of the "one" starting idexer that is used, others
like it because of that, however that is an other reason)

It is by instance like ADO, which is not a part of NET.

Cor
 
Back
Top