-1
String text = "Thanx for purchasing this, Rs. 1000. Thanx for visiting";

I need double value from the above string. Length is not fixed of Rs. value (1000).

How to get this value?

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
  • 1
    Take a look here: http://stackoverflow.com/questions/4662215/how-to-extract-a-substring-using-regex You will need a different regex but that's the approach I'd go with – Stefano Zanini Mar 08 '17 at 09:26
  • take a look here http://stackoverflow.com/questions/25225475/getting-a-substring-from-a-string-after-a-particular-word – Younes Ouchala Mar 08 '17 at 09:31

3 Answers3

1

For this you need to use java regex

Here is sample code Regex pattern will change based on your requirement simple regex to get only double value "\\d+\\.\\d+"

String line = "Thanx for purchasing this, Rs. 1000. Thanx for visiting";
        String regex = "[+-]?\d*\.?\d+([eE][+-]?\d+)?";

        String[] str = line.split(regex);

Other wise you can try below code

public String drawDigitsFromString(String strValue){
            String str = strValue.trim();
            String digits="";
            for (int i = 0; i < str.length(); i++) {
                char chrs = str.charAt(i);              
                if (Character.isDigit(chrs))
                    digits = digits+chrs;
            }
            return digits;
        }

I think this will help you.

Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59
1

if there is no other number then After Rs. you can use

text.replaceAll("[^0-9]", "");

if there is some other number also, it will fail.

SAQ
  • 186
  • 3
  • 9
0

You can make an array of all the words and the use only the number like this:

var text = "Thanx for purchasing this, Rs. 1000. Thanx for visiting";

var array = text.split(" ");
var newText = array[5].replace(".","");

console.log(newText)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Zorken17
  • 1,896
  • 1
  • 10
  • 16