如何忽略我的字符串中心函数中的某些字符串? [英] How can I ignore certain strings in my string centring function?

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

问题描述

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()){
break; //字符串的最后一个字符是一个'$'
}
if(std :: isdigit(input [pos + 1])){
width + = 2;
}
++ pos; //从下一个char开始下一个搜索
}

为了使用 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>

这篇关于如何忽略我的字符串中心函数中的某些字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