How to separate data with a comma - vb.net

For example, I'm having a two set of vars with data type of string:
users = "Admin, Staff"
pass = "202cb9, caf1a"
These are vars with only normal string data type. The two vars above is generated, so I could only get these kind of data. The question is:
How can I separate those data by the commas (like Admin -> 202cb9, Staff -> caf1a) and then store them into an array.
users_array(0) = "Admin"
users_array(1) = "Staff"
pass_array(0) = "202cb9"
pass_array(1) = "caf1a"
Thank you.

You can use users.Split(New Char() {","c}) as in this link.
http://www.dotnetperls.com/split-vbnet

I have two options to solve this problem:
Dim users_array() As String = users.Split(New String() {", "}, StringSplitOptions.RemoveEmptyEntries)
and:
Dim pass_array() As String = Split(users, ", ")
IMO, it is better to use ,<space> as a separator string instead of just ,, to avoid getting <space>staff at index 1.
Here the first solution works for both C# and VB.Net and second one is specific to VB.Net.

User String.Split to break strings apart on a specific character. It returns an array.

Related

Vb.net, replace any number at the end of a string with a placeholder

I have many strings that have numbers at the end. The numbers can be of any size, for example:
myvar123
mysecondvar3
mythirdvar219107
The strings can have numbers even inside the name, not only at the end.
for example:
my2varable123
some123variable9480395
I would need to replace any number at the END with a placeholder. (NOT those inside the varname.)
for example:
my2varable123 should become: my2variable%placeholder%
some123variable9480395 should become: some123variable%placeholder%
The only way that comes to my mind is to go through the string using .right() and remove the char if it is numeric until I find the first non numeric char. Then in the end append the placeholder, but it looks like a lot of work for a rather simply problem.
Is there a better way to do this?
You could use a Regex for this.
Dim str As String = "some123variable9480395"
Dim pattern As String = "\d+$"
Dim replacement As String = "%placeholder%"
Dim rgx As Regex = New Regex(pattern)
Dim result As String = rgx.Replace(str, replacement)

visual studio split function issue

I try to call split function on visual studio as below and i expect it return me 2 item in array after split, but vb return 5 results from my coding. It is consider vb issue or my coding issue?
Whole string is "NAME":"ALICE"
Dim a As String = """NAME"":""ALICE"""
Dim b() As String = a.Split(""":")
Output I expected in array after split
(1) "NAME
(2) "ALICE"
You were using this overload of String.Split(Char[]). Note that takes an array of characters. String is convertible to an array of characters (and that's why you could compile) but it is not equal. Try putting Option Strict On at the top of your code. It won't compile as you have it anymore :)
When passing a single string, each character in the string is used to split. Including each " in your argument, ":. It will split on " and :. You can get around it by passing a string array to Split using this overload of String.Split(String[], SplitStringOptions). Pass a single element array like this
Dim b = a.Split({""":"}, StringSplitOptions.RemoveEmptyEntries)
Yes, that is exactly as you said,
"NAME
"ALICE"
Do you want to get rid of the quotes in the result? You can do this
Dim b = a.Split({":", """"}, StringSplitOptions.RemoveEmptyEntries)
Then it's this,
NAME
ALICE
Dim a As String = """NAME"":""ALICE"""
Dim b() As String = a.Split(":")
this is how it evaluates

VB.NET string manipulation

Looking for the simplest way to extract values from a string. For example consider the following:
Dim args As String = "/firstname:Bob /lastname:Jones"
To simplify, I need to be able to popup a box that says "Firstname = Bob" or "Lastname = Jones"
Have you tried using the Split method on a string. It should look something like this:
Dim arr() as string
arr=args.Split("/")
Dim i as integer
For i=0 to arr.GetLength(0)
arr(i)=arr(i).Trim.Replace(":", "=")
Next
I would use the Split function to create an array of words, then read them in order:
char[] sep = new char[2];
sep[0] = '/';
sep[1] = ':';
string values = "/firstname:Bob /lastname:Jones";
string[] sites = values.Split(sep);
foreach (string s in sites) {
Console.WriteLine(s);
}
This post is also useful!
http://www.techrepublic.com/article/easily-parse-string-values-with-net/6030362
Using Regex this pattern can help:
(?<identifier>[a-z]+)(?<value>[a-z]+)
See how it works.
You can iterate on all groups and can extract identifier and value.

Create a string and append text to it

Funny, I had a textbox and I could append strings to it.
But now I create a string like this:
Dim str As String = New String("")
And I want to append to it other strings. But there is no function for doing so. What am I doing wrong?
Concatenate with & operator
Dim str as String 'no need to create a string instance
str = "Hello " & "World"
You can concate with the + operator as well but you can get yourself into trouble when trying to concatenate numbers.
Concatenate with String.Concat()
str = String.Concat("Hello ", "World")
Useful when concatenating array of strings
StringBuilder.Append()
When concatenating large amounts of strings use StringBuilder, it will result in much better performance.
Dim sb as new System.Text.StringBuilder()
str = sb.Append("Hello").Append(" ").Append("World").ToString()
Strings in .NET are immutable, resulting in a new String object being instantiated for every concatenation as well a garbage collection thereof.
Another way to do this is to add the new characters to the string as follows:
Dim str As String
str = ""
To append text to your string this way:
str = str & "and this is more text"
Use the string concatenation operator:
Dim str As String = New String("") & "some other string"
Strings in .NET are immutable and thus there exist no concept of appending strings. All string modifications causes a new string to be created and returned.
This obviously cause a terrible performance. In common everyday code this isn't an issue, but if you're doing intensive string operations in which time is of the essence then you will benefit from looking into the StringBuilder class. It allow you to queue appends. Once you're done appending you can ask it to actually perform all the queued operations.
See "How to: Concatenate Multiple Strings" for more information on both methods.

How to split in VB.NET

I am using VB.NET code.
I have got the below string.
http://localhost:3282/ISS/Training/SearchTrainerData.aspx
Now I want to split the above string with "/" as well. I want to store the value SearchTrainerData.aspx in a variable.
In my case
Dim str as String
str = "SearchTrainerData.aspx"
What would be the code which will split the above string and further store it in a variable?
Try to use the function String.Split.
Your "string" is obviously a URL which means you should use the System.Uri class.
Dim url As Uri = New Uri("http://localhost:3282/ISS/Training/SearchTrainerData.aspx")
Dim segments As String() = url.Segments
Dim str As String = segments(segments.Length - 1)
This will also allow you to get all kinds of other interesting information about your "string" without resorting to manual (and error-prone) parsing.
The Split function takes characters, which VB.NET represents by appending a 'c' to the end of a one-character string:
Dim sentence = "http://localhost:3282/ISS/Training/SearchTrainerData.aspx"
Dim words = sentence.Split("/"c)
Dim lastWord = words(words.length - 1)
I guess what you are actually looking for is the System.Uri Class. Which makes all string splitting that you are looking for obsolete.
MSDN Documentation for System.Uri
Uri url = new Uri ("http://...");
String[] parts = url.Segments;
Use split(). You call it on the string instance, passing in a char array of the delimiters, and it returns to you an array of strings. Grab the last element to get your "SearchTrainerData.aspx."