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.
Related
What would be the efficient way to read a paragraph, and replace anything between square brackets []? In my case following paragraph,
I agree to [Terms of Use|https://www.google.com/terms-use], [Privacy Statement|https://www.google.com/privacy-statement] and [Misc Term|https://www.google.com/misc-terms]
should parsed as,
I agree to Terms of Use, Privacy Statement and Misc Term
You can use the following regular expression pattern to match the brackets:
\[[^]]+]
This is what the pattern means:
\[ match an open bracket
[^]]+ match anything but a closed bracket one or more times
] match a closed bracket
Here is an example fiddle: https://regex101.com/r/Iyk9kR/1
Once you have the matches, you would:
Use Regex.Replace (documentation)
Use the MatchEvaluator overload
In the MatchEvaluator, use String.Split (documentation) on the pipe character
Dynamically build your anchor tag by setting the href attribute to the second match of the split and the innerHTML to the first match of the split
It might be worth adding some conditional checking in between steps 2 and 3, but I'm not sure of your exact requirements.
Here is an example:
Imports System
Imports System.Text.RegularExpressions
Public Module Module1
Public Sub Main()
Dim literal = "I agree to [Terms of Use|https://www.google.com/terms-use], [Privacy Statement|https://www.google.com/privacy-statement] and [Misc Term|https://www.google.com/misc-terms]"
Dim regexPattern = "\[[^]]+]"
Dim massagedLiteral = Regex.Replace(literal, regexPattern, AddressOf ConvertToAnchor)
Console.WriteLine(massagedLiteral)
End Sub
Private Function ConvertToAnchor(m As Match) As String
Dim matches = m.Value.Split("|"c)
Return "" & matches(0).Substring(1) & ""
End Function
End Module
Fiddle: https://dotnetfiddle.net/xUE8St
Dim rtnStr = "String goes here"
Dim pattern As String = "\[.*?\]"
If Regex.IsMatch(rtnStr, pattern) Then
Dim matches = Regex.Matches(rtnStr, pattern, RegexOptions.IgnoreCase)
For index As Integer = 0 To matches.Count - 1
If matches(index).ToString().Contains("|") Then
Dim splitBracket = matches(index).ToString().Split("|")
Dim linkName = String.Empty
Dim linkUrl = String.Empty
If splitBracket.Length > 0 Then
linkName = splitBracket(0).Replace("[", "")
linkUrl = splitBracket(1).Replace("]", "")
End If
Dim linkHtml = "<a class=""terms"" href=""javascript: void(0);"" data-url=" + linkUrl + ">" + linkName + "</a>"
rtnStr = rtnStr.Replace(matches(index).ToString(), linkHtml)
End If
Next
End If
#Html.Raw(rtnStr)
I have a file name like below:
sub_fa__hotchkis_type1a__180310__PUO4x4__180813
I want to separate it with double underscores "__" and using this code:
Dim MdlNameArr() As String = Path.GetFileNameWithoutExtension(strProjMdlName).Split(New Char() {"__"}, StringSplitOptions.RemoveEmptyEntries)
myTool.Label9.Text = MdlNameArr(1).ToString
I expect the result will be "hotchkis_type1a" but it returns "fa".
It doesnt recognize single underscore "_".
Is there any method to use it properly?
You need to split on a string rather than just a character, so if we look at the available overloads for String.Split, we find the nearest one to that is String.Split(string(), options) which takes an array of strings as the separators and requires the inclusion of StringSplitOptions like this:
Dim s = "sub_fa__hotchkis_type1a__180310__PUO4x4__180813"
Dim separators() As String = {"__"}
Dim parts = s.Split(separators, StringSplitOptions.None)
If parts.Length >= 2 Then
Console.WriteLine(parts(1))
Else
Console.WriteLine("Not enough parts found.")
End If
Outputs:
hotchkis_type1a
I have problem in removing word in my array because it is a generated GUID.
And now I need is to remove 1 of word or item in that string.
Here is my String:
Dim guid_id as string ='3a0eed1f-73b2-11e0-8670-88006707ed92','3a125s34-73b2-11e0-8670-88006707ed92','3a112w3s-73b2-11e0-8670-88006707ed92'
Q: How can i remove the word or string " '3a112w3s-73b2-11e0-8670-88006707ed92' " in that 1 whole string?
I have idea that I need to convert it into List(of String) but I don't know how to remove it in that list.
You can simply find and replace the string using Replace function
Dim guid_id as string = "'3a0eed1f-73b2-11e0-8670-88006707ed92','3a125s34-73b2-11e0-8670-88006707ed92','3a112w3s-73b2-11e0-8670-88006707ed92'"
Dim strRemove As String = "'3a112w3s-73b2-11e0-8670-88006707ed92'"
guid_id = guid_id.Replace(strRemove, "").Trim()
If guid_id.Subtring(0,1) = "," Then guid_id = guid_id.Substring(1);
If guid_id.Subtring(guid_id.Length-1) = "," Then guid_id = guid_id.Substring(0, guid_id.Length-1);
.NET Framework has an engine for text processing, which is represented by the System.Text.RegularExpressions.Regex. You can use it to replace a specific word in a string.
Try this code:
Dim guid_id As String = "'3a0eed1f-73b2-11e0-8670-88006707ed92','3a125s34-73b2-11e0-8670-88006707ed92','3a112w3s-73b2-11e0-8670-88006707ed92'"
Dim strRemove As String = "'3a112w3s-73b2-11e0-8670-88006707ed92'"
'To remove strRemove from string, we use Regex replace method
Dim regex = New Regex(strRemove, RegexOptions.IgnoreCase)
guid_id = regex.Replace(guid_id, "")
'Now we remove 'Comma' from string, if it is needed.
If guid_id.Subtring(0, 1) = "," Then guid_id = guid_id.Substring(1)
If guid_id.Subtring(guid_id.Length - 1) = "," Then guid_id = guid_id.Substring(0, guid_id.Length - 1)
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 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("[", "")