String functions

kubball
07-15-2003, 03:45 PM
Hello,

Just a stringy question.

How do I take a hyphen ("-") out of a string? For example, LP-2A to just LP2A. I tried to split it and rejoin it, but don't know how to properly use the split function . . .

Thanks!

kubball.

jauwaert
07-15-2003, 03:52 PM
Hello,

Just a stringy question.

How do I take a hyphen ("-") out of a string? For example, LP-2A to just LP2A. I tried to split it and rejoin it, but don't know how to properly use the split function . . .

Thanks!

kubball.

Split returns an array of strings, with each part of the original string broken into an element of the array. If you know that there's only one hyphen in a given string, you can use:


Sub MergeString()

Dim StringParts() As String
Dim StringWithoutHyphen As String

StringParts = Split("ash-asjdf", "-") 'first argument is string, second is delimiter
StringWithoutHyphen = StringParts(0) + StringParts(1)

End Sub

italkid
07-16-2003, 03:45 AM
A better idea would be to make use of the substitute function :

Private Sub CommandButton1_Click()
Dim Olds As String, News As String

Olds = "LP-2A"
News = WorksheetFunction.Substitute(Olds, "-", "")

MsgBox News
End Sub

This function can replace the "-" character even if you dont know its position.
In this example it will even replace all the "-" (if there would be more then one).

Agent707
07-16-2003, 08:59 AM
A function I use in Non-Excel apps to remove or replace characters is:

Function RemoveCharacter (txtString)'String to pass in to remove desired character
Dim lvl_count

If txtString <> "" Then
' Loop through each character in string
For lvl_count = 1 To Len(txtString)

' Case check on each character
Select Case Mid(txtString, lvl_count, 1)

' Don't keep this character
' 45 = "-"
Case Chr(45)
' Do nothing

' All other characters
Case Else
' Keep the character
RemoveCharacter = RemoveCharacter & Mid(txtString, lvl_count, 1)

End Select
Next
Else
RemoveCharacter = ""
End If

End Function

I modified it to do what you were wanting to do, which is get rid of the "-"

You can use this function to get rid of any character, or replace anything.
I also use it for webpages to replace carriage returns ( Chr(10) )with "<BR>" ;)

Hope that helps.

Snail1985
07-16-2003, 01:43 PM
Sorry to pee on your barbeque but you could just use the "Replace" function in VB as follows

Dim MyStr As String

...

MyStr = Replace(MyStr, "-", "")

Agent707
07-16-2003, 04:13 PM
That's pathetic. :mad:

No, not your suggestion... where I got the function I posted...

Do a search on MSDN for this
(Extending Visual Basic to the Internet)

You'll find a page where they show you how to do this.... Using the method I posted...

It just clarifies that it's the MORONS who work from MS.

They could have just put in that lesson to use the function You posted and saved a LOT of people a lot of headaches.

*I can't believe I even read that crap anymore to learn*

Thanks Snail!

Oh, and it's not nice to put it the way you did "pee on your BBQ" :p Be a little more tactful if you can. ;)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum