-1

In JavaScript, I have strings like:

"US$ 43,22"
"$ 2,44"

And I need to find the first space and cut the left content to have something like this:

"43,22"
"2,44"

Anyone knows how to do it? The point here is find the space.

Thank's very much in advance!

EDIT:

If I do:

console.log(retrievedPrice); //Have "US$ 35,88" 

let priceWithoutCurrency = retrievedPrice.substr(retrievedPrice.indexOf(' ') + 1);
console.log(priceWithoutCurrency); //I have the same: "US$ 35,88"

Why?

matisetorm
  • 857
  • 8
  • 21
Ivan Lencina
  • 1,787
  • 1
  • 14
  • 25

4 Answers4

3

const s = "US$ 43,22";

const r = s.substr(s.indexOf(' ') + 1);

console.log(r)

+1 is needed to cut the space itself.

Egor Stambakio
  • 17,836
  • 5
  • 33
  • 35
  • @IvanLencina here is your code: https://jsfiddle.net/wostex/wk4frb98/ it works as intended, you get the subsctracted number. – Egor Stambakio May 12 '17 at 19:48
2

See below code snippet

var string = "US$ 43,22";
console.log( string.substr(string.indexOf(" ")+1))

Update:

 var string = "US$ 43,22";
 var result = string.substr(string.indexOf(" ")+1);
 console.log(result);

let retrievedPrice = "US$ 43,22";
let priceWithoutCurrency = retrievedPrice.substr(retrievedPrice.indexOf(' ') + 1);
console.log(priceWithoutCurrency);
sumit chauhan
  • 1,270
  • 1
  • 13
  • 18
  • It includes the space in the result. :) I think you want `indexOf(" ") + 1` – Aaron Beall May 12 '17 at 19:08
  • If I do: `console.log(retrievedPrice); //Have "US$ 35,88"` `let priceWithoutCurrency = retrievedPrice.substr(retrievedPrice.indexOf(' ') + 1); console.log(priceWithoutCurrency); //I have the same: "US$ 35,88"` Why? – Ivan Lencina May 12 '17 at 19:40
2

const x = "US$ 43,22";
const result = x.split(' ')[1];
console.log(result);
sesamechicken
  • 1,928
  • 14
  • 19
0

I see it's only a space, right? Try this

var myVal = "US$ 43,22";
myVal = myVal.replace("US$ ", "");
Felipe Morais
  • 155
  • 2
  • 12