Find position in array of strings in Word VBA - vba

I have an array of strings "alpha", "beta"... and I'd like to test another string string2 against this to return 1, if string2 = "alpha", 2 if it equals "beta" etc.
I could do this if it were Excel using application.match, but this doesn't exist in Word.
The end goal is to return the unicode value for the appropriate Greek letter from the name of the letter, so if there's a better way of doing it, I'm open to suggestions. Maybe I've been writing too much python, and lists seem like the easy way.

You need to loop through the array and test each value against the searched value.
Here's some pseudo code to get you started.
Dim greek() As String
Dim x As Integer
Dim searchString As String
Dim match As Integer
For x = LBound(greek) To UBound(greek)
If greek(x) = searchString Then
match = x
Exit For
End If
Next x

Related

How to insert a string into an ArrayList in VB.NET?

I am working in VB.net to help teach myself the language. The program I am creating requires the input of two strings, both of which I would like to add to an ArrayList in a specific way.
For the first input, say Hello World, I would like to capitalize the letters and input it into a list of the form:
InputCharacterList = {"H","E","L","L","O"," ","W","O","R","L","D"}
After this I would then like to convert this into an ArrayList of Integers using the characters ASCII values, so:
IntegerList = {72,69,76,76,79,32,87,79,82,76,68}
Note: I want to capitalize all the letters because I want to guarantee that my IntegerList contains only integers with 2 numbers.
Now for the next part, I pretty much want to do the reverse. So if the input is a string: 7269767679, I want to create an ArrayList of Integers such as:
InputIntegerList = {72,69,76,76,79}
I then want to convert it back into an ArrayList using ASCII values, so:
CharacterList = {"H","E","L","L","O"}
This second way seems to be much more difficult to me. I cannot easily see how to step through a string two values at a time. Nevertheless I cannot easily see how to do either approach in VB.net
Thanks for the help!
Dim sInput As String = "Hello World"
Dim sOutput As String
Dim AL_c As New ArrayList
Dim AL_b As New ArrayList
' Add Input to Arraylist
AL_c.AddRange(sInput.ToUpper.ToArray)
' Add Bytes to Arralist
For Each c As Char In AL_c
AL_b.Add(Convert.ToByte(c))
Next
sOutput = String.Concat(AL_b.ToArray)
' Reverse
sInput = "7269767679"
Dim i As Integer
Dim cTemp As Char
AL_c.Clear()
AL_b.Clear()
i = sInput.Length
' Add Bytes to Arraylist
For Each c As Char In sInput.ToArray
If (i And 1) = 0 Then
cTemp = c
Else
AL_b.Add(CByte(cTemp & c))
End If
i -= 1
Next
' Add Characters to Arraylist
For Each b As Byte In AL_b
AL_c.Add(Chr(b))
Next
' Create Output string = "HELLO"
sOutput = String.Concat(AL_c.ToArray)

Change a string and insert characters in between

I have this string: 71892378917238978
I want to do this: 71892-37891723-8978
I was trying this, but how i can do it multiple times?
String.Insert(6, "-")
We could start with this idea. Create an array of the substrings that you want to exist between the dash symbol. In your expected string you have 71892-37891723-8978, so the first block is composed of 5 char and the second block is 8 char.
Using the string.Substring method we could extract these substrings parts and store them in an array. Finally we could use string.Join to rebuild the string with the expected separator
Dim s As String = "71892378917238978"
' Define the substring blocks
Dim blocks As Integer() = New Integer() {5,8}
' Define the array that will contain the substrings
Dim substrings(blocks.Length) As String
' Get the blocks
For x As Integer = 0 To blocks.Length - 1
substrings(x) = s.Substring(0,blocks(x))
s = s.Substring(blocks(x))
Next
' Join together with the last part not included in the substrings
Dim result = String.Join("-", substrings) & s
Console.WriteLine(result)
You could generalize this code putting everything in a method where you could pass the string, the blocks and the separator.

Parsing through an Array For Next loop Visual Basic

