I have a .txt file that contains this informaton:

Numb1 Number2[1] Numb3

Now there is 2 things that I am trying to do here. I dont know how to begin this.
First I will search if there is any Word in this file that is named:

Number2[1]

But the tricky thing here is that in this case there is a "1" inside the [].
The problem is that I will not know what number it is inbetween.
So initially what I am searching for is Number2 that consist of [] with an unknown number inside.

The 2 things I am trying to do is this then:

First: Get that number what it now will be and put that to a std::string.
Second: Is to output the file with one change and that is to replace in this case
"Number2[1]" to the word: "ABCDE" so the output file will look like this:

Numb1 ABCDE Numb3

Any idéas on how it could be possible to begin this ?

## All 12 Replies

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.

Then number that I have found between the square brackets I will first then put to a std::string and after this... atoi it to an int to use in a calculation.

The first thing I might wonder is then to find Number2[?] in the .txt file that I ifstream.
The problem I think of is that I am serching for the whole "Number2[?] but don´t know what is in between the brackets. Perheps I have to in someway search for a
substr(0, rfind("[")+1) that is equal to "Number2" and then put this to a string and then substring that to find the number. Am I thinking right.

I suppose I will read each line and put it to a "string" and search this string for substr Number2 in anyway.
I am not really sure how to exactly practically begin how to do.

The first goal should be to just find that number inside the brackets.
I think it could be done if I put the whole line '\n' into a string and then put the startindex to this and then continue find the number.
Is this the way it could be done ?
Thanks.

``Line.substr(Line.rfind("Number2"))``

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.

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".

I am not sure if I am doing right here. I am trying to find the startindex like this:
I think I have missed something. When doing this StartIndex return: -1

``````std::string Line = "abc Number[1]";
int StartIndex = 0;

StartIndex = Line.find("Number[");``````

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".

>>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());
}``````

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 "["
I have done like below but index2 will have the substring: "1] 2Number"

I wonder why that happens since I have set index (8,10)

``````std::string Line = "abc 1Number[1] 2Number[1]";
string index;
index = Line.substr(Line.find("1Number["));

string index2;
index2 = index.substr(8,10);``````

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?

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;``````

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;``````

I didn´t know that the second argument was the length so now I know :)

I have change the code a bit though my example perheps wasn´t the best.
I do face a problem that I dont know how to solve really.
If I put the first occurence in the string with brackets like this: a4545l5l[12]
Then this code will not work to extract the number "51".
This is because I am .find("]") wich will be the first one in the string Line;
and as it takes this one into considiration, this will meen the length at the second argument in Line.find.

Is it not possible to find the next occurence of a character and then find("]") wich comes after the string stringToFind ?

``````std::string Line = "a4545l5l[12] 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("]") - index));

fout1 << Number;``````

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.

This did work fine and I did handle the scenario like this if there is no match for the stringToFind and it seems to work fine.

I am not really sure what ::size_type really meens ? The only thing I understand is that it is something that can handle any size of string.
Anyway this is a powerful way of searching strings. Thanks...

``````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);

if( Line.find(stringToFind) != string::npos )
{
fout1 << Number;
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.