Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract only integers from string

Tags:

c++

I want to extract integers from a certain string in order to perform mathematical operations on them.

For example for a string

25 1 3; 5 9 2; 1 3 6

I only want to extract

25, 1, 3, 5, 9, 2, 1, 3, 6

is there a way I can do that?


1 Answers

I would just use the String Toolkit to parse the strings using spaces, brackets and semicolon as the delimiters.

I have answered the questions before Extract Data from a line

I paraphrased that code below:

#include <strtk.hpp>   // http://www.partow.net/programming/strtk

std::string src = "[25 1 3; 5 9 2; 1 3 6]";

std::string delims(" [];");

std::vector<int> values;

strtk::parse(src, delims, values );

// values will contain all the integers in the string.
// if you want to get floats & integers the vector needs to be float
like image 114
DannyK Avatar answered Jun 19 '26 06:06

DannyK