Disecting Strings

  • Thread starter Thread starter HardySpicer
  • Start date Start date
H

HardySpicer

Suppose I have a string "mystring" of the form


mystring="c:\dir1\dir2\dir3\artist - title.mp3"

How can I strip the artist and song title from this in VB .net?

Thanks

Hardy
 
HardySpicer said:
Suppose I have a string "mystring" of the form


mystring="c:\dir1\dir2\dir3\artist - title.mp3"

How can I strip the artist and song title from this in VB .net?

Thanks

Hardy

tempstring = IO.Path.GetFileNameWithoutExtension(mystring)

'Providing there is only one -
Dim ArtTit As String() = tempstring.Split("-")
artist = ArtTit(0).Trim
title = ArtTit(1).Trim


Chris
 
Suppose I have a string "mystring" of the form

mystring="c:\dir1\dir2\dir3\artist - title.mp3"

How can I strip the artist and song title from this in VB .net?

Thanks

Hardy

Though I don't recommend it for this example (I prefer using the Path
object too) remember you can use Regex to parse strings if you need
some very specific matching requirements. Just remember that Regex is
hard on maintenance due to the cryptic match strings and is often more
costly in performance than other methods of string parsing.

Thanks,

Seth Rowe
 
Hallo,

Though I don't recommend it for this example (I prefer using the Path
class too) remember you can use two times indexofLast in combination with
substring too

http://msdn2.microsoft.com/en-us/library/system.string.lastindexof(VS.71).aspx

Something as this typed in this message

dim myindex as integer = mystring.lastindexof(".")
dim myartist as string

if myindex.lastindexof(".") <> -1 then
mynewstring = mystring.substring(0,myIndex)
myindex = mynewstring.lastindexof(".")
if myindex.lastindexof(".") <> -1 then
myartist = mynewstring(myindex)
endif
endif

It is not checked however a direct answer on your question what I would
never use.

Cor
 
Just to expand on Rick's answer, if you wanted to store the artist and
title separately then you could do something like:

(Note - handwritten code - may not compile!!)
dim artist, title, both as string
both = System.IO.Path.GetDirectoryName(<fileName>)
artist = String.Split(both, "-")(0).Trim()
title = String.Split(both, "-")(1).Trim()

Cheers,

RB.
 
Just to expand on Rick's answer, if you wanted to store the artist and
title separately then you could do something like:

(Note - handwritten code - may not compile!!)
dim artist, title, both as string
both = System.IO.Path.GetDirectoryName(<fileName>)
artist = String.Split(both, "-")(0).Trim()
title = String.Split(both, "-")(1).Trim()

Cheers,

RB.

Thank you - you have all been helpful.

Hardy
 
dim artist, title, both as string
both = System.IO.Path.GetDirectoryName(<fileName>)
artist = String.Split(both, "-")(0).Trim()
title = String.Split(both, "-")(1).Trim()

Cheers,

RB.

This is good but I get an error in compilation
Overload resolution failed because no accessible '<method>' can be
called without a narrowing conversion:


You have made a call to an overloaded method, but the compiler cannot
find a method that can be called without a narrowing conversion. A
narrowing conversion changes a value to a data type that might not be
able to precisely hold some of the possible values.

Bit confused about this.. It says to use Option Strict Off but it is
already off.

Hardy
 
This is good but I get an error in compilation
Overload resolution failed because no accessible '<method>' can be
called without a narrowing conversion:


You have made a call to an overloaded method, but the compiler cannot
find a method that can be called without a narrowing conversion. A
narrowing conversion changes a value to a data type that might not be
able to precisely hold some of the possible values.

Bit confused about this.. It says to use Option Strict Off but it is
already off.

Don't use Option Strict Off, it will only cause you trouble. Try
this:

dim artist, title, both, mystring as string
dim fname() as string

mystring="c:\dir1\dir2\dir3\artist - title.mp3"

fname = System.IO.Path.GetFileNameWithoutExtension(mystring)
both = fname.Split("-"c)
artist = both(0).Trim()
title = both(1).Trim()
 
Back
Top