split string in two on given index and return both parts

Javascript

Javascript Problem Overview


I have a string that I need to split on a given index and then return both parts, seperated by a comma. For example:

string: 8211 = 8,211
        98700 = 98,700

So I need to be able to split the string on any given index and then return both halves of the string. Built in methods seem to perform the split but only return one part of the split.

string.slice only return extracted part of the string. string.split only allows you to split on character not index string.substring does what I need but only returns the substring string.substr very similar - still only returns the substring

Javascript Solutions


Solution 1 - Javascript

Try this

function split_at_index(value, index)
{
 return value.substring(0, index) + "," + value.substring(index);
}

console.log(split_at_index('3123124', 2));

Solution 2 - Javascript

ES6 1-liner

// :: splitAt = (number, any[] | string) => [any[] | string, any[] | string]
const splitAt = (index, xs) => [xs.slice(0, index), xs.slice(index)]

console.log(
  splitAt(1, 'foo'), // ["f", "oo"]
  splitAt(2, [1, 2, 3, 4]) // [[1, 2], [3, 4]]
)
  

Solution 3 - Javascript

You can easily expand it to split on multiple indexes, and to take an array or string

const splitOn = (slicable, ...indices) =>
  [0, ...indices].map((n, i, m) => slicable.slice(n, m[i + 1]));

splitOn('foo', 1);
// ["f", "oo"]

splitOn([1, 2, 3, 4], 2);
// [[1, 2], [3, 4]]

splitOn('fooBAr', 1, 4);
//  ["f", "ooB", "Ar"]

lodash issue tracker: https://github.com/lodash/lodash/issues/3014

Typescript:

const splitOn: {
  <T = string>(s: T, ...i: number[]): T[];
  <T extends any[]>(s: T, ...i: number[]): T[];
} = <T>(slicable: string | T[], ...indices: number[]) =>
  [0, ...indices].map((n, i, m) => slicable.slice(n, m[i + 1]));

Solution 4 - Javascript

You can also use number formatter JS available at

https://code.google.com/p/javascript-number-formatter/

Format options

http://jsfiddle.net/chauhangs/hUE3h/

  format("##,###.", 98700)
  format("#,###.", 8211)

Solution 5 - Javascript

Something like this?...

 function stringConverter(varString, varCommaPosition)
 {
   var stringArray = varString.split("");
   var outputString = '';
   for(var i=0;i<stringArray.length;i++)
   {
     if(i == varCommaPosition)
      {
        outputString = outputString + ',';
      }
   
     outputString = outputString + stringArray[i];
   }  

   return outputString;
 }

Solution 6 - Javascript

You can also do it like this.
https://jsfiddle.net/Devashish2910/8hbosLj3/1/#&togetherjs=iugeGcColp

var str, result;
str = prompt("Enter Any Number");

var valueSplit = function (value, length) {
    if (length < 7) {
        var index = length - 3;
        return str.slice(0, index) + ',' + str.slice(index);
    }
    else if (length < 10 && length > 6) {
        var index1, index2;
        index1 = length - 6;
        index2 = length - 3;
        return str.slice(0,index1) + "," + str.slice(index1,index2) + "," + str.slice(index2);
    }
}

result = valueSplit(str, str.length);
alert(result);

Solution 7 - Javascript

If code elegance ranks higher than the performance hit of regex, then

'1234567'.match(/^(.*)(.{3})/).slice(1).join(',')
=> "1234,567"

There's a lot of room to further modify the regex to be more precise.

If join() doesn't work then you might need to use map with a closure, at which point the other answers here may be less bytes and line noise.

Solution 8 - Javascript

function splitText(value, index) {
  if (value.length < index) {return value;} 
  return [value.substring(0, index)].concat(splitText(value.substring(index), index));
}
console.log(splitText('this is a testing peace of text',10));
// ["this is a ", "testing pe", "ace of tex", "t"] 

For those who want to split a text into array using the index.

Solution 9 - Javascript

If you want a really hacky one-liner using regular expressions and interpolated strings...

const splitString = (value, idx) => value.split(new RegExp(`(?<=^.{${idx}})`));
console.log(splitString('abcdefgh', 5));

This code says split the string by replacing the value returned in the regex. The regex returns a position, not a character, so we don't lose in characters in the initial string. The way it does this is finding the position, via a look-behind and the ^ anchor, where there were index characters from the start of the string.


To solve your proposed problem of adding commas every third position from the end, the regex would be slightly different and we'd use replace rather than split.

const values = [ 8211, 98700, 1234567890 ];
const addCommas = (value, idx) => value.replace(new RegExp(`(?=(.{${idx}})+$)`, 'g'), ',');

console.log(values.map(v => addCommas(v.toString(), 3)));

Here we find the idxth position from the end by using a look-ahead and the $ anchor, but we also capture any number of those sets of positions from the end. Then we replace the position with a comma. We use the g flag (global) so it replaces every occurrence, not just the first found.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMike RifginView Question on Stackoverflow
Solution 1 - JavascriptChamika SandamalView Answer on Stackoverflow
Solution 2 - JavascriptAlan R. SoaresView Answer on Stackoverflow
Solution 3 - Javascriptlonewarrior556View Answer on Stackoverflow
Solution 4 - JavascriptNullPointerExceptionView Answer on Stackoverflow
Solution 5 - JavascriptNathanView Answer on Stackoverflow
Solution 6 - Javascriptdevashish-patelView Answer on Stackoverflow
Solution 7 - Javascripti336_View Answer on Stackoverflow
Solution 8 - JavascriptvcygniView Answer on Stackoverflow
Solution 9 - Javascriptdx_over_dtView Answer on Stackoverflow