In VB.NET, I would like to increment a number in a string and have it zeroed filled.
Here is the sample string with the 5 digit number:
R00099
What I would like returned after incrementing it by one:
R00100
No need for PadLeft:
Dim result = String.Format("R{0:D5}", number)
The D5 part in the formatter will format the number as a decimal number, using a fixed number of five digits, and filling the redundant digits with zeros.
More information can be found on the MSDN article about the decimal format specifier.
If the strings have been validated and are in the form specified then this should work
Private Function add1ToStringNoChecking(theString As String) As String
'assumes many things about the input instring
Return String.Format("{0}{1:d5}", _
"R", _
CInt(theString.Substring(theString.Length - 5, 5)) + 1)
End Function
Private Sub Button1_Click(sender As System.Object, _
e As System.EventArgs) Handles Button1.Click
Dim testS As String = "R00009"
Debug.WriteLine(add1ToStringNoChecking(testS))
End Sub
Assuming (with the regex tag) that you want to strip the number out first, and the input will always be in the form of letters followed by numeric then:
Function Increment(ByVal prefixedNumber As String) As String
Dim result As String = String.Empty
Dim numericRegex As New Text.RegularExpressions.Regex("^(\D*)(\d*)")
Dim numericMatch As Text.RegularExpressions.Match = numericRegex.Match(prefixedNumber)
If numericMatch.Success Then
Dim number As Integer
If Integer.TryParse(numericMatch.Groups(2).Value, number) Then
result = String.Format("{0}{1:D5}", numericMatch.Groups(1).Value, number + 1)
Else
' throw a non parse exception.
End If
Else
' throw a non match exception.
End If
Return result
End Function
Have a look at the Regex and Integer.TryParse documentation
Here is a handy function to accomplish the OP requirement:
Public Function Counter(ByVal StartingNumber As Int32, ByVal IncrementValue As Int32, ByVal TotalNumberLength As Int32, ByVal Prefix As String) As String
Dim Temp As Int32 = StartingNumber + IncrementValue
Dim Temp2 As String = CStr(Temp)
Line50:
If Temp2.Length < TotalNumberLength Then
Temp2 = "0" & Temp2
GoTo Line50
ElseIf Temp2.Length = TotalNumberLength Then
'do nothing
Else
'means error
Throw New System.Exception()
End If
Return Prefix & Temp2
End Function
Example of using the function:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'now test the function
MessageBox.Show(Counter(99, 1, 5, "R"))
'it will show R00100
End Sub
NOTE: This solution has been tested OK with Visual Studio 2010.
Related
At the push of a button, I need a way to remove everything but numbers from a textbox and group numbers 6 in a row with a comma , to separate them.
Example:
Text in textbox:
416782 167490ai|189037jkn
expected result:
416782,167490,189037
Any help is much appreciated :)
I've got as far as this, and it gives me the right output and removes everything but numbers. I just need to add a comma every 6 characters.
Private Sub copypdmbutton_Click(sender As Object, e As EventArgs) _
Handles copypdmbutton.Click
Dim input As String = textbox.Text
Dim output As String = textbox2.Text
output = (System.Text.RegularExpressions.Regex.Replace(input, "[^\d]", ""))
'removes everything except numbers
textbox2.Text = output
End Sub
Fortunately for us, a String is a sequential collection of Char. We can use this fact by looping through each Char in the String from the text box. We only add the numbers (digits) to the new string.
I then build the result string using Substring(start Index, length) followed by a comma.
This code only works if the length of numberString is evenly divisible by 6. The Mod operator tells us this. I will leave it to you to decide how to handle the edge case. Hint: The last sb.Append can use Substring(start Index) The missing second parameters means that the substring will continue to the end of the original string.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "416782 167490ai|189037jkn"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim s = TextBox1.Text
Dim sb As New StringBuilder
For Each c As Char In s
If Char.IsDigit(c) Then
sb.Append(c)
End If
Next
Dim numberString = sb.ToString
sb.Clear()
If numberString.Length Mod 6 = 0 Then
For i = 0 To numberString.Length - 6 Step 6
sb.Append(numberString.Substring(i, 6) & ",")
Next
End If
TextBox1.Text = sb.ToString.Trim(","c)
End Sub
If you NuGet "System.Interactive" to get the Buffer operator, then this code does the job:
Dim input As String = "3434jh34kjh3b4k34m34kj43434"
Dim output As String =
String.Join(
",", _
input _
.Where(Function (x) Char.IsDigit(x)) _
.Buffer(6) _
.Select(Function (xs) String.Concat(xs)))
Console.WriteLine(output)
For my sample input I get this out:
343434,343434,43434
I have as input the string in the below format
"[1_5,3,7,1],[1_2,4,1,9],[],[1_1,,4,,,9,2]"
What I need to obtain is the same string but with the number after the _ sorted:
"[1_1,3,5,7],[1_1,2,4,9],[],[1_1,2,4,9,,,]"
Dim tmprequestedArea_selectionAreaIn As String = "[1_5,3,7,1],[1_2,4,1,9],[],[1_1,,4,,,9,2]"
tmprequestedArea_selectionAreaIn = Regex.Replace(requestedArea_selectionAreaIn,"\],\[","#")
tmprequestedArea_selectionAreaIn = Regex.Replace(tmprequestedArea_selectionAreaIn,"\[|\]","")
bracList.AddRange(tmprequestedArea_selectionAreaIn.Split(New Char() {"#"c}, StringSplitOptions.None ))
If sortNumber Then
'Split braclist by _ and puts the value in strList
'If after _ is only one number put only that number, else split it by char "," and put in strList the join of the split by , array
'Sort the array
'in previous example strList will contain a,b,c in position 0 and _d_f (instead of f,d) in position 1
For i As Integer = 0 To bracList.Count -1
Dim tmp As String()
Dim tmpInt As New System.Collections.Generic.List(Of Integer)
If Not(String.IsNullOrEmpty(bracList(i))) Then
Dim tmpRequested As String = bracList(i).Split(New Char() {"_"c})(0)
Dim tmpSelection As String = bracList(i).Split(New Char() {"_"c})(1)
If tmpSelection.Contains(",") Then
tmp = tmpSelection.Split(New Char() {","c})
For j As Integer = 0 To tmp.Length -1
tmpInt.Add(Convert.toInt32(tmp(j)))
Next
tmpInt.Sort
strList.Add("[" + tmpRequested + "_" + String.Join(",",tmpInt ) + "]")
Else
strList.Add("[" + tmpRequested + "_" + tmpSelection + "]" )
End If
Else
strList.Add("[]")
End If
Next i
I'm looking for a better way to manage it.
Try this, as a possible substitute for what you're doing now.
Given this input string:
Dim input As String = "[1_5,3,7,1],[1_2,4,1,9],[],[1_1,,4,,,9,2]"
Note: this will also deal with decimal values without changes. E.g.,
"[1_5.5,3.5,7,1],[1_2.564,4,2.563,9],[],[1_1,,4.23,,,9.0,2.45]"
You can extract the content of the brackets with this pattern: \[(.*?)\] and use Regex.Matches to return a MatchCollection of all the substrings that match the pattern.
Then use a StringBuilder as a container to rebuild the string while the parts are being treated.
Imports System.Linq
Imports System.Text.RegularExpressions
Dim pattern As String = "\[(.*?)\]"
Dim matches = Regex.Matches(input, pattern, RegexOptions.Singleline)
Dim sb As New StringBuilder()
For Each match As Match In matches
Dim value As String = match.Groups(1).Value
If String.IsNullOrEmpty(value) Then
sb.Append("[],")
Continue For
End If
Dim sepPosition As Integer = value.IndexOf("_"c) + 1
sb.Append("[" & value.Substring(0, sepPosition))
Dim values = value.Substring(sepPosition).Split(","c)
sb.Append(String.Join(",", values.Where(Function(n) n.Length > 0).OrderBy(Function(n) CDec(n))))
sb.Append(","c, values.Count(Function(n) n.Length = 0))
sb.Append("],")
Next
Dim result As String = sb.ToString().TrimEnd(","c)
If you don't know about LINQ, this is what it's doing:
String.Join(",", values.Where(Function(n) n.Length > 0).OrderBy(Function(n) CDec(n)))
values is an array of strings, generated by String.Split().
values.Where(Function(n) n.Length > 0): creates an Enumerable(Of String) from values Where the content, n, is a string of length > 0.
I could have written values.Where(Function(n) Not String.IsNUllOrEmpty(n)).
.OrderBy(Function(n) CDec(n))): Orders the resulting Enumerable(Of String) using the string value converted to Decimal and generates an Enumerable(Of String), which is passed back to String.Join(), to rebuild the string, adding a char (","c) between the parts.
values.Count(Function(n) n.Length = 0): Counts the elements of values that have Length = 0 (empty strings). This is the number of empty elements that are represented by a comma, appended at the end of the partial string.
If you are looking for a "way"
I think it is easier to fetch each char of the string and if it is a number you put it in array (and when the char is ']' you start new array) the sort the arrays and replace each number from the string with it's sorted number (so you will just do allocation without the need to reconstruct with regular expression
I wish that I had Visual Studio to provide you the code (it is joyful to code a riddle) ^_^
ps:for the commas you can use a counter for each blank commas an the put it in the end
I'm trying to make a vb function that takes as input a String and returns, if exist, the string made of numeric digits from the beginning until the first non numerical char, so:
123 -> 123
12f -> 12
12g34 -> 12
f12 -> ""
"" -> ""
I wrote a function that incrementally compares the result matching the regex, but it goes on even on non numeric characters...
This is the function:
Public Function ParseValoreVelocita(ByVal valoreRaw As String) As String
Dim result As New StringBuilder
Dim regexp As New Regex("^[0-9]+")
Dim tmp As New StringBuilder
Dim stringIndex As Integer = 0
Dim out As Boolean = False
While stringIndex < valoreRaw.Length AndAlso Not out
tmp.Append(valoreRaw.ElementAt(stringIndex))
If regexp.Match(tmp.ToString).Success Then
result.Append(valoreRaw.ElementAt(stringIndex))
stringIndex = stringIndex + 1
Else
out = True
End If
End While
Return result.ToString
End Function
The output always equals the input string, so there's something wrong and I can't get out of it...
Here's a LINQ solution that doesn't need regex and increases readability:
Dim startDigits = valoreRaw.TakeWhile(AddressOf Char.IsDigit)
Dim result As String = String.Concat(startDigits)
Try this instead. You need to use a capture group:
Public Function ParseValoreVelocita(ByVal valoreRaw As String) As String
Dim result As New StringBuilder
Dim regexp As New Regex("^([0-9]+)")
Dim tmp As New StringBuilder
Dim stringIndex As Integer = 0
Dim out As Boolean = False
While stringIndex < valoreRaw.Length AndAlso Not out
tmp.Append(valoreRaw.ElementAt(stringIndex))
If regexp.Match(tmp.ToString).Success Then
result.Append(regexp.Match(tmp.ToString).Groups(1).Value)
stringIndex = stringIndex + 1
Else
out = True
End If
End While
Return result.ToString
End Function
The expression:
Dim regexp As New Regex("^([0-9]+)")
and the result appending lines have been updated:
result.Append(regexp.Match(tmp.ToString).Groups(1).Value)
You have made your code very complex for a simple task.
Your loop keeps trying to build a longer string and it keeps checking if it is still working with digits, and if so keep appending results.
So and input string of "123x" would, if your code worked, produce a string of "112123" as output. In other words it matches the "1", then "12", then "123"and concatenates each before exiting after it finds the "x".
Here's what you should be doing:
Public Function ParseValoreVelocita(valoreRaw As String) As String
Dim regexp As New Regex("^([0-9]+)")
Dim match = regexp.Match(valoreRaw)
If match.Success Then
Return match.Groups(1).Captures(0).Value
Else
Return ""
End If
End Function
No loop and you let the regex do the work.
I'm trying to change or update the code from detecting commas for pages,
The code below shows how to input pages with commas,
sample: 1,2,5,3,8 and it would not accept 0, or greater than the maximum page
What I'm asking is to add the code that would accept
like this:
2-5,8,9
or
8,9,2-5
or
2-5,8-10
so it means, pages to print are 2,3,4,5,8,9,10
BUT it will NOT accept input like 2-5,4,8,9 because 4 was already used in 2-5
if that could be hard then its ok to input a simple range like:
2-5 and no commas, so user could not input commas, if they want to input
comma then the -sign could not be input also.
''CODED By: Chris, Combined to MackieChan solution
Public Function isCELLPageNumb(ByRef valyo As String, ByVal origMaxPage As Integer) As Boolean
Dim rgxNumberWithComma As New System.Text.RegularExpressions.Regex("^([0-9]+,?)+$")
Dim match = rgxNumberWithComma.Match(valyo)
If Not match.Success Then
Return False
Else
Dim numbers As New List(Of Integer) 'will store added numbers
For Each Item In valyo.Split(","c)
Dim intValue As Integer
'Check if number is a valid integer
'Check if number is 0
'Check if number has already added the number list
'Check if number is greater that MaxPage
If Not Integer.TryParse(Item, intValue) _
OrElse intValue > origMaxPage _
OrElse intValue = 0 _
OrElse numbers.Contains(intValue) Then
Return False
Else
'Item is valid, continue
numbers.Add(intValue)
End If
Next
End If
Return True
End Function
Private Sub DbGridPapers_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DbGridPapers.CellEndEdit
Dim pagestoprint As String = Nothing
Try
pagestoprint = DbGridPapers.Rows(e.RowIndex).Cells(1).Value.ToString
Catch ex As Exception
End Try
If (e.ColumnIndex = 1) And (pagestoprint IsNot Nothing) Then
If Not isCELLPageNumb(DbGridPapers.Rows(e.RowIndex).Cells(1).Value, OrigPage(e.RowIndex)) Then
MyThreadedControl(lbltest, "Text", "INVALID INPUT FOR [PAGES] AT ROW " & (e.RowIndex).ToString)
DbGridPapers.Rows(e.RowIndex).Cells(1).Value = OrigPage(e.RowIndex)
Return
Else
MyThreadedControl(lbltest, "Text", "The Maximum Page is:" & OrigPage(e.RowIndex).ToString)
End If
Dim pageDest As String = Nothing
If Me.btnpaperpay.Enabled Then
pageDest = DbGridPapers.Rows(e.RowIndex).Tag & "\"
Else
pageDest = docPrintnationPath & "\"
End If
Dim filename As String = pageDest & DbGridPapers.Rows(e.RowIndex).HeaderCell.Value.ToString
Dim OldRegularPrice As Decimal = DbGridPapers.Rows(e.RowIndex).Cells(3).Value
Dim FILEpages As New List(Of Integer)
''IF , AND - CAN BE MIX TO GET THE PAGE THEN ITS BETTER, AND I HAVE TO UPDATE THE CODE HERE ALSO.
If pagestoprint.Split(",").Length > 1 Then 'Split Length is +1 based
Dim pageFILES() As String = pagestoprint.Split(",")
For Each filePids As Integer In pageFILES
FILEpages.Add(filePids) ''GET range in comma sample page1,page3,page8,page2
Next
ElseIf pagestoprint.Split("-").Length > 1 Then 'Split Length is +1 based
Dim pageFILES() As String = pagestoprint.Split("-")
For page As Integer = pageFILES(0) To pageFILES(1)
FILEpages.Add(page) ''GET range sample pages2 to page5
Next
Else
Dim pages As Integer
If (Integer.TryParse(pagestoprint, pages)) Then
If pages = OrigPage(e.RowIndex) Then
DbGridPapers.Rows(e.RowIndex).Cells(2).Value = OrigImage(e.RowIndex)
DbGridPapers.Rows(e.RowIndex).Cells(3).Value = OrigPay(e.RowIndex)
DbGridPapers.Rows(e.RowIndex).Cells(4).Value = OrigCountedImage(e.RowIndex)
GoTo pCounter ''Return Original Cells Value
Else
FILEpages.Add(pages)''GET single page only
End If
End If
End If
pCounter:
Dim paperToTpay As Decimal = txtpapertotpay.Text.Substring(0, txtpapertotpay.Text.LastIndexOf(" "))
paperToTpay -= OldRegularPrice
paperToTpay += DbGridPapers.Rows(e.RowIndex).Cells(3).Value
MyThreadedControl(txtpapertotpay, "Text", paperToTpay.ToString & " dollar(s)")
End If
End Sub
I think its very hard.
I understand that a custom algorithm is acceptable. Here you have an approach accounting for all the described conditions:
Private Function extractPages(ByVal inputString As String) As List(Of Integer)
Dim outList As List(Of Integer) = New List(Of Integer)
If (inputString.Contains(",")) Then
outList = extractCommas(inputString, outList)
ElseIf (inputString.Contains("-")) Then
outList = extractDashes(inputString, outList)
End If
If (outList.Count > 0) Then
For i As Integer = outList.Count - 1 To 0 Step -1
If (outList.IndexOf(outList(i)) <> outList.LastIndexOf(outList(i))) Then
'Repeated item
'It can be just deleted or shall the function return an error?
outList.RemoveAt(i)
End If
Next
End If
Return outList
End Function
Private Function extractCommas(ByVal inputString As String, curList As List(Of Integer)) As List(Of Integer)
If (inputString.Contains(",")) Then
Dim temp() As String = inputString.Split(",")
For Each item In temp
If (Not item.Contains("-") And IsNumeric(item)) Then
If (Convert.ToInt32(item.Trim()) > 0) Then
curList.Add(Convert.ToInt32(item.Trim()))
End If
ElseIf (item.Contains("-")) Then
curList = extractDashes(item.Trim(), curList)
End If
Next
End If
Return curList
End Function
Private Function extractDashes(ByVal inputString As String, curList As List(Of Integer)) As List(Of Integer)
If (inputString.Contains("-")) Then
Dim temp() = inputString.Split("-")
If (temp.Length = 2) Then
If (Convert.ToInt32(temp(0)) <= Convert.ToInt32(temp(1))) Then
Dim count As Integer = Convert.ToInt32(temp(0)) - 1
If (count < 0) Then
count = 0
End If
Do
count = count + 1
curList.Add(count)
Loop While (count < Convert.ToInt32(temp(1)))
End If
End If
End If
Return curList
End Function
You can call extractPages and get all the page numbers:
Dim InputString As String = "2-5, 4,8-10"
Dim allPages As List(Of Integer) = extractPages(InputString) 'It returns 2, 3, 4, 5, 8, 9, 10
Description
It's not exactly clear what you're looking for, but this powershell solution shows the logic behind how I would approach the problem to allow a user to input 0,2-5,4,8,9 so that the zero and extra redundant digits are ignored.
Example
$string = "0,2-5,4,8,9"
[hashtable]$hashPages = #{}
foreach ($chunk in $String -split ",") {
# if the string has a dash then process it as a range
if ($chunk -match "(\d+)-(\d+)") {
# itterate through all the pages in the range
foreach ($Page in $Matches[1] .. $Matches[2]) {
# insert this page into a hash, which will keep the numbers unique
$hashPages[[string]$Page] = $true
} # next page
} # end if
# if string is only a number then process it as a single number
if ($chunk -match "(\d+)") {
# insert this page into a hash, which will keep the numbers unique
$hashPages[[string]$Matches[1]] = $true
} # end if
} # next chunk
# remove the undesireable numbers like zero if they were added
$hashPages.Remove("0");
Write-Host "these pages where requested:" $(($hashPages.Keys | sort ) -join ",")
Output
these pages where requested: 2,3,4,5,8,9
I have a string as below, which needs to be split to an array, using VB.NET
10,"Test, t1",10.1,,,"123"
The result array must have 6 rows as below
10
Test, t1
10.1
(empty)
(empty)
123
So:
1. quotes around strings must be removed
2. comma can be inside strings, and will remain there (row 2 in result array)
3. can have empty fields (comma after comma in source string, with nothing in between)
Thanks
Don't use String.Split(): it's slow, and doesn't account for a number of possible edge cases.
Don't use RegEx. RegEx can be shoe-horned to do this accurately, but to correctly account for all the cases the expression tends to be very complicated, hard to maintain, and at this point isn't much faster than the .Split() option.
Do use a dedicated CSV parser. Options include the Microsoft.VisualBasic.TextFieldParser type, FastCSV, linq-to-csv, and a parser I wrote for another answer.
You can write a function yourself. This should do the trick:
Dim values as New List(Of String)
Dim currentValueIsString as Boolean
Dim valueSeparator as Char = ","c
Dim currentValue as String = String.Empty
For Each c as Char in inputString
If c = """"c Then
If currentValueIsString Then
currentValueIsString = False
Else
currentValueIsString = True
End If
End If
If c = valueSeparator Andalso not currentValueIsString Then
If String.IsNullOrEmpty(currentValue) Then currentValue = "(empty)"
values.Add(currentValue)
currentValue = String.Empty
End If
currentValue += c
Next
Here's another simple way that loops by the delimiter instead of by character:
Public Function Parser(ByVal ParseString As String) As List(Of String)
Dim Trimmer() As Char = {Chr(34), Chr(44)}
Parser = New List(Of String)
While ParseString.Length > 1
Dim TempString As String = ""
If ParseString.StartsWith(Trimmer(0)) Then
ParseString = ParseString.TrimStart(Trimmer)
Parser.Add(ParseString.Substring(0, ParseString.IndexOf(Trimmer(0))))
ParseString = ParseString.Substring(Parser.Last.Length)
ParseString = ParseString.TrimStart(Trimmer)
ElseIf ParseString.StartsWith(Trimmer(1)) Then
Parser.Add("")
ParseString = ParseString.Substring(1)
Else
Parser.Add(ParseString.Substring(0, ParseString.IndexOf(Trimmer(1))))
ParseString = ParseString.Substring(ParseString.IndexOf(Trimmer(1)) + 1)
End If
End While
End Function
This returns a list. If you must have an array just use the ToArray method when you call the function
Why not just use the split method?
Dim s as String = "10,\"Test, t1\",10.1,,,\"123\""
s = s.Replace("\"","")
Dim arr as String[] = s.Split(',')
My VB is rusty so consider this pseudo-code