-2

For Example need to add a slash / in between USDYEN so it need to output USD/YEN.

Jonathan Sanchez
  • 7,316
  • 1
  • 24
  • 20

2 Answers2

2
  1. One way is to FIND and replace USD for USD/ then it will output USD/YEN
  2. However, how about if USD is not found because it's another currency let's say EUR/YEN?

The trick here is to slice or cut the string at a particular position for example at the third character then add a slash / then add the same string but without the first 3 characters.

var str = 'USDYEN'
// add a / in between currencies

// var newStr = str.slice(3) // removes the first 3 chars
// var newStr = str.slice(0,3) // removes the last 3 chars

var newStr = str.slice(0,3) + ' / ' + str.slice(3) // removes the first 3 and adds the last 3

console.log(newStr)

Here the string is being removed

More info into how to use slice() https://www.w3schools.com/jsref/jsref_slice_string.asp

Jonathan Sanchez
  • 7,316
  • 1
  • 24
  • 20
0

const pair = Array.from('USDGBP')
pair.splice(3, 0, '/')
console.log(pair.join(''))
Ben Aston
  • 53,718
  • 65
  • 205
  • 331