Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get only numeric values from text
#1
Solved: 10 Years, 9 Months ago
There is a message on a webpage like Rs.4500.00/- (here there is no space between Rs and value and /- and it is a dynamic value, it may displays big number like RS340067.23/-)

How to retrieve only value without Rs. and /-


Thanks inadvance

Sandeep
Reply
#2
Solved: 10 Years, 9 Months ago
Hi Sandeep,

Try the following code snippet:

Store the value (Rs.4500.00/- or anything) in a variable, say myVar

Quote:myVar = Split(myVar,"Rs.")
myVar1 = myVar(1) \\ it will give you 4500.00/-
myVar1 = Split(myVar1,"/-")
myVar2 = myVar1(0) \\ it will give you only 4500.00
In this way you are removing Rs. and /- from any value and it will work for all values provided the format doesn't change

Reply
#3
Solved: 10 Years, 9 Months ago
Same stuff in a better way,
Numric = Split(Split(myVar,"Rs.")(1),"/-")(0)
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
#4
Solved: 10 Years, 9 Months ago
Instead of using Split, you can also use Replace function.

Eg.
Code:
myVar="Rs.4500/-"
myVar=Replace(myVar,"Rs.","")
myVar=Replace(myVar,"/-","")
msgbox myVar
You can get 4500, by replacing unwanted characters by empty string "".
Reply
#5
Solved: 10 Years, 9 Months ago
Depends on where you want to use. In the above scenario replace is cumbersome because you want to replace multiple values. while split would simply tear the rest and give you what you need at one shot.Moreover, when you split you can reuse the other part( which would be in the array) while replace, just replaces the data.
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: 10 Years, 9 Months ago
you could also use regular expressions to represent your string. The regular expression below says to look for the string "RS", followed by a decimal characters (.), followed by zero or more numeric digits followed by zero or more decimal characters, followed by zero or more digits. The parentheses enclose the part of the regular expression that you want to keep (it stores it in Matches(0).SubMatches(0) in this example). The resulting string is everything but "RS" at the beginning of the string and the trailing "/-" string.


Code:
Dim regEx, Matches, returnString   ' Create variable.
   Set regEx = New RegExp   ' Create a regular expression.
   regEx.Pattern = "RS\.*(\d+\.*\d*)/-" ' Set pattern
   regEx.IgnoreCase = True   ' Set case insensitivity.
   regEx.Global = True   ' Set global applicability.

    strng="Rs.4500.00/-"
    'strng="RS340067.23/-"

   Set Matches = regEx.Execute(strng)   ' Execute search.
   If Matches.Count=1 Then
       If Matches(0).SubMatches.count=1 Then
       End If
       returnString=Matches(0).Submatches(0)
   End If

msgbox returnString

set regEx=nothing
Reply
#7
Solved: 10 Years, 9 Months ago
The lines of code matter a big deal for a efficient execution.
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: 10 Years, 9 Months ago
Lines of code are not the only measure of a solution. Accuracy, flexibility, ease of understanding are important as well. Regular expressions are great for dealing with ambiguity in data. Regular expressions are quite flexible and can match many variations on a data "theme". However, regular expressions often do not do as well in the "ease of understanding" area as other solutions, and, as you pointed out, they may not be as efficient as other solutions.

We run our test sets overnight, unattended. For us, speed of execution is secondary. What does the poster plan to do with the data once it is parsed? Write it to another file? That is much, much slower than the regular expression and so the regular expression is a smaller percentage of the total execution time.

I am not arguing that a regular expression is the best or only solution here, but it is a good solution if the data format is varied as the examples suggest. If the data is NOT as varied as the examples suggest (i.e., there are typos in the example), then a regular expression would be overkill - like bringing in the USS Enterprise to kill a mouse.

It does provide more information to the poster about ways to tackle the problem and, hopefully, expands the poster's knowledge of QTP a little.
Reply
#9
Solved: 10 Years, 9 Months ago
Knowledge shating wise good thought, though precision in answer is of utmost concern. The above problem is simply achieved by split a one liner then a whole junk of regularly expressioned syntax. Not necessary. Maybe helpful for a different post.
Oh.. I almost forgot, automation was brought forth to cut down the manual execution speed and if you are running scripts overnight not being bothered about the speed then it is time to think what you can improve. Not every company would want a setup like yours.
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
#10
Solved: 10 Years, 9 Months ago
Hi Friends,

See.. Forum is basically for sharing knowledge.. People want to get different kind of answers, to improve themselves. I learned from regular expression post (Thanks for that, really a good work), as i dont have much exposure to it.

We all are already aware of more LOC need more execution time, there is nothing to say about this explicitly.

Instead of climbing to the single solution, be prepare to experiment different solutions. Thats a good way of learning..
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to Validate Text filed is blank after clicking on the Text box balak89 3 4,543 09-13-2015, 12:06 AM
Last Post: ADITI1992
  Validate Numeric value is displaying expected format kalaivanan123 1 3,385 09-23-2013, 08:45 PM
Last Post: ravi.gajul
  I am trying to extract text from a webelement but it is generating blank text scenari excellentpawan 2 4,288 08-13-2013, 08:37 PM
Last Post: excellentpawan
  how to pass a numeric value into webedit silpavinod 3 3,665 10-15-2012, 01:57 PM
Last Post: silpavinod
  Need help on numeric value display AutomationTester2011 3 3,030 07-19-2011, 05:40 PM
Last Post: rajpes

Forum Jump:


Users browsing this thread: 1 Guest(s)