Parsing a string to period sign into another string

I've been searching for quite some time and I can't find any way that's enough simple for me to understand.
Let's say I have string Text1 = "qwer.tzui" and string Text2. How would I put everything up to period (".") into string Text2, so that Text2 = "qwer".

I tried this:
1
2
3
for (int i=0; (i<Text1Size) || (Text1[i]!='.'); i++) {
		Text2[i]=Text1[i];
	}


But string isn't an array, so it doesn't work. I know there is a string function called find, but that only gives me a number where period is and I don't know how to use it since string isn't an array...
You have several way.
A string is an array, you need to give it an initial size in order to make the code above work.
Or you can use string operator +=
Or you can use stringstreams
Or you can use string members find and substr
see this for reference: http://www.cplusplus.com/reference/
You might want to look at std::stringstream.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <sstream>

//... stuff

	std::string text = "qwer.tzui";
	std::string text1;
	std::string text2;

	std::istringstream iss(text); // convert string into input stream

	std::getline(iss, text1, '.'); // read up to '.' into text1 (extracting the '.')
	std::getline(iss, text2); // read the rest of the string 
Last edited on
I'm sorry, I already looked at those and its all too confusing to me, I'm a bit ahead of my knowledge... I can't get source string array to work, because I get the string from another function through pointers. I'll paste only necessary part of the code, because there's a lot of it:

Headers that I'm using:
1
2
3
4
5
6
7
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h> 
#include <vector>
#include <cctype>
using namespace std;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	//File check
	string path, *pS;
	pS = &path;
	bool grLight, *pI;
	pI = &grLight;

	do { 
		grLight = 0;
		fileCheck(pS, pI);
	} while (!grLight);

	//Seqentual file check
	int pathSize = path.size();
	string fileName[20];

	for (int i=0; (i<pathSize) || (path[i]!='.'); i++) {
		fileName[i]=path[i];
	}


This is the fileCheck function:

1
2
3
4
5
6
7
8
9
10
11
void fileCheck(string *pathF, bool *grLightF) {
	*grLightF = 1;
	cout << "Name of the file: ";
	cin >> *pathF;
	string readFile = string(*pathF);
	ifstream fin(readFile.c_str());
	if(!fin) {
		cout << "Cannot open file.\n\n";
		*grLightF = 0;
	}
}


How would I turn string "path" into an array in a proper way?

EDIT: @Galik Haven't seen your post yet, will try it out now.
Last edited on
Ok, that worked. I just forgot to add necessary headers, should probably take a break hours ago :)

Thanks to both!
Topic archived. No new replies allowed.