String question

  • Thread starter Thread starter Able
  • Start date Start date
A

Able

Dear friends

Probably a simple string question:

A string has two pair of the letter "Q" like this:
hfjhfdhfQtest1QfghghfhdfhQtest2Qghgh

How do I retrieve the string between the first pair and the second pair?

Regards Able
 
Dim myString as String = "hfjhfdhfQtest1QfghghfhdfhQtest2Qghgh"

Dim iLoc1 as Integer = myString.IndexOf("Q") + 1
Dim iLoc2 as Integer = myString.IndexOf("Q", iLoc1)

Dim s as String = myString.SubString(iLoc1, iLoc2 - iLoc1)
 
Hi Able,

That is not a simple one, because I even do not look to the regex, while it
is maybe posible with it,

I think I would do it with splits

Did not test it but something as (this is the string after the 2Q's and
before the third)
\\\\
dim myarray as string() = splits(thestring on Q)
dim myfield as string = myarray(3)
////

Cor
 
Able,
As Cor suggested, you can use RegEx to find the value:

One possibility is:

Const input As String = "hfjhfdhfQtest1QfghghfhdfhQtest2Qghgh"
Const pattern As String = "Q(?'g1'.+)Q(?'stuff'.+)Q(?'g2'.+)Q"

Dim ex As New System.Text.RegularExpressions.Regex(pattern)
Dim match As System.Text.RegularExpressions.Match

match = ex.Match(input)

Debug.WriteLine(match.Groups("g1"), "g1")
Debug.WriteLine(match.Groups("stuff"), "stuff")
Debug.WriteLine(match.Groups("g2"), "g2")

g1 is what is between the first two Q's, while g2 is what is between the
second two Q's. stuff is what is between the two pairs of Q's.

Note the .+ requires one or more characters between the Q's, change it to .*
if zero or more characters are allowed.

For info on .NET's implementation of RegEx see:
http://msdn.microsoft.com/library/d...l/cpconRegularExpressionsLanguageElements.asp

For a tutorial on RegEx see:
http://www.regular-expressions.info/

Hope this helps
Jay
 
Back
Top