Solved

Easy String question

Posted on 2004-08-18
5
241 Views
Last Modified: 2010-04-01
I'm trying to remove some title from the end of a string (phd, M.D. ESQ.)  

and in my debugging process, I am totally confused.  my goal is to remove the string listed in cleantTitle from the end of a string (there can be an extra character at the end of the string).

on removeEnding("PhD"), the index found is 6  which is the END of the second phd.  

1.  Why is Phd matching when it is not in the right case.

2.  Why is it giving me the end of the phd index?

on removeEnding("Phd") called right afterwars, the index found is 1.  which corresponds to nothing, what is going on?

CODE:

void main()
{
      static std::string name("phd phd");
      cleanTitle(name);
}

where

void cleanTitle ( string& str)
{
      cout << "cleanTitle()" << endl;
      removeEnding(str, "Phd");
      removeEnding(str, "PhD");
      removeEnding(str, "M.D.");
      removeEnding(str, "MD");
      removeEnding(str, "test");
}

void removeEnding(string &str, string ending)
{
      cout << "removeEnding()" << endl;
      cout << str.c_str() << endl;
      size_t index = str.find_last_of(ending);
      if(index != string::npos)
      {
            if(index + 2 >= str.size())
            {
                  str.erase(index - ending.size() + 1, ending.size());
            }
      }
      cout << str.c_str() << endl;
}

0
Comment
Question by:jjacksn
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
Tyrsis earned 300 total points
ID: 11837050
Hello,

find_last_of() returns the index of ANY character specified in the string.  I think you want to replace find_last_of() with rfind().

Tyrsis
0
 
LVL 15

Expert Comment

by:efn
ID: 11837497
Also, rfind will give you the index of the beginning of the substring in the searched string, so you will need to revise the index + 2 and index - ending.size() + 1 expressions.

--efn
0
 
LVL 5

Author Comment

by:jjacksn
ID: 11837559
does find do that as well?
0
 
LVL 5

Author Comment

by:jjacksn
ID: 11837563
also, why is it "finding" a string with the wrong case?
0
 
LVL 15

Assisted Solution

by:efn
efn earned 200 total points
ID: 11837861
find finds the first occurrence of the substring.  rfind finds the last occurrence.

It's not finding a string with the wrong case, it's finding a character with the right case.  When str is "phd phd" and ending is "Phd" and you call str.find_last_of(ending), you are not telling it to find the string "Phd".  You are asking for last position in str that contains a 'P' or an 'h' or a 'd'.  The last such position is 6, which contains a 'd'.

--efn
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question