VB.Net String Replace Separated by Comma - vb.net

I have a string that contains numbers
label1.text = 2,8,11,9,1,12
Now when I replace 1 it replaces 11 too and I don't want that to happen
I would like when I remove something it just remove the specific thing
but 1 will also remove 11
I'm using this for a branchaccess for multiple branch the numbers are the ids of the branch
label1.text = 2,8,11,9,1,12
Dim newstring As String
newstring = Label1.Text.Replace("," & "1", "")
Label1.Text = newstring
Can anyone please help me? Thank you

Provided that there are never spaces between your numbers, you could do something like:
label1.text = "2,8,11,9,1,12"
Dim newstring As String
newstring = label1.Text.Replace(",1,", ",")
If newstring.StartsWith("1,") Then
newstring = newstring.Substring(2)
End If
If newstring.EndsWith(",1") Then
newstring = newstring.Substring(0, newstring.Length - 2)
End If
label1.Text = newstring
This does three things:
Replace instances of ,1, with , (removing 1 and one comma from the middle of the text)
Remove 1, from the start of the string (only if it is there)
Remove ,1 from the end of the string (only if it is there)
You could also use a Regular Expression to achieve the same result, which may be cleaner or more efficient.

Perhaps a better way would be store the branch IDs separately and join them together only when you want to show them in the label.
For example,
Dim branchIds As New List(Of Integer) From {2, 8, 11, 9, 1, 12}
branchIds.Remove(1)
Label1.Text = String.Join(",", branchIds)
results in Label1 showing 2,8,11,9,12.
That way, you could change the "," to, say, ", " to improve readabilty, and it would make no difference to how the branch IDs are processed. It is a good idea to separate the data from the display of the data.

Related

String.join starting and ending index

Can I specify the starting and Ending Index in String.Join ?
Just to clarify, say I have array A (dimensioned 0 to 20) and I would like to join elements 4 to 10, can this be done with String.Join?
The way I currently do it is
Dim Str as String
Str = ""
For I = 4 to 10
Str = Str & A(I)
Next
Is there an alternative to this?
Thanks in advance
You can use Linq to extract only the array subset you want and pass it to String.Join in a single line of code.
Dim Str = String.Join("", a.Skip(3).Take(7))
(By the way, this has also the added benefit that if your array doesn't contain 10 elements you don't get an IndexOutOfRange exception)

Delete last word between two characters VB.Net

Is there any way that I can delete the last "word" in a textbox? Each word or string would be separated by a comma (",")
This is what I have at the minute, but I am open to new suggestions
Dim deleteItem() As String = Split(TextBox2.Text, ", ")
Array.Resize(deleteItem, deleteItem.Length - 1)
For i = 0 To UBound(deleteItem)
TextBox2.Text = deleteItem(i)
Next
Any help would be great thanks :)
You can use the LastIndexOf Function
TextBox2.Text = TextBox2.Text.Substring(0,TextBox2.Text.LastIndexOf(",")).Trim()
Why dont you just get the substring till the last ,
TextBox2.Text = TextBox2.Text.Substring(0,TextBox2.Text.LastIndexOf(","))

VB.net removing and keeping words in a textbox

