This question already has answers here:
Split String in VB.net
(2 answers)
Closed 7 years ago.
I have a function in VB.Net which takes two arguments(dosage & no_days).
'dosage' have values of the 1-2-1 format.
I need to get the values as integer from the string: for example, if the string is 1-1-2, I need to get the values as 1,1,2. And sum of these three numbers need to multiply with no_days and return the result.
I need to know how can I split the string using regex "-" or any other logic for doing this.
Public Function calculateNoOfPeices(ByVal dosage As String, ByVal days As Integer) As String
Dim noOfPiece As Double = 0.0
If txt_dosage.Text.Length > 4 Then
' need logic to extract the values from the dosage
'and need to multiply with no_days
Else
'lbl_notif.Visible = False
noOfPiece = "-1"
End If
Return noOfPiece.ToString
End Function
Please help.
Using String.Split
Dim dosage As String = "1-2-1"
Dim IntValues() As String = dosage.Split("-")
Dim fValue As Double = Val(IntValues(0))
Dim sValue As Double = Val(IntValues(1))
Dim tValue As Double = Val(IntValues(2))
Using Regx
Dim pattern As String = "-"
Dim substrings() As String = Regex.Split(dosage, pattern)
Dim fValueR As Double = Val(substrings(0))
Dim sValueR As Double = Val(substrings(1))
Dim tValueR As Double = Val(substrings(2))
Or You can take the Sum using:
Dim overAllSum = dosage.Split("-").ToList().Where(Function(x) IsNumeric(x)).Sum(Function(y) Val(y))
Related
I am trying to strip the country code off the phone number and place it in the textbox txtMobile. cCode is just an empty string.
GlobalsFSiA.COUNTRY_CODES.Split(",") contains +44 and +353.
The phone number I am testing with is +353861234567
For Each code As String In GlobalsFSiA.COUNTRY_CODES.Split(",")
If objEmployee.MobilePhone.Contains(code) Then
cCode = code
End If
Next
Dim number As String = objEmployee.MobilePhone.Replace(cCode, "0")
txtMobile.Text = number
First you have to search for What is actually the split() method is used for and also difference between split() and substring():
I think Substring() is more suitable in this scenario, you can code like this for extracting country code from the given number:
Dim mobileNumber As String = "+353861234567"
Dim countryCode As String = mobileNumber.Substring(0, 3)
using this code I do get the correct result
Dim sTest As String = "+353861234567"
Dim cCode As String = ""
Dim numbers As New List(Of String)()
numbers.Add("+44")
numbers.Add("+353")
For Each code As String In numbers
If sTest.Contains(code) Then
cCode = code
End If
Next
Dim number As String = sTest.Replace(cCode, "0")
Is the cCode declared internally? Might be an option to take the value from the object before parsing it
I have a datatable that returns a column name location which is a combination of city,state and zipcode,i have stored that column values in an array. now i want to split the array data and store in another array.
here is the code but it is not working.
Dim eTemp As DataTable = dt.DefaultView.ToTable(True, "Location")
Dim s() As String
Dim Count As Integer = PortCodeTemp.Rows.Count - 1
ReDim s(0 To Count)
For i = 0 To Count
s(i) = PortCodeTemp.Rows(i).Item("Location")
For t As Integer = 0 To Count
Dim items As String() = s.Split(",".ToCharArray())
Next
Next
Your issue is here
Dim items As String() = s.Split(",".ToCharArray())
because there is no Split method for an array.
I think you meant to split the string at index i, which is where you stored the string value of location.
Dim items As String() = s(i).Split(",".ToCharArray())
Update
I'm not sure why you're doing it this way but here is something you can try. Instead of using an array I just used a List(Of String) so there's no need to be doing a redim in every loop.
Dim allItems As New List(Of String)
For i = 0 To PortCodeTemp.Rows.Count - 1
Dim location as String = PortCodeTemp.Rows(i).Item("Location")
allItems.AddRange(location.Split(",".ToCharArray()))
Next
Therefore, allItems should contain everything.
given a string "abc123". How do I separate them and put into a variable? For example abc will stored into a string variable "a" and 123 will stored into an integer variable "b". Thanks!
Create two StringBuilders to accumulate the pieces for the letters and numbers, like this:
Dim letters As New StringBuilder()
Dim digits As New StringBuilder()
Now loop through the given String and determine if the characters is a digit or a letter, adding it to the appropriate StringBuilder, like this:
Dim theString As String = "abc123"
For Each c As Char In theString
If Char.IsDigit(c) Then
digits.Append(c)
End If
If Char.IsLetter(c) Then
letters.Append(c)
End If
Next
Finally, you can get the String representation of each StringBuilder, like this:
Dim a As String = letters.ToString()
Dim b As String = digits.ToString()
Dim str = "abc123"
Dim letters = New [String](str.Where(Char.IsLetter).ToArray())
Dim digits = Convert.ToInt32(New [String](str.Where(Char.IsDigit).ToArray()))
Note: I code in c# so i generated this using C# to VB code converter.
I am using ASP.NET Visual Basic.
I have an integer field in my database which I need to read as a date e.g. 20130813. But at the moment it says I cannot convert to datetime.
Is there a way to split the integer up into DD, MM, YYYY so I could even store those in variables to then use to create a date. Or a simple way of just converting to date.
Thanks for your time.
Try this:
Dim provider As CultureInfo = CultureInfo.CurrentCulture
Dim sData As String = "20130813"
Dim myDate As Date = Date.ParseExact(sData, "yyyyMMdd", provider)
You can convert your number to string easily, if needed:
Dim myNum As Integer = 20130813
sData = myNum.ToString()
See MSDN Documentation for more information.
As you're starting with a number, you could either convert it to a string and use DateTime.TryParseExact, or use a bit of integer arithmetic to split it up:
Dim n As Integer = 20130813
Dim yr As Integer = n \ 10000
Dim mon As Integer = (n - 10000 * yr) \ 100
Dim d As Integer = n Mod 100
Dim dt As New DateTime(yr, mon, d)
Quicker:
Dim d As Date = Date.FromOADate(41498)
(from Robert Shutt in experts-exchange.com)
Private Sub stringtodate(ByVal dateval As String)
Dim yy As String
Dim mm As String
Dim dd As String
Dim newdate As Date
yy = dateval.Substring(0, 4)
mm = dateval.Substring(4, 2)
dd = dateval.Substring(6, 2)
newdate =Convert.ToDateTime(dd & "/" & mm & "/" & yy)
MsgBox(newdate.ToString())
End Sub
i have 2 table in my dataset, what i trying to achieve is convert 2 table item into string
Public Shared Function mtdCDsToStr(ByVal pDataSet As DataSet) As String
Dim sDs As String
Dim sb As New System.Text.StringBuilder
Dim drRow As DataRow
Dim dcColumn As DataColumn
Dim dtTable As DataTable
Dim x As Integer = 0
For Each dtTable In pDataSet.Tables
For Each drRow In pDataSet.Tables(x).Rows
Dim colName(pDataSet.Tables(x).Columns.Count) As String
Dim i As Integer = 0
For Each dcColumn In pDataSet.Tables(0).Columns
colName(i) = dcColumn.ColumnName
sb.Append(colName(i) + "," + drRow(colName(i)).ToString + ",")
i += 1
Next
sb.Append("|")
Next
x += 1
sb.Append("$")
Next
sDs = sb.ToString
Return sDs
End Function
code explanation
the function is to pass in a dataset and convert the dataset into a string
what i trying to achieve is convert multiple datatable into a string but i can only loop one table in my code above, what should i do in order to loop multi table? =(
change as below
For Each dtTable As DataTable In dataSet.Tables
For Each dr As DataRow In dtTable.Rows
For Each column As DataColumn In dtTable.Columns
sb.Append(column.ColumnName + "," & dr(column.ColumnName).ToString() & ",")
Next
sb.Append("|")
Next
sb.Append("$")
Next
But rather than converting DataSet to string you may try to get XML from DataSet. I'm Not sure What is the exact requirement you converting to sting, but XML will be good way to communicate Data.
xmlString =lpDataSet.GetXml()