Isolate a a substring within quotes from an entire line - vb.net

To start here is an example of a line I am trying to manipulate:
trait slot QName(PrivateNamespace("*", "com.company.assembleegameclient.ui:StatusBar"), "_-0IA") type QName(PackageNamespace(""), "Boolean") value False() end
I wrote a code that will go through and read through each line and stop at the appropriate line. What I am trying to achieve now is to read through the characters and save just the
_-0IA
to a new string. I tried using Trim(), Replace(), and indexof so far but I am having a ton of difficulties because of the quotation marks. Has anyone deal with this issue before?

Assuming your source string will always follow a strict format with only some data changes, something like this might work:
'Split the string by "," and extract the 3rd element. Trim the space and _
quotation mark from the front and extract the first 5 characters.
Dim targetstr As String = sourcestr.Split(","c)(2).TrimStart(" """.ToCharArray).Substring(0, 5)
If the length of the target string is variable it can be done like this:
Dim temp As String = teststr.Split(","c)(2).TrimStart(" """.ToCharArray)
'Use the index of the next quotation mark instead of a fixed length
Dim targetstr As String = temp.Substring(0, temp.IndexOf(""""c))

Related

VBA check for ENTER character from clipboard

First of all I have little to no knowledge about VBA.. probably none at all. However I was asked to create a VBA program that paste text from clipboard in different cells. My text has the following format:
seminar: name of Seminar (in cell(1,1))
first name: participant's first name (in cell(1,2))
last name: participant's last name (in cell(1,3)) etc..
So far I was able to read the text from clipboard. Then I found the position of the ":" in order to paste only what is AFTER it in the cell.
At this point I thought to find the position of the RETURN character in order to know where the first line ends(ex. "name of Seminar") with this line of code which I found online:
end_str = InStr(str, vbCrLf) - 1
and with the Right (string, length) function to get the relative text.
This is not working. I think because there are not return character in the string variable that holds the data? I don't know.
My question is: Is it possible to check the RETURN character somehow or Is there a better way to create this program?
Thank you in advance.
An easy way would be to use the split function to get each line separately:
Suppose you have a function called ClipBoard_GetData that returns the text from ClipBoard, you could use something like this:
Dim lines() As String
lines = Split(ClipBoard_GetData, vbNewLine)
For Each Line In lines
' Parse each line to get whatever parts you want
Next
This should work fine.. and if you don't -already have a function that gets what's in the clipboard, you could refer to this link
Hope that helps :)
Most likely the Ascii code you're after is 10 (ie newline). So you could find the position of the newline like so:
i = Instr(str, Chr(10))
However, are you aware that you don't need to parse that clipboard text at all. You can write arrays directly into worksheet cells. So all you'd need to do is use the Split function. The procedure below will complete everything you need:
Public Sub PasteText(str As String)
Dim arr() As String
Dim cols As Integer
arr = Split(str, Chr(10))
cols = UBound(arr) + 1
Sheet1.Range("A1").Resize(, cols).Value = arr
End Sub

How to count the number of empty spaces in front of a String in VBA?

I have a YAML file which I am reading using VBA Excel plugin. I am getting a String of each line. I need to count the number of empty spaces in the front of the first line so that it will be a marker to find the next key. Let me know how to do that.
Option Explicit
Function empty_spaces(str_input)
empty_spaces = Len(str_input) - Len(LTrim(str_input))
End Function
harun24hr, your answer does not work with " My String " and similar.

Value of type '1-dimensional array of String' cannot be converted to 'String'. (BC30311)

I have this code which gives an error:
'declaration
Dim strFieldValues As String
'split
strFieldValues = strRecord.Split(",") 'field are separated by commas
Well, the error seems to be pretty self-explanatory to me. You've declared a variable of type String - i.e. it can hold a value of a single String reference:
Dim strFieldValues As String
You've then tried to assign a value to it returned from String.Split():
strFieldValues = strRecord.Split(",")
Now String.Split() returns a String array, not a single string value.
So you have two courses of action open to you:
Change strFieldValues to an array variable
Change the value you assign to it
My guess is that you want the first, but we don't know what you're trying to achieve. The simplest approach would be to combine declaration and initialization:
Dim strFieldValues = strRecord.Split(",")
You may also need to change the arguments to Split - I don't know how VB will sort out that call.
If all you want to do is just retrieve either side of the resulting string array, you could just invoke the left or right part like this:
strFieldValues = strRecord.Split(",")(0) ' Text to the left of the delimiter character
Or
strFieldValues = strRecord.Split(",")(1) ' Text to the right of the delimiter character
Of course, this assumes that the delimiter character does exist, so you should take the necessary precautions to ensure that you won't run into a runtime exception if said character is not found on the string you are splitting.

Extracting characters from an input string vb.net

Hey guys I'm stuck with this question. Please help.
I want to write a program that can extract alphabetical characters and special characters from an input string. An alphabetical character is any character from "a" to "z"(capital letters and numbers not included") a special character is any other character that is not alphanumerical.
Example:
string = hello//this-is-my-string#capetown
alphanumerical characters = hellothisismystringcapetown
special characters = //---#
Now my question is this:
How do I loop through all the characters?
(the for loop I'm using reads like this for x = 0 to strname.length)...is this correct?
How do I extract characters to a string?
How do I determine special characters?
any input is greatly appreciated.
Thank you very much for your time.
You could loop through each character as follows:
For Each _char As Char In strname
'Code here
Next
or
For x as integer = 0 to strname.length - 1
'Code here
Next
or you can use Regex to replace the values you do not need in your string (I think this may be faster but I am no expert) Take a look at: http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx
Edit
The replacement code will look something as follows although I am not so sure what the regular expression (variable called pattern currently only replacing digits) would be:
Dim pattern As String = "(\d+)?" 'You need to update the regular expression here
Dim input As String = "123//hello//this-is-my-string#capetown"
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, "")
Since you need to keep the values, you'll want to loop through your string. Keeping a list of characters as a result will come in handy since you can build a fresh string later. Then take advantage of a simple Regex test to determine where to place things. The psuedo code looks something like this.
Dim alphaChars As New List(Of String)
Dim specialChars As New List(Of String)
For Each _char As Char in testString
If Regex.IsMatch(_char, "[a-z]")) Then
alphaChars.Add(_char)
Else
specialChars.Add(_char)
End If
Next
Then If you need to dump your results into a full string, you can simply use
String.Join(String.Empty, alphaChars.ToArray())
Note that this code makes the assumption that ANYTHING else than a-z is considered a special character, so if needs be you can do a second regular expression in your else clause to test for you special characters in a similar manner. It really depends on how much control you have over the input.

VB.NET split string with quotation marks in it

Trying to split a line wherever "," appears (with the quotation marks). The problem is VB.NET uses " to start/end strings, so I tried using .Split(""",""") but that then splits it by " not ",".
Try something like this:
Dim TestToSplit As String = "Foo"",""Bar"
Dim Splitted() As String = TestToSplit.Split(New String() {""","""}, StringSplitOptions.None)
I just tested it and got an array with Foo And Bar. I hope this helps.
The Split function (the way you are using it) expects a Char. If you want to split on multiple characters you need to use a string array. (Seems to me another overload of a single string value would have been handy.)
This function splits a line of text and returns an array based on the delimiter you have specified. (Of course, you could make this more general purpose by passing in the separator array.)
Private Function SplitLine(ByVal lineOfText As String) As String()
Dim separator() As String = {""","""}
Dim result() As String
result = lineOfText.Split(separator, StringSplitOptions.None)
Return result
End Function
Another alternative I often find useful is this:
Regex.Split(textToSplit, """,""")
Lets you split on more complex criteria than an array of alternative separators.
To escape the "-character in VB.NET, use two: ""