[{"name":"chrisjj","uuid":"d086112c-6e25-31a0-acf0-f95c3ca98784","expiresOn":"2016-02-22 23:04:35 +0000"}]
[{"name":"ben","uuid":"d086112c-7a26-33b5-ucf3-j96c1ca26854","expiresOn":"2015-011-12 22:04:35 +0000"}]
Basically im working on a project for a while now and I am trying to keep the names chrisjj and ben and removing the rest of the text from textbox in visual basic 2012 if you have any idea that would be great help
You may use regex to achieve what you want.
Dim Input As String = RichTextBox1.Text
Dim MC As MatchCollection = Regex.Matches(Input, Regex.Escape("[{""name"":""") & "[chrisjj|ben].*?" & Regex.Escape("]"), RegexOptions.IgnoreCase)
Dim Output As New List(Of String)
For i = 0 To MC.Count - 1
Output.Add(MC(i).Value)
Next
MsgBox(String.Join(vbNewLine, Output.ToArray()))
I think this is what you want. this regex matches [{"name":" then chrisjj or ben and goes on until ] is found.
You can do this:
If InStr(Textbox1.Text, "chrisjj") Then
Textbox1.text = "chrisjj"
else if InStr(Textbox1.Text, "ben") Then
Textbox1.text = "ben"
end if
The InStr Function returns the position of the first occurrence of one string within another.
Also
if TextBox1.Text.Contains("chrisjj") Then
TextBox1.Text = TextBox1.Text = "chrisjj"
ElseIf TextBox2.Text.Contains("ben") Then
TextBox1.Text = TextBox1.Text = ben
end if
The String.Contains Method (String) returns a value indicating whether a specified substring occurs within this string.

How does the Val() function in VB.NET work?

I have an assignment in VB.NET that I'm stuck with at the moment. Would love some help.
The question is this: You enter random characters into a textbox, for example 12ab3c4d5efgh, and at the click of a button, it must sort the characters in the textbox into 2 separate Labels, depending on whether or not the 'character' is a number or letter. So, continuing the example, Label1 must show '12345' and Label 2 must show 'abcdefgh'. I hope I made myself clear enough.
I was asked to use the Val() function but I really have no clue. Could someone please help? :D
This creates one string with the digits and one with the letters. Characters that are not digits or letters are ignored.
Dim chars As String = "12ab3c4d5efgh"
Dim nums As String = chars.Where(Function(c) Char.IsDigit(c)).ToArray
Dim lets As String = chars.Where(Function(c) Char.IsLetter(c)).ToArray
If you have to use Val() something like this would do. But be careful: Val("0") also returns 0.
Dim numbers As String = String.Empty
Dim letters As String = String.Empty
Dim sourceString As String = "12ab3c4d50efgh"
For Each c As Char In sourceString
If Val(c) = 0 And c <> "0" Then letters &= c Else numbers &= c
Next
Console.WriteLine("Numbers: " & numbers)
Console.WriteLine("Letters: " & letters)
Console.ReadKey()

How to find which delimiter was used during string split (VB.NET)

lets say I have a string that I want to split based on several characters, like ".", "!", and "?". How do I figure out which one of those characters split my string so I can add that same character back on to the end of the split segments in question?
Dim linePunctuation as Integer = 0
Dim myString As String = "some text. with punctuation! in it?"
For i = 1 To Len(myString)
If Mid$(entireFile, i, 1) = "." Then linePunctuation += 1
Next
For i = 1 To Len(myString)
If Mid$(entireFile, i, 1) = "!" Then linePunctuation += 1
Next
For i = 1 To Len(myString)
If Mid$(entireFile, i, 1) = "?" Then linePunctuation += 1
Next
Dim delimiters(3) As Char
delimiters(0) = "."
delimiters(1) = "!"
delimiters(2) = "?"
currentLineSplit = myString.Split(delimiters)
Dim sentenceArray(linePunctuation) As String
Dim count As Integer = 0
While linePunctuation > 0
sentenceArray(count) = currentLineSplit(count)'Here I want to add what ever delimiter was used to make the split back onto the string before it is stored in the array.'
count += 1
linePunctuation -= 1
End While
If you add a capturing group to your regex like this:
SplitArray = Regex.Split(myString, "([.?!])")
Then the returned array contains both the text between the punctuation, and separate elements for each punctuation character. The Split() function in .NET includes text matched by capturing groups in the returned array. If your regex has several capturing groups, all their matches are included in the array.
This splits your sample into:
some text
.
with punctuation
!
in it
?
You can then iterate over the array to get your "sentences" and your punctuation.
.Split() does not provide this information.
You will need to use a regular expression to accomplish what you are after, which I infer as the desire to split an English-ish paragraph into sentences by splitting on punctuation.
The simplest implementation would look like this.
var input = "some text. with punctuation! in it?";
string[] sentences = Regex.Split(input, #"\b(?<sentence>.*?[\.!?](?:\s|$))");
foreach (string sentence in sentences)
{
Console.WriteLine(sentence);
}
Results
some text.
with punctuation!
in it?
But you are going to find very quickly that language, as spoken/written by humans, does not follow simple rules most of the time.
Here it is in VB.NET for you:
Dim sentences As String() = Regex.Split(line, "\b(?<sentence>.*?[\.!?](?:\s|$))")
Once you've called Split with all 3 characters, you've tossed that information away. You could do what you're trying to do by splitting yourself or by splitting on one punctuation mark at a time.