如何在字符串居中功能中忽略某些字符串?
问题描述
NB:直接连接到
我想我可能错过了迭代的工作原理;我错过了什么,我怎样才能让这个功能以我想要的方式工作?
所以,你真正想做的是计算字符串中 $N
的实例,其中 N
是十进制数字.为此,只需使用 std::string::find
在字符串中查找 $
的实例,然后检查下一个字符是否为数字.
std::string::size_type pos = 0;while ((pos = input.find('$', pos)) != std::string::npos) {if (pos + 1 == input.size()) {休息;//字符串的最后一个字符是'$'}if (std::isdigit(input[pos + 1])) {宽度 += 2;}++位置;//从下一个字符开始下一个搜索}
为了使用std::isdigit
,您首先需要:
#include <cctype>
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):
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, which are denoted using a dollar sign and a number from 0-9 (i.e, $1
) and are not printed in the console itself.
The string centring function above treats these markers as part of the total string, so I want to add the total amount of characters these markers take up to the width so that the string is actually centred.
I have tried modifying the function:
The goal of the above function is to iterate through the string, add the current character and the next to an ostringstream
, and evaluate the ostringstream
.
This didn't exactly do as I wanted:
(snippet from server log)
Here's a brief summary of the issue:
I think I might be missing how iteration works; what am I missing, and how can I make this function work in the way I want it to?
So, what you are really trying to do is count the instances of $N
in your string, where N
is a decimal digit. To do this, just look in the string for instances of $
using std::string::find
, and then check the next character to see if it is a digit.
In order to use std::isdigit
, you need to first:
这篇关于如何在字符串居中功能中忽略某些字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!