Insert separate each digit by delimiter a comma - vb.net

I have in Textbox1.Text these lines:
27
408
73
49
80
71
70
I want to put a comma between each number separately. I want to do this automatically, put a comma between characters.
like: note: where there are 3 characters, like 408, it will be 40,8 when it is 70, it will be 7,0. this I think I can do, if I have an example code that separates my characters with a comma.
2,7
40,8
7,3
4,9
8,0
7,1
7,0
Code: this code does not work properly. displays many values and incorrectly type 3.3,, 4,5,6,7,78, etc, and many lines. what he shouldn't do.
Dim XStrsLength = TextboxIndex1.Text.Length
Dim XStrs As List(Of String) = New List(Of String)
Dim str As String = TextboxIndex1.Text
Dim last As Integer
For interval As Integer = 1 To XStrsLength
Dim xstr As String = ""
For I As Integer = 0 To str.Length - interval - 1 Step interval
xstr &= str.Substring(I, interval) & ","
last = I
Next
xstr &= str.Substring(last + interval)
XStrs.Add(xstr)
Next interval
TextBox1.Text = String.Join("", XStrs)

Try this code. It parses whatever is in TextBox1 and places the result into TextBox2:
Private Sub Test()
Dim pieces() As String = TextBox1.Text.Split(ControlChars.CrLf.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
Dim str As String = ""
For Each piece As String In pieces
str &= piece.Insert(piece.Length - 1, ",") & ControlChars.CrLf
Next
TextBox2.Text = str.Substring(0, str.Length - 2)
End Sub

you can use String.Insert(Integer, String) to insert a comma:
if the line has 2 chars: yourline.Insert(1, ",")
else if it has 3 chars: yourline.Insert(2, ",")

Related

Combining two string and insert element VB.Net

I tried to build a combination algorithm between 2 strings, unfortunately it has some errors.
Dim strWordsA() As String = TextBox1.Text.Split(",")
Dim strWordsB() As String = TextBox2.Text.Split(",")
Dim str As String = TextBox1.Text
Dim arr As String() = TextBox1.Text.Split(","c)
For i As Integer = 0 To TextBox1.Text.Split(",").Length - 1
Dim index As Integer = str.IndexOf(strWordsA(i))
TextBox1.Text = str.Insert(index + 2, "," & strWordsB(i))
str = TextBox1.Text
Next
so if we have Textbox1.Text = 1,2,3,4,5,6,7,8,9 and Textbox2.Text = a,b,c,f,d,b,i,h, and so on... I need to display this in a 3rd textbox
Textbox3.Text = 1,a,2,b,3,c,4,f and so on
so do I combine these 2 strings?
the first element in the index displays it incorrectly, otherwise it seems to work ok.
Try this:
Private Function MergeStrings(s1 As String, s2 As String) As String
Dim strWordsA() As String = s1.Split(","c)
Dim strWordsB() As String = s2.Split(","c)
Dim i As Integer = 0
Dim OutputString As String = String.Empty
While i < strWordsA.Length OrElse i < strWordsB.Length
If i < strWordsA.Length Then OutputString &= "," & strWordsA(i)
If i < strWordsB.Length Then OutputString &= "," & strWordsB(i)
i += 1
End While
If Not OutputString = String.Empty Then Return OutputString.Substring(1)
Return OutputString
End Function
Usage:
Dim s As String = MergeStrings("1,2,3,4,5,6,7,8,9", "a,b,c,f,d,b,i,h")
You will need to add your own validation to allow for trailing commas or no commas etc but it should work with different length input strings
EDIT: amended as per Mary's comment

Compare two strings and find where letter positions match

I want to do a bitwise and on two strings so that:
Given:
Dim word As String = "abcd"
Dim temp As String = "a-d-"
I want to return only the 'a'
Given:
Dim word As String = "abcd"
Dim temp As String = "a--d"
I want to return only the 'a--d'
I have tried intersect, but it only finds characters in one string that match the characters in the other regardless of position.
I've used the '-' to represent spaces here.
Any suggestions would be appreciated.
This will handle strings with mis-matched lengths:
Public Function CheckMask(ByVal word As String, ByVal mask As String) As String
Dim wordChars() As Char = word.ToCharArray()
Dim maskChars() As Char = mask.ToCharArray()
Dim i As Integer = 0
While i < wordChars.Length AndAlso i < maskChars.Length
If wordChars(i) <> maskChars(i) Then wordChars(i) = " "c
i = i + 1
End While
'If string lengths are equal or the mask is longer, we're done
'If the word is longer, need to set remaining characters to " "
While i < wordChars.Length
wordChars(i) = " "c
End While
Return New String(wordChars)
End Function
Dim Res As String = ""
For i = 0 To Math.Min(StrA.Length, StrB.Length) - 1
If StrA(i) = StrB(i) Then Res &= StrA(i) Else Res &= " "
Next
Return Res
This basically loops to the end of the shorter one of the two strings. If the letters at a given position match the letter is added to the result, else a space is added.
Dim sFirstWord As String = "qwerty"
Dim sSecndWord As String = "qseftg"
Dim sResult As String = ""
For i As Integer = 0 To Math.Min(sFirstWord.Length, sSecndWord.Length) - 1
If sFirstWord(i) = sSecndWord(i) Then
sResult &= sFirstWord(i)
Else
sResult &= " "
End If
Next
sResult will hold: "q e t "

How can I get String values rather than integer

How To get StartString And EndString
Dim startNumber As Integer
Dim endNumber As Integer
Dim i As Integer
startNumber = 1
endNumber = 4
For i = startNumber To endNumber
MsgBox(i)
Next i
Output: 1,2,3,4
I want mo make this like sample: startString AAA endString AAD
and the output is AAA, AAB, AAC, AAD
This is a simple function that should be easy to understand and use. Every time you call it, it just increments the string by one value. Just be careful to check the values in the text boxes or you can have an endless loop on your hands.
Function AddOneChar(Str As String) As String
AddOneChar = ""
Str = StrReverse(Str)
Dim CharSet As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim Done As Boolean = False
For Each Ltr In Str
If Not Done Then
If InStr(CharSet, Ltr) = CharSet.Length Then
Ltr = CharSet(0)
Else
Ltr = CharSet(InStr(CharSet, Ltr))
Done = True
End If
End If
AddOneChar = Ltr & AddOneChar
Next
If Not Done Then
AddOneChar = CharSet(0) & AddOneChar
End If
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim S = TextBox1.Text
Do Until S = TextBox2.Text
S = AddOneChar(S)
MsgBox(S)
Loop
End Sub
This works as a way to all the codes given an arbitrary alphabet:
Public Function Generate(starting As String, ending As String, alphabet As String) As IEnumerable(Of String)
Dim increment As Func(Of String, String) = _
Function(x)
Dim f As Func(Of IEnumerable(Of Char), IEnumerable(Of Char)) = Nothing
f = _
Function(cs)
If cs.Any() Then
Dim first = cs.First()
Dim rest = cs.Skip(1)
If first = alphabet.Last() Then
rest = f(rest)
first = alphabet(0)
Else
first = alphabet(alphabet.IndexOf(first) + 1)
End If
Return Enumerable.Repeat(first, 1).Concat(rest)
Else
Return Enumerable.Empty(Of Char)()
End If
End Function
Return New String(f(x.ToCharArray().Reverse()).Reverse().ToArray())
End Function
Dim results = New List(Of String)
Dim text = starting
While True
results.Add(text)
If text = ending Then
Exit While
End If
text = increment(text)
End While
Return results
End Function
I used it like this to produce the required result:
Dim alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim results = Generate("S30AB", "S30B1", alphabet)
This gave me 63 values:
S30AB
S30AC
...
S30BY
S30BZ
S30B0
S30B1
It should now be very easy to modify the alphabet as needed and to use the results.
One option would be to put those String values into an array and then use i as an index into that array to get one element each iteration. If you do that though, keep in mind that array indexes start at 0.
You can also use a For Each loop to access each element of the array without the need for an index.
if the default first two string value of your output is AA.
You can have a case or if-else conditioning statement :
and then set 1 == A 2 == B...
the just add or concatenate your default two string and result string of your case.
I have tried to understand that you are looking for a series using range between 2 textboxes. Here is the code which will take the series and will give the output as required.
Dim startingStr As String = Mid(TextBox1.Text, TextBox1.Text.Length, 1)
Dim endStr As String = Mid(TextBox2.Text, TextBox2.Text.Length, 1)
Dim outputstr As String = String.Empty
Dim startNumber As Integer
Dim endNumber As Integer
startNumber = Asc(startingStr)
endNumber = Asc(endStr)
Dim TempStr As String = Mid(TextBox1.Text, 1, TextBox1.Text.Length - 1)
Dim i As Integer
For i = startNumber To endNumber
outputstr = outputstr + ", " + TempStr + Chr(i)
Next i
MsgBox(outputstr)
The First two lines will take out the Last Character of the String in the text box.
So in your case it will get A and D respectively
Then outputstr to create the series which we will use in the loop
StartNumber and EndNumber will be give the Ascii values for the character we fetched.
TempStr to Store the string which is left off of the series string like in our case AAA - AAD Tempstr will have AA
then the simple loop to get all the items fixed and show
in your case to achive goal you may do something like this
Dim S() As String = {"AAA", "AAB", "AAC", "AAD"}
For Each el In S
MsgBox(el.ToString)
Next
FIX FOR PREVIOUS ISSUE
Dim s1 As String = "AAA"
Dim s2 As String = "AAZ"
Dim Last As String = s1.Last
Dim LastS2 As String = s2.Last
Dim StartBase As String = s1.Substring(0, 2)
Dim result As String = String.Empty
For I As Integer = Asc(s1.Last) To Asc(s2.Last)
Dim zz As String = StartBase & Chr(I)
result += zz & vbCrLf
zz = Nothing
MsgBox(result)
Next
**UPDATE CODE VERSION**
Dim BARCODEBASE As String = "SBA0021"
Dim BarCode1 As String = "SBA0021AA1"
Dim BarCode2 As String = "SBA0021CD9"
'return AA1
Dim FirstBarCodeSuffix As String = Replace(BarCode1, BARCODEBASE, "")
'return CD9
Dim SecondBarCodeSuffix As String = Replace(BarCode2, BARCODEBASE, "")
Dim InternalSecondBarCodeSuffix = SecondBarCodeSuffix.Substring(1, 1)
Dim IsTaskCompleted As Boolean = False
For First As Integer = Asc(FirstBarCodeSuffix.First) To Asc(SecondBarCodeSuffix)
If IsTaskCompleted = True Then Exit For
For Second As Integer = Asc(FirstBarCodeSuffix.First) To Asc(InternalSecondBarCodeSuffix)
For Third As Integer = 1 To 9
Dim tmp = Chr(First) & Chr(Second) & Third
Console.WriteLine(BARCODEBASE & tmp)
If tmp = SecondBarCodeSuffix Then
IsTaskCompleted = True
End If
Next
Next
Next
Console.WriteLine("Completed")
Console.Read()
Take a look into this check it and let me know if it can help

loop through 2 strings and combine VB 2010

i have a simple task i think
my code generates 2 strings
string A (separated by line feeds)
1
2
3
4
String B (separated by line feeds)
5
6
7
8
i am trying to figure out how to combine these separate strings into one as if they were two columns next to each other, separated by a comma
the result would be string C
1,5
2,6
3,7
4,8
Thanks!
How about something like?
Dim A As String = "1" & vbCrLf & "2" & vbCrLf & "3" & vbCrLf & "4"
Dim B As String = "5" & vbCrLf & "6" & vbCrLf & "7" & vbCrLf & "8"
Debug.Print(A)
Debug.Print(B)
Dim tmp As New List(Of String)
tmp.AddRange(A.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries))
Dim values() As String = B.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
For i As Integer = 0 To values.Length - 1
If i <= tmp.Count - 1 Then
tmp(i) = tmp(i) & "," & values(i)
Else
tmp.Add("," & values(i))
End If
Next
Dim C As String = String.Join(vbCrLf, tmp.ToArray)
Debug.Print(C)
Convert both strings to char[] (Char() - character array) (string.ToCharArray()).
Iterate thru the arrays.
You will need to check boundary conditions (e.g. what if the strings are different lengths).
'first string
Dim a As String = "abcd"
'second string
Dim b As String = "defghijk"
'Remove Line feeds
Dim tempA As String = a.Replace(vbLf, "")
Dim tempB As String = a.Replace(vbLf, "")
'Get the string with the larger size.
Dim largerSize As Integer = If(tempA.Length > tempB.Length, tempA.Length, tempB.Length)
'We use a string builder to build our new string
Dim sb As New StringBuilder()
'Loop on the larger size and only insert if inside range of string
For i As Integer = 0 To largerSize - 1
If i < tempA.Length Then
sb.Append(a(i))
sb.Append(",")
End If
If i < tempB.Length Then
sb.Append(b(i))
End If
sb.Append(vbLf)
Next
'this is the result
Dim combined As String = sb.ToString()
Edit answer to remove line feeds
Another Edit after string result edited
Another edit to make it VB.NET
I would use the wonderful Linq Zip! (I would first String.Split the 2 strings with the line-feed character) and then
Dim column1() As String = {"1", "2", "3", "4"}
Dim column2() As String = {"5", "6", "7", "8"}
Dim mixed= column1.Zip(column2, Function(first, second) first & "," & second)
edit:
oh, and then, if you want to have it back into 1 string, you can either use String.Join but Linq is fun too! So you can write :
Dim mixed= column1.Zip(column2, Function(first, second) first & "," & second)
.Select(i => i.Boo)
.Aggregate((i, j) => i + vbLf + j)
Did I mention that Linq is fun?

