How to extract substring by using start and end delimiters in C++

connor449

I have a string from command line input, like this:

string input = cmd_line.get_arg("-i"); // Filepath for offline mode

This looks like a file as below:

input = "../../dataPosition180.csv"

I want to extract out the 180 and store as an int.

In python, I would just do:

data = int(input.split('Position')[-1].split('.csv')[0])

How do I replicate this in C++?

Marcelo Amorim Menegali

Here's a (somewhat verbose) solution:

#include <string>
#include <iostream>

using namespace std;

int main() {
  string input = "../../dataPosition180.csv";
  // We add 8 because we want the position after "Position", which has length 8.
  int start = input.rfind("Position") + 8;
  int end = input.find(".csv");
  int length = end - start;
  int part = atoi(input.substr(start, length).c_str());
  cout << part << endl;
  return 0;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to extract line portion on the basis of start substring and end substring using sed or awk

How to extract a substring using regex

go - creating the substring start to end

Get substring from string using start and end white spaced position

How to extract substring matching the pattern using JQuery

how to check if string contains substring at the end - C

How can I extract a pattern (start and end) in a big string, using R?

How to extract a DataFrame using start and end dates with Pandas

How to extract start and end dates from single column in large dataset in R using lubridate?

Extract certain substring using regex in c#

How to extract a substring using a regular expression?

How to extract the phrase using the start and end phrases in this string?

How to extract a variable length substring using ksh

How to extract a substring using sed on OS X?

Extract substring in R from string with fixed start position and end point as a character found

How to extract a substring using regex for this pattern

How to extract substring from a string using regex?

Find and delete subString in a String with delimiters (start: 55555, end: \n)

How to extract lines knowing start and end lines

Using Delimiters for 'fscanf' in c

How to extract a path with in a string (which contains multiple delimiters) using python

Extract strings that start with ${ and end with }

Using vector in a regex to extract substrings with only known start & end

How to extract a substring from a string using regex

C# Regex substring should be at start and end but not in the middle

How to extract data from a string with specific start and end position using Php?

How to remove start and end html tag using C#?

split a string in javascript based on start and end delimiters

Java - How to extract a substring from start until the end of a particular word in a string?