473,382 Members | 1,622 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,382 software developers and data experts.

Remove suffix from string

Hello,

I'm writing code to remove .whatever from the end of a string.

e.g. hello.world.foo.bar -hello.world.foo

The following code seems to work

#include <string>
#include <iostream>
int main(int argc, char **argv)
{
std::string foo(argv[1]);
foo.erase(foo.rfind('.'));
std::cout << foo << std::endl;
return 0;
}

$ ./a.out hello.world.foo.bar
hello.world.foo

but it crashes when rfind() returns npos

$ ./a.out hello+world+foo+bar
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::erase
Aborted

Is pos+1 valid, even when pos==npos?

If so, I could write

#include <string>
#include <iostream>
int main(int argc, char **argv)
{
std::string foo(argv[1]);
foo.erase(foo.rfind('.')+1);
std::cout << foo << std::endl;
return 0;
}

Then I'd have to remove the trailing '.' and the entire string is erased
if there is no '.' (which may be acceptable).

Is there a better solution?

Regards.
Oct 24 '06 #1
5 6066
Spoon wrote:
Hello,

I'm writing code to remove .whatever from the end of a string.

e.g. hello.world.foo.bar -hello.world.foo

The following code seems to work

#include <string>
#include <iostream>
int main(int argc, char **argv)
{
std::string foo(argv[1]);
foo.erase(foo.rfind('.'));
std::cout << foo << std::endl;
return 0;
}

$ ./a.out hello.world.foo.bar
hello.world.foo

but it crashes when rfind() returns npos

$ ./a.out hello+world+foo+bar
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::erase
Aborted

Is pos+1 valid, even when pos==npos?
Yes: the result is of type string::size_type which is unsigned. Unsigned
built-in arithmetic types wrap on overflow.

However, since you have to ask, it relying on it may not be a good idea from
a maintenance point of view: the programmer that comes after you might not
know and get confused (notice that this could easily be yourself a few
months down the road).
>
If so, I could write

#include <string>
#include <iostream>
int main(int argc, char **argv)
{
std::string foo(argv[1]);
foo.erase(foo.rfind('.')+1);
std::cout << foo << std::endl;
return 0;
}

Then I'd have to remove the trailing '.' and the entire string is erased
if there is no '.' (which may be acceptable).

Is there a better solution?
What about

foo.erase( std::min( foo.rfind('.'), foo.size() ) );
Best

Kai-Uwe Bux

Oct 24 '06 #2

Spoon wrote:
Hello,

I'm writing code to remove .whatever from the end of a string.

e.g. hello.world.foo.bar -hello.world.foo

The following code seems to work

#include <string>
#include <iostream>
int main(int argc, char **argv)
{
std::string foo(argv[1]);
foo.erase(foo.rfind('.'));
instead:
foo.assign(foo, 0, foo.rfind('.'));
std::cout << foo << std::endl;
return 0;
}

$ ./a.out hello.world.foo.bar
hello.world.foo

but it crashes when rfind() returns npos
did you supply argv[1]? How about a check with argc in the code.
also, erase(...) expects iterators. assign(...) takes a size_type.
consult the header.
>
$ ./a.out hello+world+foo+bar
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::erase
Aborted

Is pos+1 valid, even when pos==npos?

If so, I could write

#include <string>
#include <iostream>
int main(int argc, char **argv)
{
std::string foo(argv[1]);
foo.erase(foo.rfind('.')+1);
std::cout << foo << std::endl;
return 0;
}

Then I'd have to remove the trailing '.' and the entire string is erased
if there is no '.' (which may be acceptable).

Is there a better solution?

Regards.
Oct 24 '06 #3
Salt_Peter wrote:
Spoon wrote:
>$ ./a.out hello.world.foo.bar
hello.world.foo

but it crashes when rfind() returns npos

did you supply argv[1]?
Errr... yes. (I provided the command and the output)
How about a check with argc in the code.
This is but a toy example.
also, erase(...) expects iterators.
This statement is inaccurate, as far as I can tell.

A valid prototype for erase is

basic_string& erase(size_type off = 0, size_type count = npos);
assign(...) takes a size_type.
consult the header.
I will.
>>$ ./a.out hello+world+foo+bar
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::erase
Aborted
Regards.
Oct 24 '06 #4

Spoon wrote:
Salt_Peter wrote:
Spoon wrote:
$ ./a.out hello.world.foo.bar
hello.world.foo

but it crashes when rfind() returns npos
did you supply argv[1]?

Errr... yes. (I provided the command and the output)
Ah, you need to run the program otherwise the arguement isn't injected.
I checked the header... and... you're RIGHT. erase *does* have a
version with size_type.

Try:

#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
if(argc 1)
{
std::string foo(argv[1]);
foo.erase(foo.rfind('.'));
std::cout << foo << std::endl;
} else {
std::cout << "argc = " << argc;
std::cout << std::endl;
}
return 0;
}

If you see the argc = # ouput then run the program instead. I got the
following with an arguement of test.txt
/*
test
*/
and the following when debugging:
/*
argc = 1
*/

Please accept my sincere apologees for misleading you.
>
How about a check with argc in the code.

This is but a toy example.
also, erase(...) expects iterators.

This statement is inaccurate, as far as I can tell.

A valid prototype for erase is

basic_string& erase(size_type off = 0, size_type count = npos);
assign(...) takes a size_type.
consult the header.

I will.
>$ ./a.out hello+world+foo+bar
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::erase
Aborted

Regards.
Oct 24 '06 #5
Spoon wrote:
>
Is there a better solution?
inline void erase_end(std::string& str, std::string::size_type pos)
{
if (pos != std::string::npos)
erase(str, pos);
}

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Oct 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: p.kosina | last post by:
Its just for my convienience: When saving new file in IDLE I HAVE to always manually add suffix .py, otherwise it is saved without ANY suffix. Can it be set somewhere? Thank you Pavel
3
by: Joseph | last post by:
Hi all, I want to build a compressed suffix trie from a string for string matching.instead of doing like: input:a string with 10 chars insert char array 10 insert char array 9,10 insert...
12
by: Iain Downie | last post by:
Dear list, we are getting a few folk trying to register for our birdwatching surveys with emails of the form: aname@phonecoop.coop, in other words with a 4 character ending (other examples are...
3
by: Green | last post by:
Hi, I have a question that when i surf the internet, i found out that using ..aspx suffix means using asp.net, using .jsp means using javaserver page, etc. But some don't have any suffix. For...
9
by: Hamish M | last post by:
Hi I am interested in opinions on this topic. I have heard that a suffix is not a good solution and type casts are much better for example. ...
4
by: Denis Jevon | last post by:
I have a problem which is probably simple to solve but I can't find the answer. In the past I have added pages to sites I have built with names like aboutus.html and when I or anyone else wants to...
1
by: SARAHE | last post by:
I have a C++ programming problem to extract a suffix such as Jr. or Sr. from a string that may vary each time. We are currently working with one assigned variable but I wanted to know how to code it...
3
by: Cyber dorkz | last post by:
hello, i've got this problem , as example : long number = 2000000000 my manual says that you must place a suffix after 2000000000 : long number = 2000000000L. suffix L means int must be...
2
by: royG | last post by:
hi when parsing a list of filenames like etc i want to extract the basename without the suffix...ie i want to get 'one','two' etc and not 'one.jpg' is there a function in python to do this or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.