I have this string:
Dim stringToCleanUp As String = "bon;jour"
Dim characterToRemove As String = ";"
I want a function who removes the ';' character like this:
Function RemoveCharacter(ByVal stringToCleanUp, ByVal characterToRemove)
...
End Function
What would be the function ?
ANSWER:
Dim cleanString As String = Replace(stringToCleanUp, characterToRemove, "")
Great, Thanks!
The String class has a Replace method that will do that.
Dim clean as String
clean = myString.Replace(",", "")
Function RemoveCharacter(ByVal stringToCleanUp, ByVal characterToRemove)
' replace the target with nothing
' Replace() returns a new String and does not modify the current one
Return stringToCleanUp.Replace(characterToRemove, "")
End Function
Here's more information about VB's Replace function
The string class's Replace method can also be used to remove multiple characters from a string:
Dim newstring As String
newstring = oldstring.Replace(",", "").Replace(";", "")
You can use the string.replace method
string.replace("character to be removed", "character to be replaced with")
Dim strName As String
strName.Replace("[", "")
Related
I have written a function to extract a recieved token from a "xx":"..." format
Public Class HelperFunctions
Public Function ExtractToken(ByVal myToken As String) As String
'Split fields on comma
Dim fields = myToken.Split(":")
'Quote literal
Dim quote = """"c
'Use trim to remove quotes
Dim value = fields(2).Trim(quote)
Return value
End Function
End Class
But instead of initializing the function
Dim hc as New HelperFunctions
hc.ExtractToken(_string)
I want to use it straight forward
HelperFunctions.ExtractToken(_string)
I have not programmed for a while and cannot figure it out as well as come up with the name of this type of functions to find a tutorial.
You need to declare the Function as Shared:
Public Class HelperFunctions
Public Shared Function ExtractToken(ByVal myToken As String) As String
'Split fields on comma
Dim fields = myToken.Split(":")
'Quote literal
Dim quote = """"c
'Use trim to remove quotes
Dim value = fields(2).Trim(quote)
Return value
End Function
End Class
Or as #jmcilhinney said, you can use Module and you don't need to use Shared in the methods inside it (also you can't create an instance of an object from a Module):
Public Module HelperFunctions
Public Function ExtractToken(ByVal myToken As String) As String
'Split fields on comma
Dim fields = myToken.Split(":")
'Quote literal
Dim quote = """"c
'Use trim to remove quotes
Dim value = fields(2).Trim(quote)
Return value
End Function
End Module
I have a string like this:
+COPS:0,0,"XL",2 or +CSQ: "15",99
How can I get "XL" or "15" from the string.
On the second one, I've tried using replace and remove +CSQ: and ,99 but I can't do this on the first one.
For the first string use String.Split to split on commas then String.Trim to remove the quotes:
Dim line = "+COPS:0,0,""XL"",2"
' Split fields on comma
Dim fields = line.Split(",")
' Quote literal
Dim quote = """"c
' Use trim to remove quotes
Dim value = fields(2).Trim(quote)
Same spirit as #Phillip Trelford but avoiding one step splitting on quotes :
Dim line = "+COPS:0,0,""XL"",2"
' Split fields on quotes
Dim fields = line.Split(""""c)
Dim value = fields(1)
One liner function :
Public Function getValue(ByVal from As String) As String
Return from.Split(""""c)(1)
End Function
You could use Regular expressions for that task.
For example :
Dim stringtoscan = yourstringhere
Dim expression As New Text.RegularExpressions.Regex(Chr(34) + "(.*?)" + Chr(34))
Dim stringMatches = expression .Matches(stringtoscan)
I created a simple function designed to remove a string of characters from another string and replace it with what ever string the user wants (or no string as a default)
Private Function RemoveString(scontainer As String, Optional rcontainer As String = "", Optional rstring As String = "") As String
Dim container As String = scontainer
Dim tcontainer As String
If InStr(container, rcontainer) <> 0 Then
Do While (InStr(container, rcontainer) <> 0)
tcontainer = Microsoft.VisualBasic.Left(container, InStr(container, rcontainer) - 1)
tcontainer = tcontainer & rstring & Microsoft.VisualBasic.Right(container, (Len(container) - (InStr(container, rcontainer) + 2)))
container = tcontainer
Loop
RemoveString = container 'return modded string
Else
RemoveString = scontainer 'return string as is
End If
End Function
The problem is:
While this is suppose to be a general use function, I really need it to be concerned with 2 different strings
%20
amp;
the function works perfectly for the %20 situation but it leaves the semi-colon behind for the amp; string. Any ideas why this might be?
Do I get you right ?
You want to replace a certain char sequence in your string with another char sequence or just delete it.
If thats the case you could use String.Replace(oldValue As String, newValue As String) As String
Dim startString as String = "%20 amp;"
Dim resultString as String = startString.Replace("%20 ",String.Empty)
resultString = resultString.Replace(";",String.Empty)
After these lines resultString would be "amp"
I have a string xxx1-0.1/1 yyy,ccc1,1. I used split and substring. All i want to get is the 0.1/1 only. Is there any optimize way to do this?
Thank you in advance!
Use more than one string split chars. then if the position and format is always the same then this will work.
Dim s As String = "xxx1-0.1/1 yyy,ccc1,1"
Dim ans = s.Split(New Char() {"-"c, " "c})(1)
MessageBox.Show(ans)
Lets make it a function:
Private Function getMySpecialValue(input As String) As String
Return input.Split(New Char() {"-"c, " "c})(1)
End Function
These are valid characters:
a-z
A-Z
0-9
-
/
How do I remove all other characters from my string?
Dim cleanString As String = Regex.Replace(yourString, "[^A-Za-z0-9\-/]", "")
Use either regex or Char class functions like IsControl(), IsDigit() etc. Get a list of these functions here: http://msdn.microsoft.com/en-us/library/system.char_members.aspx
Here's a sample regex example:
(Import this before using RegEx)
Imports System.Text.RegularExpressions
In your function, write this
Regex.Replace(strIn, "[^\w\\-]", "")
This statement will replace any character that is not a word, \ or -. For e.g. aa-b#c will become aa-bc.
Dim txt As String
txt = Regex.Replace(txt, "[^a-zA-Z 0-9-/-]", "")
Function RemoveCharacter(ByVal stringToCleanUp)
Dim characterToRemove As String = ""
characterToRemove = Chr(34) + "#$%&'()*+,-./\~"
Dim firstThree As Char() = characterToRemove.Take(16).ToArray()
For index = 1 To firstThree.Length - 1
stringToCleanUp = stringToCleanUp.ToString.Replace(firstThree(index), "")
Next
Return stringToCleanUp
End Function
I've used the first solution from LukeH, but then realized that this code replaces the dot for extension, therefore I've just upgraded the code slightly:
Dim fileNameNoExtension As String = Path.GetFileNameWithoutExtension(fileNameWithExtension)
Dim cleanFileName As String = Regex.Replace(fileNameNoExtension, "[^A-Za-z0-9\-/]", "") & Path.GetExtension(fileNameWithExtension)
cleanFileName will the file name with no special characters with extension.