如何在字符串居中功能中忽略某些字符串?

How can I ignore certain strings in my string centring function?(如何在字符串居中功能中忽略某些字符串?)

本文介绍了如何在字符串居中功能中忽略某些字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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):

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, 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:

std::string centre(std::string input, int width = 113) { 
    std::ostringstream pStream;
    for(std::string::size_type i = 0; i < input.size(); ++i) {
        if (i+1 > input.length()) break;
        pStream << input[i] << input[i+1];
        CryLogAlways(pStream.str().c_str());
        if (pStream.str() == "$1" || pStream.str() == "$2" || pStream.str() == "$3" || pStream.str() == "$4" || pStream.str() == "$5" || pStream.str() == "$6" || pStream.str() == "$7" || pStream.str() == "$8" || pStream.str() == "$9" || pStream.str() == "$0")
            width = width+2;
        pStream.clear();
    }
    return std::string((width - input.length()) / 2, ' ') + input;
}

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:

<16:58:57> 8I
<16:58:57> 8IIn
<16:58:57> 8IInnc
<16:58:57> 8IInncco
<16:58:57> 8IInnccoom
<16:58:57> 8IInnccoommi
<16:58:57> 8IInnccoommiin
<16:58:57> 8IInnccoommiinng
<16:58:57> 8IInnccoommiinngg 
<16:58:57> 8IInnccoommiinngg  C
<16:58:57> 8IInnccoommiinngg  CCo
<16:58:57> 8IInnccoommiinngg  CCoon
<16:58:57> 8IInnccoommiinngg  CCoonnn
<16:58:57> 8IInnccoommiinngg  CCoonnnne

(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.

std::string::size_type pos = 0;
while ((pos = input.find('$', pos)) != std::string::npos) {
    if (pos + 1 == input.size()) {
        break;  //  The last character of the string is a '$'
    }
    if (std::isdigit(input[pos + 1])) {
        width += 2;
    }
    ++pos;  //  Start next search from the next char
}

In order to use std::isdigit, you need to first:

#include <cctype>

这篇关于如何在字符串居中功能中忽略某些字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在字符串居中功能中忽略某些字符串?

基础教程推荐