How to Split String into Array for multiple characters - vb.net

Here is my old script, in vb
Dim strArray As String() = str.Split(New Char() {":"C})
This works fine if the char is only :
But now I want to split for this,
:.++.:
As my str is complicated is there a way to split it ?
Edit :
My
str = hello:.++.:sAwesome Right ? yeah://.,]['; :.++.:
nvijuds789g34huve02qjgv0b0whgvn0iegvb0wvi0hn
so after split
strArray(0) = hello
strArray(1)= sAwesome Right ? yeah://.,][';
strArray(2) = nvijuds789g34huve02qjgv0b0whgvn0iegvb0wvi0hn
I think you see what I need. I am the one adding :.++.: before each part in previous functions. It is because the text almost contains every single character and I can't control it :/
thats why i used some complicated combination of characters to make it impossible for the file to contain it

You can split a string by another string using the String.Split overload (String(), StringSplitOptions).
Example:
Debug.WriteLine(String.Join("|", "A:.++.:B:.++.:C:.++.:D".Split({":.++.:"}, StringSplitOptions.RemoveEmptyEntries)))
Outputs:
A|B|C|D

Related

How to insert delimiter in a string with kotlin

I have a mac string:
mac=7A2918D5434F
And I need to convert to this:
mac=7A:29:18:D5:43:4F
How can I do that in kotlin?
If you are sure that your initial string is correct you can do something like:
"7A2918D5434F".chunked(2).joinToString(":")
chunked(2) splits the string in chunks of size 2 (can be used for any Iterable).
jointToString(":") takes a list, joins the elements to string using : as delimiter

How to Trim right and left a String in VB .net

I want to take the value of
T.GS.+0.220kg
but I don't know how to remove the string.
I just want to take numbers from the weight.
like 0.220
Can someone help me ?
You can make use of the Regular Expressions to extract a decimal value from basically any string. First you'd need to import the library:
Imports System.Text.RegularExpressions
Then using this will return just the decimal value:
Regex.Match("T.GS.+0.220kg", "\d+.\d+").Value
This particular expression looks for a digit or digits, followed by a point (dot), followed by another number of digits, so the previous points (in between T and G for example) aren't included.
This returns exactly 0.220, you can then replace the string with any string variable and assign this expression as needed.
If you havn't worked with regular expressions before and want somthing that looks a little nicer. You could use the string.split method.
dim input as string = "T.GS.+0.220kg"
input = input.split("+")(1) ' which will grab the "0.220kg"
input = input.substring(0, input.length - 2) ' then filter off the last 2 chars
In english:
split the string into 2 seperate pieces grabing the part to the right of the first '+' symbol.
Then remove the last 2 chars from the end.

how to correct incoming data that contains char not in Ascii to unicode before saving to the database

I have a webservice api in vb.net that accepts string. but i cannot control the data coming to this API. I sometimes receive chars in between words in this format (–, Á, •ï€,ââ€ï€, etc. ) Is there a way for me to handle these or convert these characters to their correct symbols before saving to the database?
i know that the best solution would be to go after the source where the characters get malformed.. but i'll make that as plan B
my code is already using utf-8 as encoding pattern, but what if the client that uses my API messed up and inadvertently sent the malformed char thru the API. can i clean that string and convert the malformed char to the correct symbol?
If you only want to accept ASCII characters, you could remove non-ASCII characters by encoding and decoding the string - the default ASCII encoding uses "?" as a substitute for unrecognized characters, so you probably want to override that:
' Using System.Text
Dim input As String = "âh€eÁlâl€o¢wïo€râlâd€ï€"
Dim ascii As Encoding = Encoding.GetEncoding(
"us-ascii",
New EncoderReplacementFallback(" "),
New DecoderReplacementFallback(" ")
)
Dim bytes() As Byte = ascii.GetBytes(input)
Dim output As String = ascii.GetString(bytes)
Output:
h e l l o w o r l d
The replacement given to the En/DecoderReplacementFallback can be empty if you just want to drop the non-ASCII characters.
You could use a different encoding than ASCII if you want to accept more characters - but I would imagine that most of the characters you listed are valid in most European character sets.
While you are kind of vague I could guide you in something I think you could potentially do:
Sub Main()
Dim allowedValues = "abcdefghijklmnopqrstuvwxyz".ToCharArray()
Dim someGoodSomeBad = "###$##okay##$##"
Dim onlyGood = New String(someGoodSomeBad.ToCharArray().Where(Function(x) allowedValues.Contains(x)).ToArray)
Console.WriteLine(onlyGood)
End Sub
The first line would be valid characters, in my example I chose to use alpha characters, you could add more characters and numbers too. Basically you are creating a whitelist of acceptable characters, that you the developer would make.
The next line would be an output from your API that has some good and some bad lines.
The next part is really more simple than it looks. I am extending the string to be an array of characters, then I am finding ONLY the characters that match my whitelist in a lambda statement. Then I extend this to an array again because if I do a new String in .NET from a char array.
I then get a good string, but I could make 'good' to be subjective based on a whitelist.
The bigger question though is WHY is your Web API sending garbled data over? It should be sending well formed JSON or XML that is then able to well parsed and strongly type to models. Doing what I have shown above is more of a bandaide than a real fix to the underlying problem and it will have MANY holes.

How Do I get this Split Function to Work? (VB.NET)

So, I made a program that for the most part, converts numbers to letters. My problem before was it was converting each individual digit instead of each number e.g. (1-0-1 instead of 101). Someone suggested that I use the Split function:
Dim numbers As String() = DTB.Split(" ")
So now it's reading the number all the way through being that it will only the split if there's a space in between. My problem now is that it's translating for example: "[102, 103, 104]" as "[102", "103" and "104]" because it will only split if there's a space between. Obviously, you can't convert "[102" or "104]" because they aren't actual numbers.
Does anyone have a solution on what I should do to get this to convert no matter the spacing? Would Regex be the way to go?
use a regular expression with \d+ it will match numbers
so
12234abcsdf23434
will return two matches
12234
23434

Code for converting long string to pass in URL

I am trying to take a string like "Hello my name is Nick" and transform it to "Hello+my+name+is+Nick" to be passed through a URL. This would be easily done by replacing all the spaces with a + char however I also need to replace all special characters (. , ! &) with their ASCII values. I have searched the net but cannot find anything. I wonder if anyone knows of existing code to do this as its a fairly common task?
I think you're looking for this: HttpUtility.UrlEncode Method (String)
Handles non-URL compliant characters and spaces.