Here is some sample code:
Dim arrValue(3) as Integer
arrValue(0) = 5
arrValue(1) = 4
arrValue(2) = 7
arrValue(3) = 1
How can I display those four values next to each other.
More specifically, given those values how can I make txtValue.Text = 5471
Edit:
The idea I had would be to use some sort of function to append each one to the end using a loop like this:
Dim finalValue
For i As Integer = 3 To 0 Step -1
arrValue(i).appendTo.finalValue
Next
Obviously that code wouldn't work though the premise is sound I don't know the syntax for appending things and I'm sure I wouldn't be able to append an Integer anyway, I would need to convert each individual value to a string first.
Another method is to use String.Join:
Sub Main
Dim arrValue(3) as Integer
arrValue(0) = 5
arrValue(1) = 4
arrValue(2) = 7
arrValue(3) = 1
Dim result As String = String.Join("", arrValue)
Console.WriteLine(result)
End Sub
If I understand your question correctly, you can use StringBuilder to append the values together.
Dim finalValue as StringBuilder
finalValue = new StringBuilder()
For i As Integer = 3 To 0 Step -1
finalValue.Append(arrValue(i))
Next
Then just return the finalValue.ToString()
Convert the integers to strings, and concatenate them:
Dim result as String = ""
For Each value as Integer in arrValue
result += value.ToString()
Next
Note: using += to concatenate strings performs badly if you have many strings. Then you should use a StringBuilder instead:
Dim builder as New StringBuilder()
For Each value as Integer in arrValue
builder.Append(value)
Next
Dim result as String = builder.ToString()
for i = lbound(arrValue) to ubound(arrValue)
ss=ss & arrValue(i)
next i
end for
debug.print ss
Dim value as string = ""
For A As Integer = 1 To Begin.nOfMarks
value += "Mark " & A & ": " & (Begin.Marks(A)) & vbCrLf
Next A
Related
I need to add a code (123456) into a line of text in a file.
\\ESSEX [D]\\\\\\Tina Richardes\\\\\\\\\\\\\\\\\\\\\\\
The code needs to be entered after the 3rd "\" so it would look something like this.
\\ESSEX [D]\123456\\\\\Tina Richardes\\\\\\\\\\\\\\\\\\\\\\\
The text is always located on line 124 of the file.
If the [D] is always there a short and easy way would be to do:
Dim MyString As String = "\\ESSEX [D]\\\\\\Tina Richardes\\\\\\\\\\\\\\\\\\\\\\\"
MyString = MyString.Insert(MyString.IndexOf("[D]") + 3, "123456")
Otherwise you could do:
Dim MyString As String = "\\ESSEX [D]\\\\\\Tina Richardes\\\\\\\\\\\\\\\\\\\\\\\"
Dim d As Integer = 0
For Each i As Match In Regex.Matches(MyString, "\\")
If d = 2 Then
MsgBox(MyString.Insert(i.Index + 1, "132456"))
End If
d = d + 1
Next
You can use File.ReadAllLines and File.WriteAllLines and string methods:
Dim lines = File.ReadAllLines(path)
If lines.Length < 124 Then Return
Dim line = lines(123)
Dim tokens = line.Split(New String() {"\"}, StringSplitOptions.None)
If tokens.Length < 4 Then Return
tokens(3) = "123456"
lines(123) = String.Join("\", tokens)
File.WriteAllLines(path, lines)
I would just loop through the string, counting the occurrences of the backslash and then exiting when you have found the third occurrence.
You would need to keep count of the index and then use this with the String.Insert method to insert the "123456" code:
Dim s As String = "\\ESSEX [D]\\\\\\Tina Richardes\\\\\\\\\\\\\\\\\\\\\\\"
Dim count As Integer = 0
Dim index As Integer = 0
For Each c In s
If c = "\" Then count += 1
index += 1
If count = 3 Then Exit For
Next
s = s.Insert(index, "123456")
Output:
\\ESSEX [D]\123456\\\\\Tina Richardes\\\\\\\\\\\\\\\\\\\\\\\
This is my string:
Dim value as string = "IR_10748(1).jpg"
How can I get this number 1 into another variable? I am thinking to use split.
How I can use to get this value in vb.net?
See String.Substring(Integer, Integer) and String.IndexOf(String).
Dim value As String = "IR_10748(1).jpg"
Dim startIndex As Integer = value.IndexOf("(") + 1
Dim length As Integer = value.IndexOf(")") - startIndex
Dim content As String = value.Substring(startIndex, length)
Regular expressions might be cleaner. This should work:
dim Result as string = Regex.Match(value, "(?<=\().*(?=\))").Value
It'll extract one or more characters contained between the parentheses.
Try this:
Dim value as String = "IR_10748(1).jpg"
Dim splitStrings() as String
'Split on either opening or closing parenthesis -
'This will give 3 strings - "IR_10748","1",".jpg"
splitStrings = value.Split(New String() {"(",")"}, StringSplitOptions.None)
'Parse as integer if required
Dim i as Integer
i = Integer.Parse(splitStrings(1))
Demo
It's not the prettiest, but this gets "1" using Remove and Split :
Dim value as String = "IR_10748(1).jpg"
Dim num As String = value.Remove(0, value.IndexOf("(") + 1).Split(")")(0)
That gives num = "1"
You can get the number more reliably than using String.Split. You'll want to use LastIndexOf to get the final opening parenthesis just in case you have a filename like "a(whatever)(1).ext", and you should inspect the filename without its extension, in case you have a filename like "a(1).(9)":
Dim value As String = "IR_10748(1).jpg"
Dim fn = Path.GetFileNameWithoutExtension(value)
Dim lastOpen = fn.LastIndexOf("(")
If lastOpen >= 0 Then
Dim length = fn.IndexOf(")", lastOpen + 1) - lastOpen - 1
If length >= 1 Then
Dim numAsString = fn.Substring(lastOpen + 1, length)
Console.WriteLine(numAsString)
ElseIf length = 0 Then
' do something if required
Console.WriteLine("No number in the parentheses.")
Else
' do something if required
Console.WriteLine("No closing parenthesis.")
End If
Else
' do something if required
Console.WriteLine("No opening parenthesis.")
End If
I have a Note as stringBuilder with word and date: reval 41/50/50
I want to manipulate it, so I will have: reval 05/05/14.
(The date is only when I have the word reval before)
My function is:
Sub correctDateShowing(ByRef Note As StringBuilder)
Dim index, i, j As Integer
For index = 0 To Note.Length - 2
If (Note(index).ToString = "r" And Note(index + 1).ToString = "e") Then
For i = 6 To Note.Length - 1 'start from 6,because I want only the date to be reversed
'Here I am Stuck!!
Next
End If
Next
End Sub
I try to do some replacing with a tmp variable but it didn't work.
I will be glad to get some help.
Thanks All!!!
Sub CorrectDateShowing(ByRef Note As StringBuilder)
Dim str As String = Note.ToString()
Dim arr As String() = str.Split(" "c)
arr(1) = StrReverse(arr(1))
Note = New StringBuilder(arr(0) & " " & arr(1))
End Sub
Split the text into two parts, reverse the second part (the date) and then reconnect them.
Try this:
Dim tempCharArray As char[]
Dim dateStartIndex, dateLength As int
'' Here you need to calculate the date start index and date length (i guess the date length is always 8)
Note.CopyTo(dateStartIndex, tempCharArray, 0, dateLength)
Note.Replace(new String(tempCharArray), new String(Array.Reverse(tempCharArray)), dateStartIndex, dateLength)
I am trying to strip all characters except letters and spaces but i am unable to do so. The code i currently have is below, how could i change that so it does allow spaces? At the moment it takes the text, strips it and it all becomes one big line of text.
Dim InputTxt As String = InputText.Text
Dim OutputTxt As System.Text.StringBuilder = New System.Text.StringBuilder()
For Each Ch As Char In InputTxt
If (Not Char.IsLetter(Ch)) Then
OutputTxt.Append(Ch)
Continue For
End If
Dim CheckIndex As Integer = Asc("a") - (Char.IsUpper(Ch) * -32)
Dim Index As Integer = ((Asc(Ch) - CheckIndex) + 13) Mod 26
OutputTxt.Append(Chr(Index + CheckIndex))
Next
OutputText.Text = (OutputTxt.ToString())
Dim output = New StringBuilder()
For Each ch As Char In InputTxt
If Char.IsLetter(ch) OrElse ch = " " Then
output.Append(ch)
End If
Next
OutputText.Text = output.ToString()
Not fully tested but a simple Regex should sustitute all of your code
Dim s = "ADB,12.#,,,122abC"
Dim result = Regex.Replace(s, "[^a-zA-Z ]+", "")
Console.WriteLine(result)
--> output = ADBabC
Here you can find the Regular Expression Pattern Reference
And here is a way to use LINQ to query the string.
Dim candidateText = "This is a test. Does it work with 123 and !"
Dim q = From c In candidateText
Where Char.IsLetter(c) OrElse c=" "
Select c
candidateText = String.Join("", q.ToArray)
Edit
Removed the Char.IsWhiteSpace in query to match the OP question.
I think graumanoz solution is best and doesn't use any unnecessary operations like ToList, but just for kicks:
Shared Function Strip(ByVal input As String)
Dim output = New StringBuilder()
input.ToList().Where(Function(x) Char.IsLetter(x) OrElse x = " ").ToList().
ForEach(Function(x) output.Append(x))
Return output.ToString()
End Function
I have some problem in my code :
For i = 0 To Split(awal, vbCrLf).Length - 1
For j = 0 To Split(hasil(i), " ").Length - 1
hasil1(j) = hasil(i).Split(" ")
Next j
Next i
it comes with error :
"Value of type '1-dimensional array of String' cannot be converted to
'String' " in the " hasil1(j) = hasil(i).Split(" ") " section.
can anyone explain to me how to solve this, please?
It is not clear how you initialize your hasil array,
however, you could simplify your code using a List(Of String) in this way
Dim hasil() as String = New String() {"abcd 123","efgh 11","ijkl"}
Dim hasil1 as List(Of String) = New List(Of String)()
For i = 0 To hasil.Length - 1
hasil1.AddRange(hasil(i).Split(" "))
Next i
You can allways convert it back to a string array with:
Dim s() as String = hasil1.ToArray()
You problem is that you are trying to add an Array to a String Array.
The Split() function returns an Array and this is causing the error.
If your intention is to get an Array of Arrays, make that variables that way, if not, what was the purpose on using Split(" ") on that line?
Edit:
Based on you comment:
You will have to loop once more for this to be complete:
Dim it As Integer
Dim tmp() As String
it = 0
For i = 0 To Split(awal, vbCrLf).Length - 1
For j = 0 To Split(hasil(i), " ").Length - 1
tmp = hasil(i).Split(" ")
For k = 0 To tmp.Length - 1
hasil1(it) = tmp(k)
it += 1
Next
Next j
Next i
My skills in VB.Net are rusted and I'm not in my dev machine, so try this and let me know if something doesn't work.