Strip all punctuation from a string in VB.net - vb.net

How do I strip all punctuation from a string in vb.net? I really do not want to do stringname.Replace("$", "") for every single bit of punctuation, though it would work.
How do i do this quickly and efficiently?
Other than coding something that codes this for me....

You can use a regular expression to match anything that you want to remove:
str = Regex.Replace(str, "[^A-Za-z]+", String.Empty);
[^...] is a negative set that matches any character that is not in the set. You can just put any character there that you want to keep.

Quick example using a positive regex match. Simply place the characters you want removed in it:
Imports System.Text.RegularExpressions
Dim foo As String = "The, Quick brown fox. Jumped over the Lazy Dog!"
Console.WriteLine(Regex.Replace(foo,"[!,.\"'?]+", String.Empty))

If you want a non-regex solution, you could try something like this:
Dim stringname As String = "^^^%%This,,,... is $$my** original(((( stri____ng."
Dim sb As New StringBuilder
Dim c As Char
For Each c In stringname
If Not (Char.IsSymbol(c) OrElse Char.IsPunctuation(c)) Then
sb.Append(c)
End If
Next
Console.WriteLine(sb.ToString)
Output is "This is my original string".

Related

Visual Basic removing remaing string after delimiter

I'm trying to remove the last part of the string to make it more presentable. The string looks like this
Stringname.number.RemainingStringTobeRemoved
Is there a way to remove the last part without using string.Substring(0,string.Length-10)?
I want it to be dynamic, the string I'll be removing is not a constant number.
My initial idea was to use delimiter to identify the starting point of the part I want to remove.
My initial idea was using delimiter to identify the starting point of the part I want to remove.
That's a good idea if your string is always the same format and the substring you want to remove does not contain that delimiter. Take a look at LastIndexOf.
Spoiler:
Dim s = "Stringname.number.RemainingStringTobeRemoved"
Dim r = s.Substring(0, s.LastIndexOf("."))
You can use Split to achieve this
String s = "Stringname.number.RemainingStringTobeRemoved"
Dim words As String() = s.Split(New Char() {"."c})
String RequiredString = words(0) & words(1)
The RequiredString is what you need
Using a delimiter is one way. You can use the Split method to split the string into parts at the ..
Dim FullString As String = "Stringname.number.RemainingStringTobeRemoved"
Dim StringParts() As String = Strings.Split(FullString, ".", 3)
Return StringParts(0) & StringParts(1)
The Split method returns an array of String that contains the parts. The array will contain the following items
Stringname
number
RemainingStringTobeRemoved
The delimiters are omitted.

VB.net get specific characters from listbox

I want to get specific characters from listbox, but I don't know how to do it properly. I already used search (tried because I don't know how properly to name) but get nothing.
So i have this line in my listbox:
1,2014-01-01,Text,Text,XYZ123,Text,Text
How do i need to get only XYZ123? Its always same format, 3 letters and 3 numbers.
Thank you.
I would use a Regular Expression
The Regex of XYZ123 = \w{3}\d{3}
First solution:
Based on a small console application:
Dim i As String = "1,2014-01-01,Text,Text,**XYZ123**,Text,Text"
For Each Str As String In i.Split(",")
Dim match As Match = Regex.Match(Str, "\w{3}\d{3}")
If match.Success Then
Console.WriteLine(Str)
End If
Next
Console.ReadLine()
Second (better) solution:
Based on the comment of Chinz (all credits belong to him)
Dim i As String = "1,2014-01-01,Text,Text,**XYZ123**,Text,Text"
Console.WriteLine(Regex.Match(i, "\w{3}\d{3}").Value)
Console.ReadLine()
if all the strings have the same overall format you could split on "**" and get the [1] from the plitted

how to extract a quote from a quote generator in Vb.net

