Python Strip

GVNPublic123
GVNPublic123 used Ask the Experts™
on
Hello,

I am in need of a way (maybe function) on how to strip part of  a string away.

So I have a string like this:
mistur,attende,blackmetal,black,metal,skoddef

What I'd like to do is remove everything after last comma including last comma. So output would be:
mistur,attende,blackmetal,black,metal

Whats the easiest way to do it?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Use rsplit()


a='mistur,attende,blackmetal,black,metal,skoddef'


a.rsplit(",", 1)[0]


rsplit() splits() a string by a split character (you'll choose of course the coma)

rsplit starts splitting from the right handed side.
you tell it to search only one comma
you wil thus have a result of
[ "'mistur,attende,blackmetal,black,metal", "skoddef" ]

you take the first split value

Look at the pthon documentation of split() and rsplit() for more details.

Author

Commented:
Great that worked! Do you happen to know also how to do that in PHP?

Author

Commented:
works thanks
for PHP I'm not aware of an rsplit function, but I'm a little rusty with PHP.
I would use the function
strrpos($string, $substring)  to locate the first comma from the right side and then the function subst() to get the string until this position.


It should therefore be:

$s = "mistur,attende,blackmetal,black,metal,skoddef";
$till_last_comma = substr($s, 0, strrpos($s, ","));
print("<$till_last_comma>\n");

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial