Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

String.Remove(int) broken?

SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
Alright, I need to strip the four-character extension from the end of a filename, but VB.NET 2005 seems to have broken the "Remove()" method for strings. If not, what is wrong here. If it is broken, how the heck can I do this? In C++ it's simple, but VB.NET makes it hard by using a string class.
[code]
'Setup the configuration filename
arrayString = Me.exeString.Split("".ToCharArray())
fileString = ".Config" & arrayString(arrayString.Length() - 1)
fileString.Remove(fileString.Length() - 4)
fileString += ".486"
[/code]
Assume that "Me.exeString" was set to "C:MyAppMyApp.exe" for this example. The "fileString" variable is somehow being set to "MyApp.exe.486" instead of "MyApp.486" even though I have used the remove method to strip the last four characters from the string. Ideas?

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • zibadianzibadian Member Posts: 6,349
    : Alright, I need to strip the four-character extension from the end
    : of a filename, but VB.NET 2005 seems to have broken the "Remove()"
    : method for strings. If not, what is wrong here. If it is broken,
    : how the heck can I do this? In C++ it's simple, but VB.NET makes it
    : hard by using a string class.
    : [code]:
    : 'Setup the configuration filename
    : arrayString = Me.exeString.Split("".ToCharArray())
    : fileString = ".Config" & arrayString(arrayString.Length() - 1)
    : fileString.Remove(fileString.Length() - 4)
    : fileString += ".486"
    : [/code]:
    : Assume that "Me.exeString" was set to "C:MyAppMyApp.exe" for this
    : example. The "fileString" variable is somehow being set to
    : "MyApp.exe.486" instead of "MyApp.486" even though I have used the
    : remove method to strip the last four characters from the string.
    : Ideas?
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/gre
    : en][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h
    : [/red][/b][/italic]

    It could be that Remove() creates a new string instance, which has the characters removed. You should check the documentation on that.
  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    : It could be that Remove() creates a new string instance, which has
    : the characters removed. You should check the documentation on that.

    I thought that as well, but that is not what the documentation says.

    *EDIT*

    That is how it works. The documentation claims that it simply removes x characters starting from position x. It does not mention returning anything.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
  • zibadianzibadian Member Posts: 6,349
    : : It could be that Remove() creates a new string instance, which has
    : : the characters removed. You should check the documentation on that.
    :
    : I thought that as well, but that is not what the documentation says.
    :
    : *EDIT*
    :
    : That is how it works. The documentation claims that it simply
    : removes x characters starting from position x. It does not mention
    : returning anything.
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/gre
    : en][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h
    : [/red][/b][/italic]

    Then I would suggest stepping through your code and placing some watches to see if fileString really contains what you think it contains. Alternatively place some print statements to show the contents of fileString on screen to see what's happening.

    Here's another discussion on this problem: http://forums.msdn.microsoft.com/en-US/vbgeneral/thread/b596f182-b5a8-4004-8b73-2469b44a54fb/

    Perhaps it helps you.
Sign In or Register to comment.