I have just started to learn Visual Basic and I am having trouble with a loop. What I want it to do is print out the string "ABCDEFG" into a list box then remove the last character and print it out until only "A" is left.
This is the code I am using:
Dim abc As String = "ABCDEFG"
For i = 0 To 5
abc.Substring(0, abc.Length - 1)
lstabc.Items.Add(abc)
Next i
The desired result would look like this but all i get is lines of "ABCDEFG"
ABCDEFG
ABCDEF
ABCDE
ABCD
ABC
AB
A
You're never assigning anything different to abc, so it's always adding the full string. Also, you are not specifying a different length to substring. Try this.
Dim abc As String = "ABCDEFG"
Dim abc_alt as String
For i = 0 To abc.Length - 1
abc_alt = abc.Substring(0, abc.Length - i)
lstabc.Items.Add(abc_alt)
Next i
String in c#, vb.net are non mutable. So you need to store the result in another variable and print that variable.
Dim substr As String
substr = abc.Substring(0, abc.Length - i)
lstabc.Items.Add(substr)
Related
I have been using this code as a starting point: https://danwagner.co/how-to-transpose-horizontal-data-to-vertical-data-for-easy-pivot-tables/
One one of my cells Ax (x referring to the number), the content is ABCDEFGHI and I want to substring the cells every 2 characters, and the last set is 3 characters. Final result looks like:
AB CD EF GHI
At line 44, using the variable
varDetails = .Range(.Cells(lngIdx, 1), .Cells(lngIdx, 4))
and think that is where I need to modify the code. I am not fluent enough with VBA and need some help.
To split the data from your string you can use the following code
Sub SplitStringEveryTwoCharacters()
Dim arrayWithValuesByTwo() As String
Dim myString As String
'Just replace with your data
myString = "ABCDEFGHIJKLM"
'Resize
ReDim arrayWithValuesByTwo(Len(myString) - 1)
'For each 2 character in string
For i = 1 To Len(myString) Step 2
'Add in array
If (i <= Len(myString) - 1) Then
arrayWithValuesByTwo(i - 1) = Mid$(myString, i, 2)
End If
If (i = Len(myString)) Then
arrayWithValuesByTwo(i - 1) = Mid$(myString, i, 1)
End If
Next
End Sub
What you need to change
Here I have set my string into a variable with myString = "ABCDEFGHIJKLM" but you can easily change this and take it directly from a cell with something like myString = Range("A5").
You can access you data with arrayWithValuesByTwo(1) for example. Just loop through it to get all of the values.
I have a script that takes the contents of a cell, and puts the first 2 characters of the cell into a string array. I need to later compare that string array to a string, but I can't seem to get that to work. Here's what I have:
For i = 2 To 600
colStr = Sheets("smartlist").Cells(i, "A").Value
If colStr <> "" Then
ReDim charArray(Len(colStr) - 1)
For j = 1 To Len(colStr)
charArray(j - 1) = Mid$(colStr, j, 1)
Next
strArray = LCase(charArray(0)) & LCase(charArray(1))
If CStr(Join(strArray)) = CStr(Join(pwArray)) Then
Now, I've tried:
If charArray = "ab"
If Join(charArray) = "ab"
If CStr(Join(charArray)) = "ab"
I'm pretty lost at this point. Any suggestions would be welcome!
Edit: added the whole function up until I get the 'Type mismatch'
You could use Join(charArray, "") - without "" it joins the elements with space so the result of your initial try was "a b"
Firstly, you really need to clarify what you're doing. You say that later you need to check what's in the string. If that is the case, then you don't need an array, you simply need another string...
Dim chars As String
chars = Left$(cellToTest, 2)
Later you can test more simply using the InStr function, like so...
Dim loc As Integer
loc = Instr(colStr, chars)
If loc > 0 Then
...
If you want to turn this into a function on your spreadsheet you can use the following in the cells as formulas...
=LEFT(A1, 2)
=SEARCH(A3, A1)
Here's a little screen shot of what I mean...
I am building a tool for Autodesk Inventor that works with an expression string.
=Pipe Ø<Pipe_OD> x <Pipe_t> - lg. <Pipe_length>mm
The text between <...> can be almost anything it's what the user made as input so these values are not fixed. And the number of <...> in the string can vary from 0 to 5.
What I would like to have as result for this string is following:
A converted string where the values between the <...> are replaced by number with an ascending value.
=Pipe Ø<1> x <2> - lg. <3>mm
And a string() where the values that are replaced by the numbers (above) are stored in.
I found a method that can work for strings with 1 <...> in the string but now that the quantity is variable I'm clueless about how I can do this.
Link to method
Edit: New answer with regular expressions (much better, will still have problems with additional < and > though)
Dim s As String = "abc <pipe_val1> 123 <pipe_val2> &*( <pipe_val3>k"
Dim myValues As New List(Of String)
Dim matches As MatchCollection = Regex.Matches(s, "<(.|\n)*?>", RegexOptions.IgnoreCase)
Dim totalDiff As Integer = 0
Dim idx As Integer = 0
For Each ma As Match In matches
Dim realIndex = ma.Index - totalDiff
s = s.Remove(realIndex, ma.Length).Insert(realIndex, "<" & idx.ToString & ">")
idx += 1
totalDiff += ma.Length - (idx.ToString.Count + 2)
myValues.Add(ma.Value.Trim({"<"c, ">"c}))
Next
"abc <pipe_val1> 123 <pipe_val2> &*( <pipe_val3>" will be changed to "abc <0> 123 <1> &*( <2>" and myValues will contain "pipe_val1", "pipe_val2" and "pipe_val3"
The variable datatype is string .it contain string value like greater than 300 chars. i want to split that string by 150 char and stored in the string array using vb.net
My code:
msg = t1("fld_msg")
msg1 = msg.Length
For i = 0 To msg.Length - 1
strarr = msg.Substring(0, 150)
Next
Error:
value of type string cant be converted into one dimensional array
You need a counter to increment the cells in the array
msg = t1("fld_msg")
msg1 = msg.Length
dim Counter as Integer = 0
For i = 0 To msg.Length - 1 Step 150
strarr(Counter) = msg.Substring(i, 150)
Counter += 1
Next
Substring returns a value of type string.
You are trying to put the results into an array.
Try:-
strarr(0) = msg.Substring(0,150)
strarr(1) = msg.Substring(150)
Required correction in your code is to assign substring value to an index of array "strarr(i)" rather than to an array "strarr". Also taking a substring like (0,XX) is not correct. Every time it will return a substring from index 0, use (i*NumberOfCharactersToInclude,XX) instead. But here 'XX' also matters.
For example,
if string has 311 characters and XX is fixed to 150, it will lead to an error in third substring. So i would suggest you to go with this one: (Assuming Framework is 3.5 or above)
For i As Integer = 0 To len ' len represents possible no. of substrings
strarr(i) = New String(msg.Skip(i * 150).Take(150).ToArray)
Next
I have a string that has 2 sections broken up by a -. When I pass this value to my new page I just want the first section.
An example value would be: MS 25 - 25
I just want to show: MS 25
I am looking at IndexOf() and SubString() but I can't find how to get the start of the string and drop the end.
This might help:
http://www.homeandlearn.co.uk/net/nets7p5.html
Basically the substring method takes 2 parameters. Start position and length.
In your case, the start position is 0 and length is going to be the position found by the IndexOf method -1.
For example:
Dim s as String
Dim result as String
s = "MS 25 - 25"
result = s.SubString(0, s.IndexOf("-")-1)
You could use the Split function on the hyphen.
.Split("-")
If you want to stay away from Split, you could use SubString
yourString.Substring(0, yourString.IndexOf("-") - 1)
EDIT
The above code will fail in the instances where there is no hyphen at all or the hyphen is in the beginning of the string, also when there are no spaces surrounding the hyphen, the full leading substring will not be returned. Consider using this for safety:
Dim pos As Integer
Dim result As String
pos = yourString.IndexOf("-")
If (pos > 0) Then
result = yourString.Substring(0, pos)
ElseIf (pos = 0) Then
result = String.Empty
Else
result = yourString
End If