Need assistance extracting dynamic text between curly braces - vb.net

"keys": [{"key": "da0a1c781f5647e1b32b517d9a9afdfc:4f216ee9d53e1bf7767b6fcf140d799f"}]}
This is the code that is generated by the application I'm trying to finish. I'm trying to figure out how to take the key from between the quotation marks and have it displayed in another text box in the application.
Can anyone assist with this?
I was able to find this code:
Public Function extract_value(str As String) As String
Dim openPos As Integer
Dim closePos As Integer
Dim midBit As String
openPos = Instr(str, "{")
closePos = Instr(str, "}")
midBit = mid(str, openPos + 1, closePos - openPos - 1)
If openPos <> 0 And Len(midBit) > 0 Then
extract_value = midBit
Else
extract_value = "F"
End If
End Function
But now I can't append this to my text box once I click the button. Can anyone help?

Related

Parse text vb.net to strings

Lets say i have a string which goes like
<image1>C:\images\dropbox\lister\4844-001.jpg</image1><image2>C:\images\dropbox\lister\4844-002.jpg</image2><image3>C:\images\dropbox\lister\4844-003.jpg</image3><image4>C:\images\dropbox\lister\4844-004.jpg</image4>
It contains from Image1 to ImageN
I need to check through each image path and check if the image on that path really exist.
How to loop through that ?
I think that finding the index of word and and pulling all text between is the slowest way.
Can you suggest anything beside that ?
Private Function Word(source As String, start As String, ends As String) As String
Dim sSource As String = source
Dim sDelimStart As String = start
Dim sDelimEnd As String = ends
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart)
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd)
If nIndexStart > -1 AndAlso nIndexEnd > -1 Then
Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
Return res
Else
Return ""
End If
End Function
In this I converted your string to a XElement, then iterated each of the imageN nodes. This should work so long as your input string is as you specified.
Dim s As String
s = "<image1>C:\images\dropbox\lister\4844-001.jpg</image1><image2>C:\images\dropbox\lister\4844-002.jpg</image2><image3>C:\images\dropbox\lister\4844-003.jpg</image3><image4>C:\images\dropbox\lister\4844-004.jpg</image4>"
s = "<FOO>" & s
s = s & "</FOO>"
Dim fooXE As XElement = XElement.Parse(s)
For Each el As XElement In fooXE.Elements
' Debug.WriteLine(el.Value)
If IO.File.Exists(el.Value) Then
'the path exists
End If
Next

select random from target string VB.NET

how could i make that
Source_Source.Text (richtextbox) :
src="test.com" akalsjdjalksdv src="another.com" asdbnmslkbjcxv
asdas as danms d amsn asdasd src="cold.com"asdas as dasd amnbs dma d sdf kjhds f src="find.com" asd kja sdasjhk d asdsrc="other.com" a jksdh asksjd hasdjh src="found.com"
what if i wanna get random src=" ", for example, if i clicked butten will show message = src="test.com" , another time if i clicked button will show another random, for example src="another.com" ,
my currently code only select first string which = src="test.com" , and i wanna select randoms src="[random test / cold / other / found / find]"
for example my next click can show message for src="find.com" that for example.
my code :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sSource As String = Source_Source.Text 'String that is being searched
Dim sDelimStart As String = Search_1.Text 'First delimiting word
Dim sDelimEnd As String = Search_2.Text 'Second delimiting word
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd, nIndexStart + sDelimStart.Length + 1) 'Find the first occurrence of f2
If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
MessageBox.Show(res) 'Display
Else
MessageBox.Show("One or both of the delimiting words were not found!")
End If
End Sub
Private Function RndUrl() As String
Dim UrlStr() As String = {"whatever.com", "whatever2.com"}
Return Rand.Next(0, UrlStr.Length) '"Rand" Declared at Class scope as Random
End Function
Usage:
TextBox1.Text = "src=""" & RndUrl() & ""
If I understand your question correctly, you're looking for a certain phrase in a String? If so look into String.Contains() method.
For example:
If Source_Source.Text.Contains(Search_1.Text) Then
' Do you're logic here if found
Source_Source.Text.SubString(0, 5) ' Or where ever that part of the string is that you want.
End If

VBA Split a String in two parts

