Spliting ?!

OK. When a user enters a variable like say

var1 bdate = “24”;

         I want to split the variable into "2" and "4" so that I 

can add the two.

        Merry Christmas And A Happy New Year !

Wow, I don’t think you can, but if there is a way, I would definitely want to know.

I know there is the split method for strings, but you need to have something to split at, such as a space or a symbol.

This has nothing to split at. Unless you can tell it to split after the first digit.

But I don’t know much string manip, so I will leave that to some experts. Sbeener will probably answer this one.

myString = "12";
i = myString.substring(myString.length-2, myString.length-1)+" "+myString.substring(myString.length-1, myString.length);
trace(i);//returns 1 2

That rocks h88.

Where did you learn your string manipulation skills? A book? Any online tutorials?

I tried teaching myself it by experimentation, and I have learned a lot, but I still have much more to go.

I think this answer’s your question:

myString = "12";
a = myString.substring(myString.length-2, myString.length-1)
b = myString.substring(myString.length-1, myString.length);
c = parseInt(a)+parseInt(b)
trace(c)

Nice, h88, but it will only work with 2 numbers. You should try to write a function instead :slight_smile:

That is nice.

Ilyas: Yeah, a function to do any amount of numbers would rock, but in this case, they only asked for a day, which is only a max of two numbers.

By the way, there’s an excellent thread at bit-101 about that kind of things:http://www.bit-101.com/forum/viewtopic.php?t=531

Thanks Ilyas.

And Lost, sure, but try this:

myString = "8";
a = myString.substring(myString.length-2, myString.length-1)
b = myString.substring(myString.length-1, myString.length);
c = parseInt(a)+parseInt(b)
trace(c)

pom :stuck_out_tongue:

Ok, time for me to put my ghetto coding ability to the test…lol.

myString = "8";
if (myString.length == 2) {
	a = myString.substring(myString.length-2, myString.length-1);
	b = myString.substring(myString.length-1, myString.length);
	c = parseInt(a)+parseInt(b);
	trace(c);
} else {
	trace(myString);
}

All right all right, but it’s a bit dirty. A function is so much more elegant… :slight_smile:

Yeah… true.

But my ghetto method still works though :beam:

True, true. I need a hairsplitting smiley. Eilsoe? Where are you man?

LOL… Hairsplitting? As in the good kind or the bad kind?

Is there a good kind? :evil:

Yes. There is hairsplitting which means to get into every detail, then there is hairsplitting in the way you probably meant it :beam:

Knowing me, I am probably the only person that has ever heard it both ways…lol.

Wow, i can’t believe that it’s possible using charAt:

myString = "12";
myString = parseInt(myString.charAt(0))+parseInt(myString.charAt(1));
trace(myString);
//returns 3

[COLOR=silver][SIZE=1]bump[/SIZE][/COLOR]

Why don’t you believe it? :stuck_out_tongue: