How do I remove a substring from the end of a string?

strip doesn’t mean “remove this substring”. x.strip(y) treats y as a set of characters and strips any characters in that set from both ends of x. On Python 3.9 and newer you can use the removeprefix and removesuffix methods to remove an entire substring from either side of the string: The relevant Python Enhancement Proposal is PEP-616. On Python 3.8 and older you can use endswith and slicing: Or a regular expression:

What is the difference between isdigit() and isnumber()?

isnumber() may be an Apple-specific C++ method (I don’t have a Mac on hand to check). You can see it in the Apple dev guide: The isnumber() function behaves similarly to isdigit(), but may recognize additional characters, depending on the current locale setting. Besides, isnumber() is not declared on Linux: I use g++ 6.1.1 on Linux 4.7.2 and get the error: I … Read more

Best way to replace multiple characters in a string?

Replacing two characters I timed all the methods in the current answers along with one extra. With an input string of abc&def#ghi and replacing & -> \& and # -> \#, the fastest way was to chain together the replacements like this: text.replace(‘&’, ‘\&’).replace(‘#’, ‘\#’). Timings for each function: a) 1000000 loops, best of 3: 1.47 μs per … Read more

std::out_of_range error?

I’m dealing with a file with a linked list of lines with each node looking like this: and I’m writing a function that adds spaces at the beginning of the lines found in the variable text, by calling functions like linelist_ptr->text.insert(0,1,’\t’); The program compiles, but when I run it I get this error: Any ideas?

Convert array of strings into a string in Java

Java 8+ Use String.join(): Note that arr can also be any Iterable (such as a list), not just an array. If you have a Stream, you can use the joining collector: Legacy (Java 7 and earlier) Alternatively, if you just want a “debug-style” dump of an array: Note that if you’re really legacy (Java 1.4 and earlier) you’ll need to replace StringBuilder there with StringBuffer. Android … Read more

How to return a string from a C++ function?

You never give any value to your strings in main so they are empty, and thus obviously the function returns an empty string. Replace: with: Also, you have several problems in your replaceSubstring function: std::string::find returns a std::string::size_type (aka. size_t) not an int. Two differences: size_t is unsigned, and it’s not necessarily the same size as an int depending on your platform (eg. on 64 bits Linux or … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)