Its pretty easy to extract the number between the square brackets, but why? put the number in a std::string ?
std::string has a replace() method that you can use to replace one substring with another.
Ancient Dragon
Retired & Loving It
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
How about using the "find" function in the string class to search for an occurrence of "Number2["?
I think you are on to the same idea with rfind. find will return the index of the 'N' in "Number2[". Add the number of characters in "Number2[" and you are at the index of the number inside the brackets and you can extract that number. I'm sure using rfind would also work equally well. This page link has a good example of how to use "find".
http://www.cplusplus.com/reference/string/string/find.html
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
>>substr(0, rfind("[")+1)
Don't even attempt to do that because it might crash the program if rfind returns string::npos (-1). Better to split it into two lines so that you can do error checking
size_t pos = line.find('[');
if( pos != string::npos)
{
int x = atoi( line.substring(pos+1).c_str());
}
Ancient Dragon
Retired & Loving It
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
I think I have to take it from the beginning. I mix things up I think.
I will take a straight example. I have the string Line and want to know the index of "[" in
1Number[1].
If I do like I have done below I will have the index 4 wich is the beginning of "1Number["
I am unsure how to continue to I reach the "["
std::string Line = "abc 1Number[1] 2Number[1]";
int index;
index = Line.find("1Number[");
Here was my idea. This only works for one digit indexes, but it takes you to the index of the STRING that is the first digit of the integer inside of the brackets. You'd have to extract that number and convert it to an integer, but this delivers you to the correct spot in the string:
#include <string>
#include <iostream>
using namespace std;
int main ()
{
std::string Line = "abc 1Number2[5] 2Number[1]";
int index;
string stringToFind = "Number2[";
index = Line.find(stringToFind);
index = index + stringToFind.length ();
cout << "Here is the index: " << Line[index] << endl;
return 0;
}
This will display 5.
I think this is what you are looking for, or at least part of it. Is it, or am I misinterpreting the goal?
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Thank you. That did work and I understand the logic. I have tried for a while now to experiment on this as this as you said only could find one digit number inside the []
So I am trying to develop it to find everyting until the next "]"
I have tried out this example but this finds the last "]" on that line and not next after the number 51 so it extracts: "51] 2Number[1]" and not only then "51"
std::string Line = "abc 1Number2[51] 2Number[1]";
int index;
string stringToFind = "1Number2[";
index = Line.find(stringToFind);
index = index + stringToFind.length ();
std::string Number;
Number = Line.substr(index, Line.find("]"));
fout1 << Number;
It actually is finding the first ']', not the second. Here is the prototype for substr:
string substr ( size_t pos, size_t n) const;
n in the above prototype is NOT an index of the string. Rather it is the number of characters in the substring. In your case above, the '5' is at index 13 of the string and ']' is at index 15. You want the substring to be two characters. You are specifying that it will be 15 characters. Hence you get all the extra characters. Try this modification:
std::string Line = "abc 1Number2[51] 2Number[1]";
int index;
string stringToFind = "Number2[";
index = Line.find(stringToFind);
index = index + stringToFind.length ();
std::string Number;
int rightBracketIndex = Line.find("]");
int numCharsInNumber = rightBracketIndex - index;
Number = Line.substr(index, numCharsInNumber);
fout1 << Number;
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Is it not possible to find the next occurence of a character and then find("]") wich comes after the string stringToFind ?
The find() function can be told where to start the search from, see below
string Line = "a4545l5l[12] 1Number2[51] 2Number[1]";
string stringToFind = "1Number2[";
string::size_type startPos = Line.find(stringToFind);
startPos += stringToFind.length();
string::size_type endPos = Line.find("]", startPos);
string Number = Line.substr(startPos, endPos - startPos);
cout << Number;
Remember that find() returns std::string::npos, if there is no match, so handle that case also in your code.
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395