0

Basically, I have a string of unknown numbers (determined by user input + my own math), and I need to split this string into 3 parts.

For example, I may have "1273498". I need to split it at the two characters, and the 3rd and 4th from the RIGHT, like so:
127
34
98

Another example: 1234567890 would need to be:
123456
78
90

Currently, I am accomplishing it this way:

// get first input box value
var depositgold = document.getElementById('v-gold').value;
// set it to 0 if it's empty
if(depositgold == null || depositgold == '')
    depositgold = 0;

// second input box value
var depositsilver = document.getElementById('v-silver').value;
if(depositsilver == null || depositsilver == '')
    depositsilver = 0;

// third input box value
var depositcopper = document.getElementById('v-copper').value;
if(depositcopper == null || depositcopper == '')
    depositcopper = 0;

// combine the 3 input box values (adding dec to make split easier)
var depositnums = depositgold + '.' + depositsilver + depositcopper;

// do some math on our new value, then split it at out dec
var deposit12 = (0.15 * depositnums).toFixed(4).split(".");
// split the last part of the above split into 4 characters
var result12 = deposit12[1].split("", 3);
// keep the first part of out dec split
var deposit12gold = deposit12[0];
// combine the second part split results into paired numbers
var deposit12silver = result12[0] + result12[1];
var deposit12copper = result12[2] + result12[3];

// repeat the above process
var deposit24 = (0.30 * depositnums).toFixed(4).split(".");
var result24 = deposit24[1].split("", 3);
var deposit24gold = deposit24[0];
var deposit24silver = result24[0] + result24[1];
var deposit24copper = result24[2] + result24[3];

var deposit48 = (0.60 * depositnums).toFixed(4).split(".");
var result48 = deposit48[1].split("", 3);
var deposit48gold = deposit48[0];
var deposit48silver = result48[0] + result48[1];
var deposit48copper = result48[2] + result48[3];

I know there must be a much better (and more sane) way of accomplishing the above - I need to do it several more times for this project, and I'm certainly not looking forward to continuing to do it this way.

I am new to JS and programming, so laugh away, just try not to laugh too hard ;)

1
  • Sorry, I meant to say I need to split it at the LAST two characters, and the two characters before the last two. Commented Mar 25, 2011 at 3:14

7 Answers 7

5

Try something along these lines:

var str = "123412341";
var matches = str.match(/(.+?)?(.{2})?(.{2})?$/);

// matches[1] = 12341
// matches[2] = 23
// matches[3] = 41

You may want to modify the RegEx depending on your input, currently all groups are optional.

Sign up to request clarification or add additional context in comments.

Comments

4

Just use the substring method

var number = 123456789;
var one = number.substring( 0, number.length - 4 );
var two = number.substring( number.length -4, number.length - 2);
var three = number.substring( number.length - 2 );

Comments

4

Use the substr() method for this:

var L = mystring.length
var part1 = mystring.substr(0,L-4);
var part2 = mystring.substr(L-4,2);
var part3 = mystring.substr(L-2,2);

Comments

3
('1234'+'56'+'78').match(/(\d*)(\d\d)(\d\d)/)
["12345678", "1234", "56", "78"]

Comments

1
var number = 1234567890;

number = number.toString();

var a = number.substr(0, number.length - 4),
    b = number.substr(-2),
    c = number.substr(number.length - 4, 2);

console.log(a, b, c);

jsFiddle.

Output

123456 90 78

Comments

1

here is a function i made:

/**
 * @param num A number to split like 382203849238
 * @return Returns an array of size 3, where index 0 = 38220384, index 1 = 92, index 2 = 38 based on the example above
*/
function splitNumbers(num) {
    var num = (typeof num == 'string' || typeof num == 'String') ? parseInt(num) : num,
        rem = num % 10000;
    return [Math.floor(num / 10000), Math.floor(rem / 100), rem % 100];    
}

Comments

1
// Function to parse code in 3 parts
function parse_code_Split (code)
{
    var len = code.length;
    var divisor = (len / 3) >> 0; // get whole number
    console.log(divisor);
    var stringReg = "";
    var regexp = ".{1,"  + divisor + "}";
    try{
        stringReg = new RegExp(regexp,"g");
    }catch(Error){
        window.console.log(Error.message);
    }

    codeSplit = code.match(stringReg);

    // window.console.log(" codeSplit[0] " + codeSplit[0]);
    // window.console.log(" codeSplit[1] " + codeSplit[1]);
    // window.console.log(" codeSplit[2] " + codeSplit[2]);
    // window.console.log(" codeSplit[3] " + codeSplit[3]); // remainder

    return codeSplit;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.