0

I would like to know if there is a simple approach to split units and quantities apart in a string notation, where the unit is optional.

entry examples: 10, 20kg, 14h, 5;

What would you use to split for example the 20kg into 20 and kg etc?

*edit: in my examples list I didn't include decimal values, but those are also possible. (0.1 or 1.25 euro)

Puddingboy
  • 127
  • 9
  • http://stackoverflow.com/questions/3370263/separate-integers-and-text-in-a-string maybe this? – Irtza.QC Jan 06 '16 at 08:24
  • 1
    Use a regexp such as `/\d+(\w+)?/`, suitably modified for the decimal value case. –  Jan 06 '16 at 08:50

2 Answers2

0
var string="10kg"// string="10kg"
var number=parseInt(string);//number=10
var unit=string.substr(parseInt(string).toString().length);//kg
Venkat
  • 2,549
  • 2
  • 28
  • 61
  • 1
    Would it not be logical to use the number var by referance instead of a second parseInt(string) ? – Puddingboy Jan 06 '16 at 08:46
  • Would this example still work if `parseInt()` were to be replaced with `parseFloat()`? – Puddingboy Jan 06 '16 at 08:48
  • 1
    var string="10.22kg"// string="10kg" var number=parseFloat(string);//number=10 var unit=string.substr(parseFloat(string).toString().length);//kg – Venkat Jan 06 '16 at 09:19
0

You could use regex to split your quantities...

.match(/(\D*)(\d+)(\D*)/)

...will split your text into an array of 4 elements, the first of which will contain the original string followed by the groups of prefix, numeric value, and the suffix.

Zero or more non-digits followed by one or more digits followed by zero or more non-digits.

Here is an example, check the console:

var input = ['20kg', '40m', '$10', '50 km', '20'], 
    result = [];

input.forEach(function(elem) {
  result.push(elem.match(/(\D*)(\d+)(\D*)/));
});
console.table(result);

You can account for the decimal places as well by changing the middle group to:

(\d+(\.\d+)?)

Digits followed by a dot and then digits if present.

Abhitalks
  • 27,721
  • 5
  • 58
  • 81
  • In my list a prefaced example doesn't exist ($), would you still suggest a regex? – Puddingboy Jan 06 '16 at 09:32
  • @Puddingboy: Then just remove the first group and your regex would become `(\d+(\.\d+)?)(\D*)`. Here is a demo fiddle with that -- https://jsfiddle.net/abhitalks/6j65q3ty/ – Abhitalks Jan 06 '16 at 09:36
  • would you say using a regex has strong benefits in this case? – Puddingboy Jan 06 '16 at 09:42
  • It will certainly make your life easier, otherwise you will have to somehow find the number-letter boundary and then split manually into two along with taking care of the edge cases like spaces, single or multiple digits and non-existent units etc. – Abhitalks Jan 06 '16 at 09:49
  • I purposefully never said what I'd use this for so I could see examples like yours. In my case I believe it's better not to use regex, because I'm not handling userinput or prefaced quantities. Regex helps checking for patterns, but it also decreases readabillity of code to some extend. Vankats example is more suited for this case. Otherwise I would've definitely used your approach. – Puddingboy Jan 06 '16 at 10:16
  • Is it not possible to exclude the original string from the array? – Puddingboy Jan 11 '16 at 11:19
  • @Puddingboy: I don't understand what you mean by excluding original string from array? – Abhitalks Jan 11 '16 at 11:39
  • "will contain the original string followed by the groups of prefix, numeric value, and the suffix" – Puddingboy Jan 11 '16 at 11:57
  • @Puddingboy: I think I included that in my answer! See the second paragraph. Also, did you check the console while running the snippet? The first element in the resulting array contains the original string. So all you need to do is to ignore the first element i.e. do not use index 0, if you don't want to exclude the original string. If using in a loop, start the index from 1 instead of 0. – Abhitalks Jan 11 '16 at 12:05
  • I was thinking, it may be a healthier practice to exclude the full(original) string from capture and not save it to memory, rather than ignoring it. I don't have enough experience to say anything about this matter tho. – Puddingboy Jan 11 '16 at 12:12