Solved

Easy String question

Posted on 2004-08-18
5
240 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

803 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