Micro Focus QTP (UFT) Forums
How to separate a string - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: VB Scripting/Descriptive Programming (https://www.learnqtp.com/forums/Forum-VB-Scripting-Descriptive-Programming)
+--- Thread: How to separate a string (/Thread-How-to-separate-a-string)



How to separate a string - linhke - 05-28-2010

Hi all,

I have a string as "Ref1234", i want to get the character string, not number string. So can anybody show me the solution?

Thanks and BRs,
linhke


RE: How to separate a string - guin.anirban - 05-28-2010

Is you requirement to get only "Ref"?
If your requirement is to get only "Ref" then you can try the following:

Code:
str = "Ref1234"
intLen = Len(str)
getstr = ""
For itemCnt = 1 To intLen
    char = Mid(str, itemCnt, 1)
    If (IsNumeric(char)) Then
    Else
            getstr = getstr + char
    End If
Next

MsgBox getstr



RE: How to separate a string - sreekanth chilam - 05-28-2010

Hi Linhke,

Check with the below example, it would give only the charcter string by ignoring Numericals.
Code:
Dim Str,temp,i,x
   Str="Ref1234"
   temp=""
   For i=1 to len(Str)
         x=mid(Str,i,1)
         If isnumeric(x)<>true Then
    temp=temp&x
         End If
   Next
   Str=temp
   msgbox Str



RE: How to separate a string - jsknight1969 - 06-04-2010

Code:
Dim re, teststring, testresults, showresults
teststring = "Ref123"
Set re = new regexp
re.Pattern = "[aA-zZ]"
re.Global = true
re.IgnoreCase = true
set testresults = re.Execute(teststring)
For each match in testresults
    showresults = showresults + match.value
Next
msgbox showresults

This works as well using regular expressions. The "execute" method returns a collection of all characters in the test string that match the regex pattern, in this case alphas between A and Z upper or lower case. You must then loop through the collection and concatenate each match to a results string.


RE: How to separate a string - basanth27 - 06-07-2010

(06-04-2010, 01:35 AM)jsknight1969 Wrote:
Code:
Dim re, teststring, testresults, showresults
teststring = "Ref123"
Set re = new regexp
re.Pattern = "[aA-zZ]"
re.Global = true
re.IgnoreCase = true
set testresults = re.Execute(teststring)
For each match in testresults
    showresults = showresults + match.value
Next
msgbox showresults

This works as well using regular expressions. The "execute" method returns a collection of all characters in the test string that match the regex pattern, in this case alphas between A and Z upper or lower case. You must then loop through the collection and concatenate each match to a results string.

JskNight -
Although powerful a function, I would suggest that in the context of QTP this would hamper the test execution if used at several places. The reason being, with the already available string functions supplied by HP dev it seems that the structural programming to link between them works faster than our own. I am just trying to say we minimize the use of loops and other complex regular expressions because QTP's internal engine is weak when it comes to analytical calculations inturn causing load. Unlike perl, phython and other languauges though primitive have a very good engine to perform these operations.

Just a thought.


RE: How to separate a string - jsknight1969 - 06-07-2010

Thanks basanth27 and I agree. Using loops does slow down the scripting language, so you need to be wary.

At the same point to test every character in a string to find numbers is a loop (for next). I found that using RE is most beneficial in a function that you can then reuse whenever necessary by just changing the expression. You only need the loop if you want to return the underlying matching string, which in this example is wanted. Just returning a boolean doesn't need the loop at all and performs extremely quick in QTP.

Quote:available string functions supplied by HP dev

I am not aware of any specific HP string testing. Looks like all VBScript string functions to me of which Regular Expression is built in too. Do you have an example of HP functions?

As with all things in development coding, there are many ways to accomplish the tasks at hand. I have used both methods in the past and have only recently become a RE convert. This is just my way to do the requested job and provide a reusable function because I hate rewriting code over and over.


RE: How to separate a string - basanth27 - 06-08-2010

Buddy -
It was just a thought Smile . It hardly matters how we do it but all that the investor looks for is wether it is done.

In this case i agree with you to the fullest extent.


RE: How to separate a string - jsknight1969 - 06-09-2010

Exactly. No worries. Smile