Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to separate a string
#1
Solved: 11 Years, 3 Months, 4 Weeks ago
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
Reply
#2
Solved: 11 Years, 3 Months, 4 Weeks ago
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
Reply
#3
Solved: 11 Years, 3 Months, 4 Weeks ago
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
Reply
#4
Solved: 11 Years, 3 Months, 4 Weeks ago
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.
Reply
#5
Solved: 11 Years, 3 Months, 4 Weeks ago
(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.
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#6
Solved: 11 Years, 3 Months, 4 Weeks ago
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.
Reply
#7
Solved: 11 Years, 3 Months, 4 Weeks ago
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.
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#8
Solved: 11 Years, 3 Months, 4 Weeks ago Shy 
Exactly. No worries. Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to have a variable that has a string comma string .. shareq1310 5 5,080 11-09-2011, 03:33 PM
Last Post: parminderdhiman84
  string function to get a letter from a string ajay.r1982 3 7,591 06-30-2009, 08:58 PM
Last Post: Ankur

Forum Jump:


Users browsing this thread: 4 Guest(s)