im trying to extract a quote from this quote gen URL 'http://www.quotedb.com/quote/quote.php?action=random_quote'. i need it to extract JUST the quote and optionally the person who made the quote. this is an example reply from the generator.
document.write('When nothing seems to help, I go and look at a stonecutter hammering away at his rock perhaps a hundred times without as much as a crack showing in it. Yet at the hundred and first blow it will split in two, and I know it was not that blow that did it, but all that had gone before.');
document.write('More quotes from Jacob August Riis');
I know i need to parse it to extract the quote itself but im not to sure how to so this.
I know how to download the string of the quote but not how to extract it. So this is all i have currently:
Dim Cient As New System.Net.WebClient
Dim grab = Cient.DownloadString("http://www.quotedb.com/quote/quote.php?action=random_quote")
any help is greatly appreciated!
Someone else could probably come up with more elegant regular expressions, but this should work. Just a couple of regular expressions to extract the parts of the returned data that you are interested in.
Dim quote = RegEx.Matches(grab, "document\.write\('(.*?)<br>'\);")(0).Groups(1).Value
Dim author = RegEx.Matches(grab, "document\.write\('<i>.*?>(.*?)</a></i>'\);")(0).Groups(1).Value
I'm not a fan of parsing HTML with Regex, but since all of these come back with the same grammar so to speak, we can consider it regular for this case.
Dim pattern As String = <![CDATA[document\.write\('(?<quote>.*)<br\>'\);\ndocument\.write\('.*href=\"(?<url>[^\"]*)\">(?<author>[^<]*)</a>.*'\).*]]>.Value
Dim quoteRegex As New Regex(pattern, RegexOptions.Compiled Or RegexOptions.IgnoreCase Or RegexOptions.Singleline)
Dim Cient As New System.Net.WebClient
Dim grab = Cient.DownloadString("http://www.quotedb.com/quote/quote.php?action=random_quote")
Dim matches As MatchCollection = quoteRegex.Matches(grab)
For Each m As Match In matches
Console.WriteLine("Quote: {0}", m.Groups("quote"))
Console.WriteLine("Author: {0}", m.Groups("author"))
Console.WriteLine("URL: {0}", m.Groups("url"))
Next
This finds the quote (text within the first document.write() ignoring the quotes and the <br> tag), the Author of the quote (the Textual display of the anchor tag) and then the URL for more quotes (the href attribute of the anchor)
I declared the pattern by using XML literals so that I didn't have to escape out all the quote characters.
Requires Imports System.Text.RegularExpressions

Extracting characters from an input string vb.net

Hey guys I'm stuck with this question. Please help.
I want to write a program that can extract alphabetical characters and special characters from an input string. An alphabetical character is any character from "a" to "z"(capital letters and numbers not included") a special character is any other character that is not alphanumerical.
Example:
string = hello//this-is-my-string#capetown
alphanumerical characters = hellothisismystringcapetown
special characters = //---#
Now my question is this:
How do I loop through all the characters?
(the for loop I'm using reads like this for x = 0 to strname.length)...is this correct?
How do I extract characters to a string?
How do I determine special characters?
any input is greatly appreciated.
Thank you very much for your time.
You could loop through each character as follows:
For Each _char As Char In strname
'Code here
Next
or
For x as integer = 0 to strname.length - 1
'Code here
Next
or you can use Regex to replace the values you do not need in your string (I think this may be faster but I am no expert) Take a look at: http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx
Edit
The replacement code will look something as follows although I am not so sure what the regular expression (variable called pattern currently only replacing digits) would be:
Dim pattern As String = "(\d+)?" 'You need to update the regular expression here
Dim input As String = "123//hello//this-is-my-string#capetown"
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, "")
Since you need to keep the values, you'll want to loop through your string. Keeping a list of characters as a result will come in handy since you can build a fresh string later. Then take advantage of a simple Regex test to determine where to place things. The psuedo code looks something like this.
Dim alphaChars As New List(Of String)
Dim specialChars As New List(Of String)
For Each _char As Char in testString
If Regex.IsMatch(_char, "[a-z]")) Then
alphaChars.Add(_char)
Else
specialChars.Add(_char)
End If
Next
Then If you need to dump your results into a full string, you can simply use
String.Join(String.Empty, alphaChars.ToArray())
Note that this code makes the assumption that ANYTHING else than a-z is considered a special character, so if needs be you can do a second regular expression in your else clause to test for you special characters in a similar manner. It really depends on how much control you have over the input.

VB.NET split string with quotation marks in it

Trying to split a line wherever "," appears (with the quotation marks). The problem is VB.NET uses " to start/end strings, so I tried using .Split(""",""") but that then splits it by " not ",".
Try something like this:
Dim TestToSplit As String = "Foo"",""Bar"
Dim Splitted() As String = TestToSplit.Split(New String() {""","""}, StringSplitOptions.None)
I just tested it and got an array with Foo And Bar. I hope this helps.
The Split function (the way you are using it) expects a Char. If you want to split on multiple characters you need to use a string array. (Seems to me another overload of a single string value would have been handy.)
This function splits a line of text and returns an array based on the delimiter you have specified. (Of course, you could make this more general purpose by passing in the separator array.)
Private Function SplitLine(ByVal lineOfText As String) As String()
Dim separator() As String = {""","""}
Dim result() As String
result = lineOfText.Split(separator, StringSplitOptions.None)
Return result
End Function
Another alternative I often find useful is this:
Regex.Split(textToSplit, """,""")
Lets you split on more complex criteria than an array of alternative separators.
To escape the "-character in VB.NET, use two: ""