1

I'm trying to retrieve the two integers (not floating numbers) from the following string:

 0 1 
 0 2 
 0 3 
 0 4 
 0 5 
 0 6 
 0 7 
 0 8 
 12 11
 10 22
 33 2
 102 149

I want to get in javasScript like:

 var line = "0 1";
 var firstInt = line.someMagic();
 var secondInt = line.someMagic();

please help. Any help will be appreciated. thanks!

EDIT

the split method can't be used.
cos sometimes the line has extra space in front or at the end of each line!

like:

var line = " 0 2 ";

that's why I needed the regex.

EDIT 2

Turns out, I don't need the regex!

thanks for the help!

5
  • 1
    you can still use split, but trim each line before. Commented Jan 7, 2016 at 1:29
  • @Sebas I really need a smooth and elegant solution... that's why I thought in regex... trim is not cool Commented Jan 7, 2016 at 1:31
  • If you really want to be elegant, you probably even can completely skip this parsing step and provide the string to the final method. I might be wrong since I don't see your code, but... Commented Jan 7, 2016 at 1:33
  • 3
    @AlvaroJoao: Regex is the opposite of smooth and elegant. It's a Swiss army knife so large it doesn't even fit in your pocket. When string methods can do the work easily, it's much more elegant, self-documenting, and more maintainable to use them. Commented Jan 7, 2016 at 1:35
  • @ShadowRanger I learned smth today! thanks! nice tips and thanks for the answer! Commented Jan 7, 2016 at 1:36

4 Answers 4

4

Just split the line on the space, and parseInt the chars into numbers :)

var line = "-21 42";

var ints = line.split(' ').map(function (num) {
    return parseInt(num, 10);
});

var firstInt = ints[0]; // -21
var secondInt = ints[1]; // 42

Edit:

If you are worried about trailing spaces etc, just trim the string first:

var ints = line.trim().split(' ').map(...
Sign up to request clarification or add additional context in comments.

10 Comments

just edit my question... can you use regex in your solution... thanks for this btw! :)
@AlvaroJoao using regexp does not make the solution better in some way. This is probably the most robust solution provided here so far.
@AlvaroJoao: just add .trim() between line and .split(' ') to make it line.trim().split(' ').map(... and it works fine.
@ShadowRanger hahaha I swear I didn't copy your comment in my edit lol I just chose the exact same spot for the ellipsis - what a coincidence :)
@naomik: Yeah, forgot that JS's split doesn't have the special behavior when passed a single space of splitting on runs of whitespace like Perl's does (and like Python's does when passed None). Easy fix is just changing the split pattern from ' ' to /\s+/. Using a regex, but such a simple one it shouldn't detract from readability.
|
2

This solution will only work for positive integers

// ES5
var input = "0 1";
var matches = input.match(/\d+/g);
var a = matches[0];
var b = matches[1];
console.log(a); // 0
console.lob(b); // 1

It's a little nicer with ES6

// ES6
let input = "0 1";
let [a,b] = input.match(/\d+/g);
console.log(a); // 0
console.lob(b); // 1

That said, RegExp isn't the only way to solve this. You may have leading or trailing space, but that's a non-issue. Here's a functional approach that makes quick work of this problem for you

const isNumber = x => ! Number.isNaN(x);
const parseInteger = x => window.parseInt(x, 10);

let input = '    20   -54    ';
let [a,b] = input.split(' ').map(parseInteger).filter(isNumber);
console.log(a); // 20
console.lob(b); // -54

Also note some people are advising the use of .trim which is not really going to solve your problems here. Trim may remove extraneous whitespace at the beginning end of your string, but it's not going to remove extra spaces in between the numbers. My solution above works regardless of how many spaces are used, but if you evaluate it, you'll find that it might be improved by splitting (not matching) with a regexp

// same effect as above, but breaks the string into less parts
let [a,b] = input.split(/\s+/).map(parseInteger).filter(isNumber);

The result of this is that the map and filter operations don't have to test for as many '' (empty string) and NaN (not a number) values. However, the performance cost of using the regexp engine may not outweigh the few extra cycles used to process the empty strings one-by-one.

1 Comment

Dude thanks for the regex! I will use this ES6 code for sure!
1

You can use the split method to break the string into an array:

var line = "0 1";
var pieces = line.trim().split(" ");
console.log(pieces);

If you prefer to use a regex:

var line = "0 1";
var pieces = /(\d+)\s(\d+)/g.exec(line);
console.log(pieces);

1 Comment

\d+ will not match negative numbers, use [+-]?\d+
0

You don't even need regex for this, it can be done very simply:

var line = "0 1";

// Split line into an array using the space as a delimiter
var parsedLine = line.split(" ");
var firstInt = parsedLine[0]; // => 0
var secondInt = parsedLine[1]; // => 1

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.