If I am serching this substring within a string. Is there any function that can search this just if the letters are correct, not if it is uppercase or lowercase. So it wouldn´t matter if the string looked like ex:

Number[
number[
numBer[
etc...

string stringToFind = "Number[";

there is stricmp() C function, but there is no c++ equivalent. convert the string to all upper or lower case then search it.

string stringToFind = "number[";
string stringToSearch = "something Number[1]";
transform(stringToSearch.begin(),stringToSearch.end(),tolower);
stringToSearch.find(stringToFind);

I am not sure if you ment that this example wont work with C++ ?
Anyway it seems that I cant find the tolower member at std::tolower so it doesn´t compile.

I found an example like this( I changed the transformline to this line). Though I dont know if this is correct. It do compiles but when running the program there is a message "External component throw an exeption"
It do compiles but I cant find std::tolower here either.

std::transform(stringToSearch .begin(), stringToSearch .end(), stringToSearch .begin(), static_cast<int(*)(int)>(std::tolower));

there is stricmp() C function, but there is no c++ equivalent. convert the string to all upper or lower case then search it.

string stringToFind = "number[";
string stringToSearch = "something Number[1]";
transform(stringToSearch.begin(),stringToSearch.end(),tolower);
stringToSearch.find(stringToFind);

tolower isn't in std:: namespace. And its defined to return an int so it isn't necessary to typecast it. You might have to include <ctype.h> std::transform(stringToSearch .begin(), stringToSearch .end(), stringToSearch .begin(), ::tolower);

if you use the algorithm std::search with a predicate, the string does not have to be transformed. will also work on const strings (with const_iterator).

#include <iostream>
#include <algorithm>
#include <cctype>

struct cmpnocase
{
  bool operator() ( char a, char b ) const
  { return std::toupper(a) == std::toupper(b) ; }
};

int main()
{
  std::string stf = "number[";
  std::string sts = "something NumBer[1]";
  std::string::iterator f = std::search( sts.begin(), sts.end(),
                       stf.begin(), stf.end(), cmpnocase() ) ;
  if( f != sts.end() ) 
    std::cout << "found: " << &*f << '\n' ;
}

if you use the algorithm std::search with a predicate, the string does not have to be transformed.

But the same letters have to be transformed many, if not hundreds, of times depending on the length of the string. Consequently, my guess is that transform() and then find() is faster than search()

in the worst case, search() does take quadratic time, but on the average, it runs in linear time.
and for transform and find to work in all cases, both strings have to be transformed.
i agree that if both strings are mutable, transform, transform and find could be a little faster than search with a predicate.

That did work when I included <ctype.h> but when I did put this inside a while loop then the program will through an exeption again.
The meening would be to search a .txt file for stringToFind1 line by line. It seems correct to me but perheps there is something wrong.

string stringToFind1 = "Number1[";

while( getline(fin1, Line, '\n')  )
{
			
std::transform(Line.begin(), Line.end(), Line.begin(), ::tolower);
string::size_type startPos = Line.find(stringToFind1);
startPos += stringToFind1.length(); 
string::size_type endPos = Line.find("]", startPos); 
string Number = Line.substr(startPos, endPos - startPos); 
	 	
       if( Line.find(stringToFind1) != string::npos )
       {
	fout1 << Number;
       }

}

But the same letters have to be transformed many, if not hundreds, of times depending on the length of the string. Consequently, my guess is that transform() and then find() is faster than search()

First make stringToFind1 all lower case because if you don't the find won't work. Then you need to validate that the find worked. Below I've left out a few of the lines you posted so you'll have to fix that in your own code.

string stringToFind1 = "number1[";
if( startPos != string::npos )
{
   // found, so continue here
   string::size_type endPos = Line.find("]", startPos);
   if( endPos != string::npos)
   {
        if( Line.find(stringToFind1) != 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.