Changing Unicode character to string - vb.net

I have to following string:
Dim text As String = "userĖ˛upload"
I want to change the Unicode character #0332 to underscore. I have the following code for this:
Dim test As New Text.StringBuilder
test.Append(text.Replace("#0332", "_"))
Dim normalizedUrl As String = test.ToString()
However it does not work, my "test" string has the same value as "text" variable. Anyone has an idea what can be wrong?

You can;
text.Replace(ChrW(&H332), "_")

Related

How to split on a string instead of a character?

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

VB: Search in String

How do I search in a string from ";" to ";"?
Dim strInput as String = "text1;text2;text3"
Solution should look like this:
strOutput1 = "text1"
strOutput2 = "text2"
strOutput3 = "text3"
The lenght of the single "parts" is not fix, strInput can also be like "12345;name;Christoph;"
I just want to get the parts in a own string.
Does anybody knows how to?
Quickest way is probably using SPLIT to populate an array then set the individual variables to each element in the array. The only issue that might arise is if you have ;'s inside the strings, but that would pretty much prevent anything not based on length.
Starting with your string:
Dim strInput as String = "text1;text2;text3"
Then do a split:
Dim inputArray() As String = Split(strInput, ";")
This will split your string up using ; as the delineator. So you will end up with this:
inputArray(0) = "text1"
inputArray(1) = "text2"
inputArray(2) = "text3"
More on split()...
https://msdn.microsoft.com/en-us/library/6x627e5f%28v=vs.90%29.aspx?f=255&MSPPError=-2147217396
Using .Split you can separate the original string into an array of strings like this
Dim strInput As String = "text1;text2;text3"
Dim tempStringList() As String = strInput.Split(";"c)
So now
tempStringList(0) contains "text1"
tempStringList(1) contains "text2"
and so on.
The beauty of .split is that it doesnt care how many sections there are to separate.

Removing the double quotes from a string

I have a variable images that is a String. The value of the images are in this format:
"['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']"
How can I convert the value of the images to something without the double quotes in the beginning and ending (or should I change the String into some other types of variables? The format that I want is this:
['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']
No double quotes
Thanks
The String class has a Replace method that will do that.
Dim clean as String
clean = images.Replace("""", "")
Try this...
Dim s as String = "['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']"
s = s.Replace("'", "").Trim()
Any character or word or phrase or even a sentence is considered a string when it's enclosed in double quotes, but if the value of your string literally has double quotes, like this "SAMPLE", and you want to remove the double quotes, then do something...
Dim s as String = ""['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']""
s = s.Replace("""", "").Trim()
Yes I noticed...double double quotes...I equated s to something that you say is passed from MATLAB, the string literally has double quotes, so to remove this, you replace double double quotes with nothing. That's how you do it in .NET. Your compiler interprets double double quotes as just a single quote :)
This worked for me: (Line had a double quote that I didn't want)
Line = Line.Replace(ChrW(34), "").Trim()
Based on the OP's comments I think there is some confusion. Maybe you are new to .Net, coming from MATLAB.
To re-iterate in VB or C# .Net this is an Empty string:
""
that is a string with length == 0. That is something distinct from a string with the value of a quote or double quote.
To continue:
Dim singleQuoteString As String = "'"
Dim doubleQuoteString As String = """"
and so, what you are asking for
['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']
is not a string.
Now, if you want a Textbox, , console output, or something else to show that value then just take your string, like
Dim listOfImagesAsAString = "['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']"
and assign it to where you want it, like
Console.Write(listOfImagesAsAString)
then the command prompt will show what you are asking for.

Avoid escaping all double-quotes in strings?

In a string that has multiple double-quotes, is there an easier solution than escaping each and everyone of them?
For instance, here's an HTML string:
Dim test As Regex = New Regex("^<div class="blah">\r\n<div class="blah"></div>\r\n</div>\r\n<div class="blah">\r\n(.+?)\r\n<div> class="blah">\r\n", RegexOptions.Singleline)
Alternatively, to hold the string, can VB.Net be told to use another character than the double-quote, eg.
New Regex(#my string="" my other string=""#)
?
Thank you.
If you are certain that the # symbol does not appear in your string...
Dim s As String = Replace("^<div class=#blah#>\r\n<div class=#blah#></div>\r\n</div>\r\n<div class=#blah#>\r\n(.+?)\r\n<div> class=#blah#>\r\n", "#", """")
Dim test As Regex = New Regex(s, RegexOptions.Singleline)

Remove special characters from a string

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.