I'm trying to obtain two codes from this string: "HL PNX-70[15200]"
But with this code, I obtain two times the same output: "HL PNX-70". So, the code is not properly done.
How to obtain the output '15200' from the above mentioned String?
Code:
Private Sub Comando221_Click()
MsgBox (Right(Split("HL PNX-70[15200]", "[")(0), 50))
MsgBox (Left(Split("HL PNX-70[15200]", "[")(0), 50))
End Sub
Are you looking for this ?
Sub Test()
MsgBox Split("HL PNX-70[15200]", "[")(0)
MsgBox Replace(Split("HL PNX-70[15200]", "[")(1), "]", "")
End Sub
Split returns a zero-based array so you are interested in the second element, index 1. Both lines of your code are extracting "HL PNX-70" and the leftmost and rightmost 50 characters will clearly be the same.
This code illustrates two ways of extracting the desired string for your specific example, but it is not necessarily ironclad if you are working with multiple different types of string. You could also use Instr, as per the other answer, or look at regular expressions if you need more complex pattern matching.
Sub y()
Dim s As String, v
s = "HL PNX-70[15200]"
v = Split(s, "[")
Debug.Print v(0) 'HL PNX-70
Debug.Print v(1) '15200]
MsgBox Left(v(1), Len(v(1)) - 1) '15200
v = Split(v(1), "]")
MsgBox v(0) '15200
End Sub
You could try:
Option Explicit
Sub Test()
Dim str As String, Result As String
Dim Start_Point As Long, No_Characters As Long
str = "HL PNX-70[15200]"
Start_Point = InStr(str, "[") + 1
No_Characters = Len(str) - Start_Point
Result = Mid(str, Start_Point, No_Characters)
Debug.Print Result
End Sub
Here is your code
Dim text, text1, text2 As String
text = "HL PNX-70[15200]"
text1 = Break_String(CStr(text), 0)
text2 = Break_String1(Break_String(CStr(text), 1))
Function Break_String(a As String, pos As Integer) As String
Dim WrdArray() As String
WrdArray() = Split(a, "[")
Break_String = WrdArray(pos)
End Function
Function Break_String1(a As String) As String
Dim WrdArray() As String
WrdArray() = Split(a, "]")
Break_String1 = WrdArray(0)
End Function

copy fixed content between two delimeter in visual basic

i want to copy or transfer the fixed words between two delimiting symbols from one textbox to another.i was successful in transferring the single word from one text box to another but when i wanted to transfer two or more word,it showed the error.the transferring of the word happens when the button is pressed.
My code for transferring single word between the delimiting symbol is:-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sSource As String = TextBox1.Text 'String that is being searched
Dim sDelimStart As String = "FirstName=" 'First delimiting word
Dim sDelimEnd As String = "." 'Second delimiting word
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2
If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
TextBox2.Text = res 'Display
End If
End Sub
Above code works properly but when when i wanted to search and transfer two words it showed the error as ArgumentException was Unhandled and Argument 'Length' must be greater or equal to zero.My error containg code is:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sSource As String = TextBox1.Text 'String that is being searched
Dim sSource1 As String = TextBox1.Text
Dim sDelimStart As String = "FirstName=" 'First delimiting word
Dim sDelimStart1 As String = "LastName="
Dim sDelimEnd As String = "." 'Second delimiting word
Dim sDelimEnd1 As String = "."
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexStart1 As Integer = sSource1.IndexOf(sDelimStart1)
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2
Dim nIndexEnd1 As Integer = sSource1.IndexOf(sDelimEnd1)
If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
TextBox2.Text = res 'Display
End If
If nIndexStart1 > -1 AndAlso nIndexEnd1 > -1 Then '-1 means the word was not found.
Dim res1 As String = Strings.Mid(sSource1, nIndexStart1 + sDelimStart1 + 1, nIndexEnd1 - nIndexStart1 - sDelimStart1.Length) 'Crop the text between
TextBox3.Text = res1 'Display
End If
End Sub
I think the error is due to the repeated use of Length variable in 'if' statement but i don't know how to fix that.The link to the snapshot of my desired output is:http://tinypic.com/r/1zy7iwz/8 i was only able to transfer the word harry but not the porter.Any help is much appreciated.
To start, I think you need to add .length after your sDelimStart and sDelimStart1. Currently, you're trying to pass a String + Integer as an Integer.
Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.length + 1, nIndexEnd - nIndexStart - sDelimStart.Length)
And in your second if statement:
Dim res1 As String = Strings.Mid(sSource1, nIndexStart1 + sDelimStart1.length + 1, nIndexEnd1 - nIndexStart1 - sDelimStart1.Length)
However, I would recommend using the String.Split function:
https://msdn.microsoft.com/en-us/library/b873y76a%28v=vs.110%29.aspx
EDIT: Why it is displaying same thing in both.
I think this is because you need to find the last index of the delimiters.
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexStart1 As Integer = sSource1.LastIndexOf(sDelimStart1)
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2
Dim nIndexEnd1 As Integer = sSource1.LastIndexOf(sDelimEnd1)

VB code to remove special character in a column

I am having a column which contains integer values with two special character "," and "_". I am trying to remove these character for example 1,10_2,2_3,3 should be like 1102233. Thanks in advance for your suggestions.
this function isn't foolproof but it is a good start.
Function trim(aStringToTrim As String, aElementToTrinm() As Variant) As String
Dim elementToTrim As Integer
Dim IndexInString As Integer
For elementToTrim = LBound(aElementToTrinm) To UBound(aElementToTrinm)
IndexInString = InStr(aStringToTrim, aElementToTrinm(elementToTrim))
Do While IndexInString > 0
aStringToTrim = Left(aStringToTrim, IndexInString - 1) & Right(aStringToTrim, Len(aStringToTrim) - IndexInString - Len(aElementToTrinm(elementToTrim)) + 1)
IndexInString = InStr(aStringToTrim, aElementToTrinm(elementToTrim))
Loop
Next
End Function
It can be use like this:
Sub main()
Dim myString As String
Dim caracterstoRemove As Variant
caracterstoRemove = Array(",", ".")
myString = "This, is. a, string, with. caracters to remove."
myString = trim(myString, caracterstoRemove)
End Sub