getstringbetween Error [solved]

Posted on

The term getstringbetween exists in JavaScripts. There are some errors related to getstringbetween. On the site named Stack Overflow, there is an error about getstringbetween reported by Ro Machet entitled “get all strings between 2 strings”.

     Public Function GetStringBetween(ByVal InputText As String, _

ByVal starttext As String, _

ByVal endtext As String)

Dim InTextStart As Long

Dim InTextEnd As Long

InTextStart = InStr(StartPosition, InputText, starttext, vbTextCompare) + Len(startext)

InTextEnd = InStr(InTextStart, InputText, endtext, vbTextCompare)

If InTextStart >= (StartPosition + Len(starttext)) And InTextEnd > InTextStart Then

       GetStringBetween = Mid$(InputText, InTextStart, InTextEnd – InTextStart)

Else

      GetStringBetween = “ERROR”

End If

End Function

Dim xa As String

Dim x As String = WebBrowser1.DocumentText

Usage:

Xa = GetStringBetween(x, TextBox1.Text, TextBox2.Text)

    MsgBox(xa)

The user has tried a lot of different methods to get all strings between 2 other strings with for each and so on. The site has more than 1 string with those 2 strings but they just get the first string between 2 strings.

As the answer to the question, Steve stated that the easiest way to write that code is probably through RegEx, but it could be overkill. Here is the same method to do the same thing using string.IndexOf

Public Function GetStringBetween(ByVal InputText As String, _

   ByVal starttext As String, _

   ByVal endtext As String)

       Dim startPos As Integer

       Dim endPos As Integer

      Dim IenStart As Integer

      startPos = InputText.IndexOf(startText, StringComparison.CurrentCultureIgnoreCase)

      if startPos >= 0 Then

lenStart = startPos + starttext.Length

endPos = InputText.IndexOf(endtext, lenstart, StringComparison.CurrentCultureIgnoreCase)

If endPost >= 0 Then

        Return InputText.Substring(lenStart, endPos – lenStart)

End If

End If

return “ERROR”

End Function

Ro Machet, then, interrupted that he needs all strings the code he uses works but it only gets the first string between the other two but the other ones in the text only gives him the first one. He added that everyone probably knows that for each string they need all the strings in the text that is between the 2 strings.

The second one, Onur Gazioglu, answered that regex may be used. For example: (word1). *(word2). Another one named Intellensense answered that in the old style right is depreciated as you know.

Public Function GetBetween(IStringStr As String, IBefore As String, IPas As String)

On Error Resume Next

Dim iString As String

iString = IStringStr

iString = Right(iString, Len(iString) = InStr(iString, IBefore) – Len(IBefore) + 1)

iString = Mid(iString, 1, InStr(iString, IPast) – 1)

GetBetween = iString

End Function

For those who are not familiar with StorckOverflow, it is the name of the platform that is widely used by those who come to learn, share their knowledge, team up, and build their career. The site is free so feel free to visit it without having to log in anytime you want.

In JavaScript world, the term “string” is used to call an object that represents a series of characters. There are two methods that can be used to create strings in JavaScript. The first one is by string literal and the second one is by string object (using new keyword).

The string literal is made using double quotes. The syntax of making string using string literal is:

Var stringname=”string value”;

Here is the example of making string literal.

<script> var str=”This is string literal”;

Document.write(str);

</script>

The syntax of making string object using new keyword is:

Var stringname=new String(“string literal”);

The new keyword is used to make instance of string here.

Here is the example of making string in JavaScript by new keyword.

<scipt>

Var stringanme=new String(“hello javascript string”);

Document.write(stringname);

</string>

For more information about getstringbetweeen error, it is better for you to visit the forum called StackOverflow and some other ones. Forums or communities might be really effective to look for the fix of the errors. Feel free to discuss with the members of the community about the error that you are currency facing.

JavaScript string methods:

  1. charAt(): This one provides the char value present at the certain index.
  2. charCodeAt(): This one provides the Unicode value of a character present at the certain index.
  3. concat(): This one is used to join two or more strings.
  4. indexOf(): This one returns the position of the first occurrence of a certain value in a string.
  5. lastIndexOf(): This one returns the position of the last occurrence of a certain value in a string.
  6. length(): This one returns the length of a string (number of characters).
  7. Repeat(): This one returns a new string with a particular number of copies of the string it was called on.
  8. replace(): This one searches a string for a certain value, or a regular expression, and returns a new string where the certain values are replaced.
  9. search(): This one searches a string for a certain value, and returns the position of the match.
  10. slice(): This one extract parts of a string and returns the extracted parts in a new one.
  11. split(): This one is used to split a string into an array of substrings and returns the new one. In case an empty string is used to separate it, the string is split between each character.
  12. startsWith(): This one determines whether a string starts with the characters of a certain string. It returns true if the string starts with the characters and false if not.
  13. Etc.

In Java API, string is considered as a special class and has a lot of special behaviors, which is not obvious to most programmers. If you want to master Java, mastering String class is the first step. Both string literal and string object gives you string object, but there is a little bit difference between them. When you make a string object using the new () operator, it always makes a new object in head memory. Meanwhile, if you make an object by using string literal syntax such as “Java”, it may come back as an existing object from the string pool, if it already exists. Otherwise, it will produce a new string object and put it in string pool to be used in the future.

Leave a Reply

Your email address will not be published. Required fields are marked *