i have textbox with text is "ab1234de524sfe6985dsef456" and i want take all number in this to array
like
arr(0)=1234,
arr(1)=524,
arr(2)=6985,
arr(3)=456
help me, thanks
You can use Regex.Check out following code snippet that is written in c#
string strRegex = #"\d+";
Regex myRegex = new Regex(strRegex, RegexOptions.Singleline);
string strTargetString = #"ab1234de524sfe6985dsef456";
ArrayList list=new ArrayList();
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
list.Add(myMatch.Value);
}
}
list.Dump();
Converted to Visual Basic:
Dim strRegex As String = "\d+"
Dim myRegex As New Regex(strRegex, RegexOptions.Singleline)
Dim strTargetString As String = "ab1234de524sfe6985dsef456"
Dim list As New ArrayList()
For Each myMatch As Match In myRegex.Matches(strTargetString)
If myMatch.Success Then
list.Add(myMatch.Value)
End If
Next
Related
I am trying to replace integers in a string using Visual Basic and can't seem to get it just right.
This is what I currently have:
lblNewPassword.Text = txtOrigPassword.Text.Replace("[0-9]", "Z")
I have also tried:
lblNewPassword.Text = txtOrigPassword.Text.Replace("#", "Z")
And:
lblNewPassword.Text = txtOrigPassword.Text.Replace("*#*", "Z")
You have to use a regex object like this:
C#
string input = "This is t3xt with n4mb3rs.";
Regex rgx = new Regex("[0-9]");
string result = rgx.Replace(input, "Z");
VB
Dim input As String = "This is t3xt with n4mb3rs."
Dim rgx As New Regex("[0-9]")
Dim result As String = rgx.Replace(input, "Z")
Update: if then you want to change vowels by X you can add:
rgx As New Regex("[A-Za-z]")
result = rgx.Replace(result, "X")
I have tried a couple different ways and cannot seem to get the result I want using vb.net.
I have an array of strings. {"55555 ","44444", " "}
I need an array of integers {55555,44444}
This is a wpf page that is sending the array as a parameter to a crystal report.
Any help is appreciated.
You can use the List(Of T).ConvertAll method:
Dim stringList = {"123", "456", "789"}.ToList
Dim intList = stringList.ConvertAll(Function(str) Int32.Parse(str))
or with the delegate
Dim intList = stringList.ConvertAll(AddressOf Int32.Parse)
If you only want to use Arrays, you can use the Array.ConvertAll method:
Dim stringArray = {"123", "456", "789"}
Dim intArray = Array.ConvertAll(stringArray, Function(str) Int32.Parse(str))
Oh, i've missed the empty string in your sample data. Then you need to check this:
Dim value As Int32
Dim intArray = (From str In stringArray
Let isInt = Int32.TryParse(str, value)
Where isInt
Select Int32.Parse(str)).ToArray
By the way, here's the same in method syntax, ugly as always in VB.NET:
Dim intArray = Array.ConvertAll(stringArray,
Function(str) New With {
.IsInt = Int32.TryParse(str, value),
.Value = value
}).Where(Function(result) result.IsInt).
Select(Function(result) result.Value).ToArray
You can use the Array.ConvertAll method:
Dim arrStrings() As String = {"55555", "44444"}
Dim arrIntegers() As Integer = Array.ConvertAll(arrStrings, New Converter(Of String, Integer)(AddressOf ConvertToInteger))
Public Function ConvertToInteger(ByVal input As String) As Integer
Dim output As Integer = 0
Integer.TryParse(input, output)
Return output
End Function
Maybe something like this:
dim ls as new List(of string)()
ls.Add("55555")
ls.Add("44444")
ls.Add(" ")
Dim temp as integer
Dim ls2 as List(Of integer)=ls.Where(function(x) integer.TryParse(x,temp)).Select(function(x) temp).ToList()
Maybe a few more lines of code than the other answers but...
'Example assumes the numbers you are working with are all Integers.
Dim arrNumeric() As Integer
For Each strItemInArray In YourArrayName
If IsNumeric(strItemInArray) Then
If arrNumeric Is Nothing Then
ReDim arrNumeric(0)
arrNumeric(0) = CInt(strItemInArray)
Else
ReDim Preserve arrNumeric(arrNumeric.Length)
arrNumeric(arrNumeric.Length - 1) = CInt(strItemInArray)
End If
End If
Next
My $.02
Dim stringList() As String = New String() {"", "123", "456", "789", "a"}
Dim intList() As Integer
intList = (From str As String In stringList
Where Integer.TryParse(str, Nothing)
Select (Integer.Parse(str))).ToArray
Everything is much easier ))
Dim NewIntArray = YouStringArray.Select(Function(x) CInt(x)).ToArray
I have a text file contains delimited records.
1243;jhhf';982u4k;9u2349;huf8
kij;9238u;98ur23;jfwf;03i24
I need to replace the value of 4th part from every record with the value returned from database or someother source.
Any clue ? expecting VB CODE
Take a look at here( for C# language );
Split string in C#
You could try this (written in C#):
C# release
List<string> newLines = new List<string>();
string[] lines = File.ReadAllLines(filename);
foreach (string line in lines)
{
string[] parts = line.Split(";".ToCharArray());
parts[3] = string_from_db;
newLines.Add(String.Join(";", parts));
}
File.WriteAllLines(filename, newLines.ToArray());
VB.NET release
Dim newLines As List(Of String) = New List(Of String)
Dim lines As String() = File.ReadAllLines(filename)
For Each line As String In lines
Dim parts As String() = line.Split(";")
parts(3) = string_from_db
newLines.Add(String.Join(";", parts))
Next
File.WriteAllLines(filename, newLines.ToArray())
I have a CSV file with many rows in which I need to update/replace column four's value using VB.NET. There are no headers on the columns so the loop can start on row one.
infile.csv
"value1","value2","value3","value4"
After much googling, there are lots of examples on how to read and write CSV files, but none quite what is needed. I know there are multiple ways to accomplish this task, but a requirement is that it's done with VB.NET. Any help would be greatly appreciated.
Final code ported from Marco's C# answer
Private Sub CSVmod(ByVal strFileName As String, ByVal newFileName As String)
Dim strLines As String() = File.ReadAllLines(strFileName)
Dim strList As New List(Of String)
Dim strReplace As String = "test"
For Each line In strLines
Dim strValues As String() = line.Split(",")
If (strValues.Length = 14) Then
strValues(3) = strReplace
strList.Add(String.Join(",", strValues))
End If
Next
File.WriteAllLines(newFileName, strList.ToArray())
I'm sorry, but I can write it only in C#.
I think you won't find many problems to convert to VB.NET.
Here is my code:
private void SubstFile(string filename, string newFileName)
{
// Reading all rows of the file
string[] lines = File.ReadAllLines(filename);
List<string> list = new List<string>();
foreach (string line in lines)
{
// For every row I split it with commas
string[] values = line.Split(',');
// Check to have 4 values
if (values.Length == 4)
{
// Substitute fourth value
values[3] = "new value";
// Add modified row to list
list.Add(String.Join(",", values));
}
}
// Save list to new file
File.WriteAllLines(newFileName, list.ToArray());
}
VB.NET version:
Private Sub SubstFile(filename As String, newFileName As String)
' Reading all rows of the file
Dim lines As String() = File.ReadAllLines(filename)
Dim list As List(Of String) = New List(Of String)()
For Each line As String In lines
' For every row I split it with commas
Dim values As String() = line.Split(","c)
' Check to have 4 values
If values.Length = 4 Then
' Substitute fourth value
values(3) = "new value"
' Add modified row to list
list.Add(String.Join(",", values))
End If
Next
' Save list to new file
File.WriteAllLines(newFileName, list.ToArray())
End Sub
Use python....one line of code
df = pandas.read_csv('file.csv)
im getting an exception on this:
Dim strings_extreme As String()
strings_extreme(0) = ""
it says that i am using it before it is being assigned a value
how do i initialize it?
please note that i need to be able to do this:
strings_extreme = input.Split(","c).Distinct().OrderBy(Function(s) s)
If you truly don't know how many strings there are going to be, then why not just use an IList:
Dim stringList As IList(Of String) = New List(Of String)
stringList.Add("")
You can get the Count of how many strings there are and you can For Each through all the strings in the list.
EDIT: If you're just trying to build an array from a Split you should be able to do this:
Dim strings_extreme() As String = input.Split(...)
Dim strings_extreme As List(Of String) = New List(Of String)
strings_extreme.Add("value1")
strings_extreme.Add("value 2")
strings_extreme.Add("value 3rd")
strings_extreme.Add("value again")
Dim strings() As String = strings_extreme.ToArray()
...
Dim strings_extreme(10) As String
strings_extreme(0) = ""
Further info: http://www.startvbdotnet.com/language/arrays.aspx
Dim strings_extreme as String() = {"yourfirstitem"}
But actually, why not take a look at some more advanced data structure from System.Collections namespace?
Shouldn't bother setting a string to "".
Try using:
Dim strings_extreme(10) As String
strings_extreme(yourIndex) = String.Empty