I am trying to convert an array of characters into a string array (where each character becomes a string), as I need it to be a string array for some processing on the array later in the program. Here is the code I am using:
Dim inputexpression As String = UCase(txtInput.Text)
Dim arrinputexpressionchar() As Char = inputexpression.ToCharArray()
Dim arrinputexpression() As String
For i = 0 To arrinputexpressionchar.Length
arrinputexpression(i) = Char.ToString(arrinputexpressionchar(i))
Next
However, this throws up a 'NullReferenceException was unhandled' (Object reference was not set to an instance of an object) error. Why does this code not work?
You have declared but not initialized the string array.
You could use LINQ:
Dim charsAsStringArray = inputexpression.
Select(Function(c) c.ToString()).
ToArray()
Here's the non-linq way:
Dim strArray(inputexpression.Length - 1) As String
For i = 0 To charArray.Length - 1
strArray(i) = inputexpression(i).ToString()
Next
Related
I'm struggling to solve a small bit of code. What the code does is to first load a CSV file, line by line (starting by line 3), and add it to an array. Then run a regex match and I want to insert the value in an array.
This is my working code, it shows a msgbox with the actual matches:
Dim file = "C:\Users\David\Desktop\mycsv.csv"
Dim basestatisticspath = Path.GetDirectoryName(file)
Dim statistics() As String = IO.File.ReadAllLines(file)
'Dim x As Integer = statistics.Length
'ReDim Preserve statistics(x)
Dim regexlang As Regex = New Regex("(?<=^"")\[.*\]")
Dim regexlinefilename As Regex = New Regex("(?<=^""\[.*?\]\s).*(?="")")
Dim linefilename As Match = Nothing
Dim langmatch As Match = Nothing
Dim filename() As String
Dim lang() As String
For i = 2 To UBound(statistics)
langmatch = regexlang.Match(statistics(i))
linefilename = regexlinefilename.Match(statistics(i))
MsgBox(langmatch.Value & linefilename.Value)
Next
That works well and the actual matches is what I want. So my next step was to add each match to an array to then use it to generate other files.
Therefore I ended up with this:
Dim file = "C:\Users\David\Desktop\myscv.csv"
Dim basestatisticspath = Path.GetDirectoryName(file)
Dim statistics() As String = IO.File.ReadAllLines(file)
'Dim x As Integer = statistics.Length
'ReDim Preserve statistics(x)
Dim regexlang As Regex = New Regex("(?<=^"")\[.*\]")
Dim regexlinefilename As Regex = New Regex("(?<=^""\[.*?\]\s).*(?="")")
Dim linefilename As Match = Nothing
Dim langmatch As Match = Nothing
Dim filename() As String
Dim lang() As String
' Set all value line by line
For i = 2 To UBound(statistics)
langmatch = regexlang.Match(statistics(i))
linefilename = regexlinefilename.Match(statistics(i))
lang(i) = langmatch.Value.ToString
filename(i) = linefilename.Value.ToString
MsgBox(langmatch.Value & linefilename.Value)
Next
After adding the below the program crashes on that line
lang(i) = langmatch.Value.ToString
filename(i) = linefilename.Value.ToString
I am assuming you can add the value of a regex match to a certain position of a string, but it seems I am wrong.
I´ve been searching for an answer with no results (at least to my poor understanding).
How could I convert each of the matches to a string and add it to the i position of the array?
Thanks in advance!
UPDATE:
As #Tval explained, I solved it by including the size of the array when declaring it. Thanks!
Dim filename(UBound(statistics)) As String
Dim lang(UBound(statistics)) As String
you need to initialize the array before you can reference it or you'll get a null reference error, also you can't reference an index that doesn't exist yet or you'll get an index out of range exception.
right now your using an array with a fixed length, so if you want to add a value to it you'll have to re-declare it one index larger every time.
If you want an array of a variable length id suggest using a list, so you can just append values to it without any issues
Dim myList = New List(Of String)
For Each foo As String In bar
myList.Add(bar)
Next
I have the code below:
Dim base64Decoded As String
Dim base64Encoded As String = "aGVsbG8="
Dim data As Byte()
data = System.Text.ASCIIEncoding.ASCII.GetBytes(base64Encoded)
base64Decoded = System.Convert.FromBase64String(data)
MsgBox(base64Decoded)
However, I'm getting an error message at the base64Decoded = ... line:
Value of type 'Byte()' cannot be converted to 'String'.
Any ideas?
First of all, you have to convert this string to an array of bytes using System.Convert.FromBase64String then convert it to String using System.Text.ASCIIEncoding.ASCII.GetString , FromBase64String take a string as parameter. Just try the following:
base64Decoded = System.Text.ASCIIEncoding.ASCII.GetString(System.Convert.FromBase64String(base64Encoded))
in your case aGVsbG8= will be converted to:
hello
Reference
ASCIIEncoding.GetString Method
Convert.FromBase64String Method
I'm trying to convert a string array to byte array. I would like to use Encoding.Default.GetBytes() but it only accepts String and Char(). This is the code
Dim aStringArray(2) As String
aStringArray(0) = "FileName"
aStringArray(1) = "FileSize"
Dim stringArrayBytes() As Byte = Encoding.Default.GetBytes(aStringArray) `this is wrong
If you are trying to combine the array into a single byte array try this
Dim aStringArray(1) As String
aStringArray(0) = "FileName"
aStringArray(1) = "FileSize"
Dim stringArrayBytes As New List(Of Byte)
For Each s As String In aStringArray
stringArrayBytes.AddRange(System.Text.Encoding.Default.GetBytes(s))
Next
Dim bytarray() As Byte = stringArrayBytes.ToArray
Here little bid different approach, based on your example :)
Dim aStringArray(2) As String
aStringArray(0) = "FileName"
aStringArray(1) = "FileSize"
Dim allStrngItems As String = String.Join(String.Empty, aStringArray)
Dim allBytes As Byte() = Encoding.Default.GetBytes(allStrngItems)
I am writing a Ceasaer Function that takes a string and runs it through a variant of the Ceasear cipher, and returns the encoded text. For some reason I am getting an Index Out Of Range error on an Array declared with no specific bounds. Why am I getting this exception, and how do I fix it?
VB.NET Code:
Public Shared Function Ceaser(ByVal str As String) As String
Dim r As String = ""
Dim ints() As Integer = {}
Dim codeints As Integer() = {}
Dim codedints As Integer() = {}
Dim ciphertext As String = ""
For i As Integer = 0 To str.Length - 1
Dim currentch As Integer = Map(str(i))
ints(i) = currentch 'Where exception is happening
Next
Dim primes As Integer() = PrimeNums(ints.Length)
For i As Integer = 0 To primes.Length - 1
codeints(i) = ints(i) + primes(i) - 3
Next
For i As Integer = 0 To codeints.Length - 1
Dim currentnum As Integer = codeints(i) Mod 27
codedints(i) = currentnum
Next
For i As Integer = 0 To codedints.Length - 1
Dim letter As String = rMap(codeints(i))
ciphertext += letter
Next
Return ciphertext
End Function
You have to specify the array bounds before you can acces its elements:
Dim ints As Integer(str.length-1)
will instantiate the array with n elements where n = length of string str.
(Take care: VB .NET array lengths are zero-based, so an array with 1 element is instantiated with array(0)).
You have to adopt the other arrays accordingly.
I have a string. For example stringone/string2
How do I get the section before the "/" and also after the "/"
Dim Word As String = "stringone/string2"
Dim wordArr As String() = Word.Split("/")
Dim stringBefore As String = wordArr(???)
Dim stringBefore As String = wordArr(???)
What is the next step
Split() returns an Array. The first element is at Index 0 (zero), the second element is at Index 1 (one), etc...
You should check to make sure the returned array is your expected size (at least), otherwise you'll get an error attempting to access an index slot that doesn't exist.
Dim Word As String = "stringone/string2"
Dim wordArr As String() = Word.Split("/")
If wordArr.Length = 2 Then
Dim stringBefore As String = wordArr(0)
Dim stringAfter As String = wordArr(1)
Debug.Print("stringBefore = " & stringBefore)
Debug.Print("stringAfter = " & stringAfter)
End If
*By the way, the code you posted is not VB6, it's VB.Net.