473,383 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

std::string remove the last character if its a '/'

14
hi,
how do I remove the last character in a string only if its a "/". otherwise no need to remove the last character.

im using std::string. tried with find_last_of( ).

thanks
rsennat
Feb 4 '08 #1
3 31465
rsennat
14
Is this efficient,
Expand|Select|Wrap|Line Numbers
  1. size_t pos = Path.find_last_of("/");
  2. if(pos == Path.length()-1)
  3. {
  4.     Path.erase(Path.length()-1);
  5. }
  6.  
hi,
how do I remove the last character in a string only if its a "/". otherwise no need to remove the last character.

im using std::string. tried with find_last_of( ).

thanks
rsennat
Feb 4 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
No.

You should find_last_of() and check the value of pos afterwards, it if is string::npos, the / was not found. Please do not assume that -1 means not found.

Otherwise, the / was found and you can erase the character at the location where it was found:
Expand|Select|Wrap|Line Numbers
  1. string::size_type pos = Path.find_last_of("/");
  2. if(pos !=string::npos)
  3. {
  4.     Path.erase(pos);
  5. }
  6.  
Feb 4 '08 #3
Sorry weaknessforcats, it seems that you didn't understand rsennat's question.

The question was about about last character in a string whatever it is and not about last occurrence of '/' character. You code will remove '/' even if it is found in the middle of string.

The correct code is:

Expand|Select|Wrap|Line Numbers
  1. if (path.length() > 0)
  2. {
  3.     std::string::iterator it = path.end() - 1;
  4.     if (*it == '/')
  5.     {
  6.          path.erase(it);
  7.     }
  8. }
  9.  
Apr 27 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
1
by: Chris Mantoulidis | last post by:
PROBLEM: I'm having some weird problems with string::find() (in ParamGenerate()), but since I'm not sure if that is the source of all bad output in my program, I'm posting least code that's...
18
by: JKop | last post by:
Can some-one please point me to a nice site that gives an exhaustive list of all the memberfunctions, membervariables, operators, etc. of the std::string class, along with an informative...
1
by: Tero Toivanen | last post by:
Dear experts, I am doing code to Solaris 9 system with C++. I get every now and then segmentation fault in the following code that removes heading and trailing white spaces (mLineStr is of...
8
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
7
by: JustSomeGuy | last post by:
I need to make a class called uid. A UID is a unique identifier. It looks like... 1.2.3.345.1.2.4.566 This uid get transmitted over a network as 8 bit binary data. If the length of the UID is...
4
by: Jim Langston | last post by:
Is there any builtin lowercase std::string compare? Right now I'm doing this: if ( _stricmp( AmmoTypeText.c_str(), "GunBullet" ) == 0 ) AmmoType = Item_Ammo_GunBullet; Is there anything the...
3
by: Jim Langston | last post by:
Is it possible to initialize a std::string with a character array, not neccessarily null terminated? I.E. Given something like this: char buffer; buffer = 0x01; buffer = 0x00; buffer = 'A';...
2
by: darkkal | last post by:
Hi I'm a student majoring a computer . I tried to make a simple hashing table like (int) 1 , (std::string) "One" There is a problem in using std::map ㅜㅜ this is a simple code. PLZ~~Help...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.