String contains function

This is a discussion on String contains function within the C++ Programming forums, part of the General Programming Boards category; Hi all: In Java strings, there is a String.contains method that will tell you whether a particular string contains another ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    54

    String contains function - handling unicode

    Hi all:

    In Java strings, there is a String.contains method that will tell you whether a particular string contains another string as its substring. Is there something that is the same in C++? I'm trying to use string.find(string input), but that seems to return -1 every time.

    Thanks.

    Edit: This has progressed to a unicode/ascii problem. Anyone can shed some light regarding Unicode processing/detection?

    Thanks again.
    Last edited by JizJizJiz; 06-19-2006 at 06:09 PM.
    -Zack

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,618
    Yes, the find member function of std::string should work, assuming you are working with such strings. If the substring is not found, std::string::npos would be returned.

    If you are using null terminated strings, then strstr() from C++'s C legacy should be used.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by laserlight
    Yes, the find member function of std::string should work, assuming you are working with such strings. If the substring is not found, std::string::npos would be returned.

    If you are using null terminated strings, then strstr() from C++'s C legacy should be used.
    Thanks. However, that doesn't seem to be working. BTW, what does null terminated strings mean? I presume the getline function does give a null terminated string (I could be very wrong)?

    Here's what I'm working with:

    Code:
    string currentLine;
    	int line = 0;
    	ifstream input (file);
    	//input.open(file, ios::in);
    	while(!input.eof())
    	{
    		getline(input, currentLine);
    		line ++;
    		if (currentLine.find(contains) != string::npos)
    		{
    			int a = currentLine.find(contains);
    			cout <<"[" <<line <<"] " <<a <<"\n\n";
    		}
    	}
    -Zack

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,618
    BTW, what does null terminated strings mean? I presume the getline function does give a null terminated string (I could be very wrong)?
    By null terminated string I mean the C-style strings that use '\0' to denote one past the last character of the string. As for getline, that depends on which getline you refer to. In this case, std::getline works with std::string.

    I have no clue as to what data you are working with, so it could well be a problem with your data rather than with your code. To convince yourself that find works, try:
    Code:
    #include <string>
    #include <iostream>
    
    int main() {
    	if (std::string("hello world!").find("world") != std::string::npos) {
    		std::cout << "found" << std::endl;
    	} else {
    		std::cout << "not found" << std::endl;
    	}
    }
    By the way, you probably shouldnt be testing input.eof() in your loop condition. Rather, test with getline directly.
    Code:
    string currentLine;
    int line = 0;
    ifstream input(file);
    while (getline(input, currentLine))
    {
    	++line;
    	std::size_type pos = currentLine.find(contains);
    	if (pos != string::npos)
    	{
    		cout << "[" << line << "] " << pos << "\n\n";
    	}
    }
    Last edited by laserlight; 06-19-2006 at 10:55 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by laserlight
    By null terminated string I mean the C-style strings that use '\0' to denote one past the last character of the string. As for getline, that depends on which getline you refer to. In this case, std::getline works with std::string.

    I have no clue as to what data you are working with, so it could well be a problem with your data rather than with your code. To convince yourself that find works, try:
    Code:
    #include <string>
    #include <iostream>
    
    int main() {
    	if (std::string("hello world!").find("world") != std::string::npos) {
    		std::cout << "found" << std::endl;
    	} else {
    		std::cout << "not found" << std::endl;
    	}
    }
    By the way, you probably shouldnt be testing input.eof() in your loop condition. Rather, test with getline directly.
    Code:
    string currentLine;
    int line = 0;
    ifstream input(file);
    while (getline(input, currentLine))
    {
    	++line;
    	std::size_type pos = currentLine.find(contains);
    	if (pos != string::npos)
    	{
    		cout << "[" << line << "] " << pos << "\n\n";
    	}
    }
    Thanks. I can't seem to get the part with the find working...I changed it to look pretty much just like yours .

    You have a PM btw .
    -Zack

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    54
    So I know that "hello world" works, but "HELLO WORLD" doesn't work when I look for "HELLO" - also, if I use something like <html>, it would not find anything. I'm starting to think that this is perhaps a character set conversion problem?

    Can anyone shed light on this?

    Thanks.
    -Zack

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    54
    So we've determined that this is a problem between Unicode and ASCII text. Is there an easy way to convert?
    -Zack

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21