For Example need to add a slash / in between USDYEN so it need to output USD/YEN.
Asked
Active
Viewed 536 times
-2
Jonathan Sanchez
- 7,316
- 1
- 24
- 20
2 Answers
2
- One way is to FIND and replace
USDforUSD/then it will outputUSD/YEN - However, how about if
USDis not found because it's another currency let's sayEUR/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