1

I have string looking like this:

01
02
03
99

I'd like to parse these to make them into strings like:

 1. 2. 3. 99. etc. 

The numbers are a maximum of 2 characters. Also I have to parse some more numbers later in the source string so I would like to learn the substring equivalent in javascript. Can someone give me advice on how I can do. Previously I had been doing it in C# with the following:

int.Parse(RowKey.Substring(0, 2)).ToString() + "."

Thanks

6 Answers 6

1

Why, parseInt of course.

// Add 2 until end of string
var originalA = "01020399";
for (var i = 0; i < originalA.length; i += 2)
{
  document.write(parseInt(originalA.substr(i, 2), 10) + ". ");
}

// Split on carriage returns
var originalB = "01\n02\n03\n99";
var strArrayB = originalB.split("\n");
for (var i = 0; i < strArrayB.length; i++)
{
  document.write(parseInt(strArrayB[i], 10) + ". ");
}

// Replace the leading zero with regular expressions
var originalC = "01\n02\n03\n99";
var strArrayC = originalC.split("\n");
var regExpC = /^0/;
for (var i = 0; i < strArrayC.length; i++)
{
  document.write(strArrayC[i].replace(regExpC, "") + ". ");
}

The other notes are that JavaScript is weakly typed, so "a" + 1 returns "a1". Additionally, for substrings you can choose between substring(start, end) and substr(start, length). If you're just trying to pull a single character, "abcdefg"[2] will return "c" (zero-based index, so 2 means the third character). You usually won't have to worry about type-casting when it comes to simple numbers or letters.

http://jsfiddle.net/mbwt4/3/

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

5 Comments

But I need a string as I will add the "." character. How can I convert the above to a string?
JavaScript is weakly typed, so you can just use + to combine a string and a number. If you're uncomfortable with that, substitute parseInt(test.substr(i, 2)).toString() above.
Thanks Bryan. I didn't realize it was weekly typed. Works great. I'll accept.
You need to have the base 10 on parseInt or the leading zero is going to cause problems. parseInt( num, 10);
@epascarello True, forgot about that. I've updated the code to reflect that. parseInt evaluates strings beginning with "0" in octal, so parseInt("07") returns 7 but parseInt("08") and parseInt("09") return 0.
1

use parseInt function.

parseInt(09) //this will give you 9

var myString = parseInt("09").toString()+". "+parseInt("08").toString();

4 Comments

Later on I will need to get the value of the 4th and 5th characters in my string and do the same. Is there a substring kind of function in javascript ?
@TonyG - yes there is a substring in javascript.
Thank you but I get this error message: function toString() { [native code] }
@TonyG- oops sorry, I missed braces ()
0
string = '01\n02\n03\n99';
array = string.split('\n');
string2 = '';
for (i = 0; i < array.length; i++) {
    array[i] = parseInt(array[i]);
    string2 += array[i] + '. ';
}
document.write(string2);

Comments

0
var number = parseFloat('0099');

2 Comments

Will this give me something looking like "99." from the input?
Try: var number = parseFloat('0099');
0

Substring in JavaScript works like this:

string.substring(from, to);

where from is inclusive and to is exclusive. You can also use slice:

string.slice(from, to)

where from is inclusive and to is exclusive. The difference between slice and substring is with slice you can specify negative numbers. For example, from = -1 indicates the last character. from(-1, -3) would give you the last 2 characters of the string.

With both methods if you don't specify end then you will get all the characters to the end.

Paul

Comments

0

Ii they are always 2 digits how about;

var s = "01020399";
var result = []
for (var i = 0; i < s.length; i+=2)
    result.push(parseInt(s.substr(i, 2), 10) + ".")

alert( result[2] )        // 3.
alert( result.join(" ") ) // 1. 2. 3. 99.

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.