Getting a string

This is a discussion on Getting a string within the C++ Programming forums, part of the General Programming Boards category; Hi, So, basically I want to extract a sequence of characters (which is located between two other sequences that I've ...

  1. #1
    Registered User
    Join Date
    Jan 2006

    Getting a string


    So, basically I want to extract a sequence of characters (which is located between two other sequences that I've given) from a string.

    char* str="abcdefghi";
    someFunction(str, "abc", "ghi"); 
    //this is the function that would do what I want. It would return "def"- the sequence that is located between the given strings. It's not necessary for it to be one function, it could be any technique that accomplishes what I want.
    Thank you.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Look at functions like "strstr".

    Edit: I just realized that this is in the C++ section, in which case, perhaps you'd like to use the string class instead of char* for your string. In that case, functions find() and substr() are what you're looking for.


  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    I too would recommend using std::string with find() and substr(). However, how would you handle say, "abcdefghighi"? If you have a greedy matching, then the string in between the two others would be "defghi". For such a case, you might use rfind() instead of find().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2006
    strstr() gives me the address of a substring in memory. How do I get the substring's location relative to the whole string?


    I'll try to use the C++ variant if I won't find a way to do it C-style.

  5. #5
    Join Date
    Aug 2002
    Tucson, Arizona
    Here is an example of a std::string function that finds a string between strings

    #include <iostream>
    #include <string>
    std::string StringBetween(const std::string &findString, const std::string startString, const std::string endString)
    	//Grab the length of the first string for moving the index
    	size_t startLength = startString.length();
    	size_t endLength = endString.length();
    	//Sanity Check: Cannot find between empty strings
    	if(0 == startLength || 0 == endLength){
    		return "";
    	//Find the index of the first string
    	size_t startIndex = findString.find(startString);
    	//If we can't find that string return blank
    	if(std::string::npos == startIndex){
    		return "";
    	//Increment the start index to get the contents between
    	startIndex = startIndex + startLength;
    	//Find the index of the end string
    	size_t endIndex = findString.find(endString, startIndex);
    	//If we can't find it return blank
    	if(std::string::npos == endIndex){
    		return "";
    	//We found our string within our indexes return it back
    	return findString.substr(startIndex, endIndex - startIndex);
    int main()
    	//The string to search between
    	std::string findString = "abcdefghi";
    	//The string that will contain the items between strings
    	std::string subString = StringBetween(findString, "abc", "ghi");
    	//Output the result
    	//"Wait" for input

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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