0

Answered # Extract and seperate data from a string

I have been looking around a while, trying to figure out at the very least, how to take a string like "25 from a car accident" and have 25 as a useable integer and "from a car accident" as a useable string. what i'm trying to accomplish is doing this from a text file. I can read and write and everything i just don't know how to take a string and separate it into the 2 components. Help much appreciated. I know this probably isn't that high a priority question :P so thank you for the support.

Featured Reply VernonDozier 2,218 I am wondering if there would be any chance that someone could help me out with this, I am trying to teach myself c++ which i find it really interesting and I came accross this and would love to see how it works so i could do it and learn ...

0

1

Stringstreams might be your best bet here.

```
#include <iostream>
#include <sstream>
using namespace std;
int main ()
{
string bigString, stringAfterNumber;
int number;
bigString = "25 from a car accident";
stringstream ss (bigString, stringstream::in | stringstream::out);
ss >> number;
getline(ss, stringAfterNumber);
cout << number << stringAfterNumber << endl;
cin.get();
return 0;
}
```

Note that stringAfterNumber will be " from a car accident", not "from a car accident". Depending on whether you want to retain that space, you may want to write a function that trims the white space in stringAfterNumber.

This question has already been answered. Start a new discussion instead.

Recommended Articles

Hi!

I have two tables, I will call them first-half and second-half (of the year)

```
Table: first
id jan feb mar apr
-----------------------
2 80 90 70 60
3 50 40 60 30
Table: second
id jan feb mar apr
-----------------------
2 50 70 60 90
3 80 60 50 ...
```

```
from turtle import *
import random
def allTriMedian (w=300):
speed (0)
vertices = []
point = turtle.Point(x,y)
for i in range (3):
x = random.randint(0,300)
y = random.randint(0,300)
vertices.append(trutle.Point(x,y))
point = turtle.Point(x,y)
triangle = turtle.Polygon(vertices)
a = triangle.side()
b = triangle.side()
c = triangle.side()
m1 = tirangle.median
m2 = triangle.median ...
```