ASP.Net VB Separate digit and alphabet - asp.net

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.

Related

How to extract values from the '-' seperated string in VB.Net [duplicate]

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))

Replace and Contains not recognizing the string

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

Formatting a column to use a phone number format in EPPlus

I have phone numbers stored as a string of numbers, e.g.: 6028035615, but CAST them as integers when querying. I want to format the data in the cell in proper phone number format, such as (602) 803-5615 as Excel would. But how do I state the format type I want to use?
If I use:
objData.Columns.Add("HPhone", GetType(Integer))
wshUS.Column(i).Width = 15 ' PhoneH
wshUS.Column(i).Style.Numberformat.Format = "###-###-####"
or
wshUS.Column(i).Style.Numberformat.Format = "(###) ###-####"
a blank cell is shown as either -- in the former case or ()- in the latter.
Is there a format type that I can put after the = to get phone numbers as Excel would?
How would I use conditional formatting to overcome that problem?
I am writing in VB.NET in codebehind for web pages.
What is objData? Generally speaking, you shouldn't need to add columns to a sheet - EPPlus will do this automatically when your reference the cell for the first time. My VB is a little rusty but this works fine for me:
<TestMethod> _
Public Sub PhoneNumberFormatTest()
Dim file = New FileInfo("c:\temp\temp.xlsx")
If file.Exists Then
file.Delete()
End If
Dim pck = New ExcelPackage(file)
Dim workbook = pck.Workbook
Dim worksheet = workbook.Worksheets.Add("newsheet")
Const number As String = "6028035615"
Const format As String = "(###) ###-####"
Dim startcell = worksheet.Cells(1, 1)
startcell.Value = Int64.Parse(number)
startcell.Offset(1, 0).Value = Int64.Parse(number) + 1
worksheet.Column(1).Width = 15
worksheet.Column(1).Style.Numberformat.Format = format
pck.Save()
End Sub
I downloaded the source code solution of EPPlus 3 (stable) and ran this as a unit test...
EDIT: Added string manipulation:
<TestMethod> _
Public Sub PhoneNumberFormatTest()
Dim file = New FileInfo("c:\temp\temp.xlsx")
If file.Exists Then
file.Delete()
End If
Dim pck = New ExcelPackage(file)
Dim workbook = pck.Workbook
Dim worksheet = workbook.Worksheets.Add("newsheet")
Const number As String = "6028035615"
Const format As String = "(###) ###-####"
Dim startcell = worksheet.Cells(1, 1)
'Directly to numbers with parse
startcell.Value = Int64.Parse(number)
startcell.Offset(1, 0).Value = Int64.Parse(number) + 1
'OR as a string back to a number.
Dim stringcell = startcell.Offset(2, 0)
stringcell.Value = number
stringcell.Value = Int64.Parse(DirectCast(stringcell.Value, String))
stringcell.Style.Numberformat.Format = "(###) ###-####"
'As string - does not work as number
stringcell.Offset(0, 1).Value = number
stringcell.Offset(0, 1).Style.Numberformat.Format = "(###) ###-####"
worksheet.Column(1).Width = 15
worksheet.Column(1).Style.Numberformat.Format = format
pck.Save()
End Sub
The exact formatting that excel use for phone numbers is: "[<=9999999]###-####;(###) ###-####".
I suggest you also check this answer: EPPlus Format Cell as "Accounting" Number. It shows how to get all built-in formatting formula.

how to split array data in vb.net?

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.

loop through datatable to create a string that looks like this

I want to loop through my datatable column called SDESCR and created a string that looks like this.
Dim labels As String() = {"North", "South", "East", "West", "Up", "Down"}
this is what i am trying and it is not working
Dim labels As String()
For Each row As DataRow In tablegraph.Rows
labels = labels " ' " + row.Item("SDESCR") + " ',"
Next row
THANK YOU FOR THE HELP, I WILL TEST THEM TOMORROW AND SEE WHICH WORKS BEST AND MARK ONE CORRECT.
Do this instead
Dim labels As String()
Dim labelsList As List(Of String) = New List(Of String)
For Each row As DataRow In tablegraph.Rows
labelsList.Add(row.Item("SDESCR"))
Next
labels = labelsList.ToArray()
If you need it to be a comma delimited list instead you can just do
Dim commaLabels As String = String.Join(labels, ",")
If you need them stored in an array, then you can add them to a list and then call the ToArray() method on the list:
Dim labelList As New List(Of String)
For Each row As DataRow In tablegraph.Rows
labelList.Add(row.Item("SDESCR"))
Next row
Dim labels As String() = labelList.ToArray()
If you want a string, use this (string builder is best due to memory management issues with constant string declaration.
Dim labels As New StringBuilder()
For Each row As DataRow In tablegraph.Rows
labels.Append(" ' " + row.Item("SDESCR") + " ',")
Next row
Then when you need the string, use labels.ToString()
If you want an array, use this...
Dim labels As New List(Of String)()
For Each row As DataRow In tablegraph.Rows
labels.Add(row.Item("SDESCR"))
Next row
Then when you need the array, use labels.ToArray()
The main reason your above code is failing is that you are declaring an Array of strings.
If you mean a String-Array instead of a String, this works:
Dim labels(tablegraph.Rows.Count - 1) As String
For i As Int32 = 0 To tablegraph.Rows.Count - 1
labels(i) = tablegraph(i)("SDESCR").ToString
Next
If you want them separated with a comma, you can use a StringBuilder to append:
Dim labels As New System.Text.StringBuilder
For i As Int32 = 0 To tablegraph.Rows.Count - 1
labels.Append(" ' ").Append(tablegraph(i)("SDESCR").ToString).Append(" ',")
Next
If labels.Length <> 0 Then labels.Length -= 1 'remove last comma'
Dim labelString As String = labels.ToString
There are plenty of ways to do this; here is an approach using LINQ
Dim labels As String() = (From myRow In tablegraph _
Select CType(myRow.Item("SDESCR"), String)).ToArray
It's worth nothing that this will fail for NULL/NOTHING values of "SDESCR".
If you just want a single comma-separated values you can do this:
Dim sOutput = String.Join(",", (From myRow In tablegraph _
Select CType(myRow.Item("SDESCR"), String)).ToArray)
labels = labels " ' " + row.Item("SDESCR") + " ',"
^
Plus sign missing?
Also, when you do this you'll be left with an extra , at the end of your string. So you'll want to strip that off.

Resources