Getting substrings between commas. Why Mid(string,integer,integer) is failing?

I have a textbox (DropDownList1) that contains a string of 46 characters following this format:
(string1,string2,string3)
I want to get string values without the commas, this way:
a=string1
b=string2
c=string3
So I used the below code:
Dim a As String
Dim b As String
Dim c As String
Dim x As Integer = InStr(1, DropDownList1.Text, ",", CompareMethod.Text) + 1
Dim y As Integer = InStr(InStr(1, DropDownList1.Text, ",", CompareMethod.Text) + 1, DropDownList1.Text, ",") - 1
Dim z As Integer = Len(DropDownList1.Text)
a = Mid(DropDownList1.Text, 1, InStr(1, DropDownList1.Text, ",", CompareMethod.Text) - 1)
b = Mid(DropDownList1.Text, x, y) _
'InStr(1, DropDownList1.Text, ",", CompareMethod.Text) + 1, _
'InStr(InStr(1, DropDownList1.Text, ",", CompareMethod.Text) + 1, DropDownList1.Text, ",") - 1)
c = Mid(DropDownList1.Text, _
InStr(InStr(1, DropDownList1.Text, ",", CompareMethod.Text) + 1, DropDownList1.Text, ",") + 1, _
Len(DropDownList1.Text))
However, when I debug it happens:
x=18 (which is correct with the string I was using)
y=42 (correct too)
z=46 (correct)
a=string1 (yes!)
c=string3 (yes again!)
and b=string2,string3 ----->what happened here?
Can you please tell what is wrong with my code? I simply don't get it
Assuming x,y, and z are just for debugging, and it's really a,b, and c that you care about, and that there are no commas or parenthese in the important parts of your string:
Dim values = DropDownList1.Text.Replace("(","").Replace(")","").Split(","c)
Dim a as String = values(0)
Dim b As String = values(1)
Dim c As String = values(2)
Use the Split() function on the string, applying it to some string array variable, and then assigning the values to your variables as needed if you still want to.
If in fact you're using VB.NET you can use the Split function.
Dim text As String = "a,b,c"
Dim parts As String() = text.Split(CChar(","))
Dim a As String = parts(0)
Dim b As String = parts(1)
Dim c As String = parts(2)
Give this a try...
Private Sub ParseMyString()
Dim TargetString() As String = Split("string1,string2,string3", ",")
Dim Count As Integer = 0
Dim Result As String
Const ASC_OFFSET As Integer = 97
Result = ""
Do Until Count > UBound(TargetString)
Result = Result & Chr(ASC_OFFSET + Count) & "=" & TargetString(Count) & vbCrLf
Count = Count + 1
Loop
MsgBox(Result)
End Sub