string-iteration

How can I ignore certain strings in my string centring function?

眉间皱痕 提交于 2020-01-22 00:20:14
问题 N.B: Directly connected to a problem I had a few years ago, but I'd like to resolve the first issue there which wasn't otherwise part of the question, so please don't flag it as a duplicate of my earlier question. I have a string centring function that centres the given string according to the given width (which is 113 characters): std::string center(std::string input, int width = 113) { return std::string((width - input.length()) / 2, ' ') + input; } I am using a game SDK in order to create

How can I ignore certain strings in my string centring function?

你说的曾经没有我的故事 提交于 2019-12-02 05:47:44
N.B: Directly connected to a problem I had a few years ago , but I'd like to resolve the first issue there which wasn't otherwise part of the question, so please don't flag it as a duplicate of my earlier question. I have a string centring function that centres the given string according to the given width (which is 113 characters): std::string center(std::string input, int width = 113) { return std::string((width - input.length()) / 2, ' ') + input; } I am using a game SDK in order to create a gameserver modification, and this game SDK supports coloured strings in the game's command console,

Why does Python 'for word in words:' iterate on individual characters instead of words?

与世无争的帅哥 提交于 2019-12-01 17:34:08
问题 When I run the following code on a string words : def word_feats(words): return dict([(word, True) for word in words]) print(word_feats("I love this sandwich.")) I get the output dict-comprehension in letters instead of words: {'a': True, ' ': True, 'c': True, 'e': True, 'd': True, 'I': True, 'h': True, 'l': True, 'o': True, 'n': True, 'i': True, 's': True, 't': True, 'w': True, 'v': True, '.': True} What am I doing wrong? 回答1: You need to explicitly split the string on whitespace: def word