I am stuck here. Spent hours trying many different approaches but nothing is working
I have an array that holds text that looks like this
4456|4450|17
4466|4430|18
4446|4420|19
4436|4410|20
The separator is a pica ("|").
What I am trying to do is run through the array and extract the first two columns in separate strings to compare the values, look for the max, and min.
I am trying to end up with a string like this
4456,4466,4446,4436
Here is the solution:
Dim source As String = prices
Dim stringSeparators() As String = {vbCrLf}
Dim result() As String
result = source.Split(stringSeparators,
StringSplitOptions.RemoveEmptyEntries)
Dim fString As String = String.Join(Of String)(", ", result.Cast(Of String).Select(Of String)(Function(x) x.Split("|")(0)))
MsgBox(fString)
Let's take your example below...
4456|4450|17
4466|4430|18
4446|4420|19
4436|4410|20
prices = [the array shown above]
For Each i As String In prices
high = (i.Split("|"))(0)
highs = highs & highs1 & ","
MsgBox(highs)
Next
The reason you are getting 4,4,5,6,,4,4,5,0,,1,7 is because for each string you are splitting on the | and then taking the first character adding a comma to it.
If you want to get the first column or index whatever you want to call it before the | you need to loop through each string in that array and select out the values...
'this is my test array...
Dim arr As New ArrayList From {"4456|4450|17", "4466|4430|18", "4446|4420|19", "4436|4410|20"}
Now we can use a String.Join function, cast the array for each item as a string and finally select the first item on the split. This will get every item before the | and put them in a string separated with a comma.
Dim fString As String = String.Join(Of String)(", ", arr.Cast(Of String).Select(Of String)(Function(x) x.Split("|")(0)))
If you want the second section select the 1st index as arrays start at 0...
Dim sString As String = String.Join(Of String)(", ", arr.Cast(Of String).Select(Of String)(Function(x) x.Split("|")(1)))
Here is my screenshot of the outputs...

VB.NET Get text in between Quotations or other symbols

I want to be able to extract a string in between quotation marks or parenthesis etc. to a variable. For example my text might be "Hello there "Bob" ". I want to extract the text "Bob" from in between the two quotation marks and put it in the string "name" for later use. The same would be for "Hello there (Bob)". How would I go about this? Thanks.
=======EDIT======
Sorry, I worded this poorly. Ok, so lets say I have a textbox(Textbox1) and a button. If the user inputs the text: MsgBox "THIS IS MY MESSAGE" I want that when the Button is pressed, only the text THIS IS MY MESSAGE is displayed.
This is a solution very simple:
Dim sAux() As String = TextBox1.Text.Split(""""c)
Dim sResult As String = ""
If sAux.Length = 3 Then
sResult = sAux(1)
Else
' Error or something (number of quotes <> 2)
End If
There are basically three methods -- regular expressions, string.indexof and substring and finally looping over the characters one by one. I would avoid the latter as it is just reinventing the wheel. Whether to use regexs or indexof depends upon the complexity of your requirements and data. Indexof is a bit wordy but fairly straightforward and possibly just what you want in this case.
Dim str as String = "Hello there ""Bob"""
Dim startName as Integer
Dim endName as Integer
Dim name as String = ""
startName = str.IndexOf("""")
endName = str.Indexof("""", If(startName > 0, startName,0))
If (endName>startName) Then
name = str.SubString(startName, endName)
End If
If you need to do this for arbitrary symbols, then you want regexs.

VB.Net Extract numbers from string function

My request is one that can extract a number somewhat by a search.
Example:
animalsOwned|4 would return an containing "4"
animals|3|2|1|3 would return an array containing "3", "2", "1", "3"
This would make it easier for me during a file stream reader.
Thank you
Dim astring = "ABCDE|1|2|3|4"
Dim numbers = (From s In astring
Where Char.IsDigit(s)
Select Int32.Parse(s)).ToArray()
This LINQ statement should help. It simply checks each character in a string to see if it's a digit. Note that this only applies to single digit numbers. It becomes a bit more complicated if you want "ABC123" to return 123 vs. 1, 2, 3 array.
Try regular expression. It's a powerful tool for simple text parsing.
Imports System.Text.RegularExpressions
Namespace Demo
Class Program
Shared Function Main(ByVal args As String()) As Integer
Dim array As Integer() = ExtractIntegers("animals|3|2|1|3")
For Each i In array
Console.WriteLine(i)
Next
Return 0
End Function
Shared Function ExtractIntegers(ByVal input As String) As Integer()
Dim pattern As String = "animals(\|(?<number>[0-9]+))*"
Dim match As Match = Regex.Match(input, pattern)
Dim list As New List(Of Integer)
If match.Success Then
For Each capture As Capture In match.Groups("number").Captures
list.Add(Integer.Parse(capture.Value))
Next
End If
Return list.ToArray()
End Function
End Class
End Namespace
I haven't programmed VB for awhile but I'll give you some pseudo code:
First, loop through each line of file. Call this variable Line.
Then, take the index of what you're searching for: like Line.indexOf("animalsOwned")
If it returns -1 it isn't there; continue.
Once you find it, add the Index variable to the length of the search string and 1. (Index=Index+1+Len(searchString))
Then, take a substring starting there, and end at the end of the line.
Explode the substring by | characters, then add each into an array.
Return the array.
Sorry that I can't give you much help, but I'm working on an important PHP website right now ;).
You can do a variable.Split("|") and then assign each piece to an array level.
You can do a count on string and with a while or for loop, you can assign the splited sections to array levels. Then you can do a IsNumeric() check for each array level.