计算一组字符的出现次数,并从字符串中减去空格 [英] Count occurrence of a set of characters and subtract spaces from the string

查看:98
本文介绍了计算一组字符的出现次数,并从字符串中减去空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关问题,请参阅下面的第一张图片。您可以看到文本的第一行正确对齐,但第二行不正确。



几个月前,我写了一些代码,一个字符串,以便在控制台上进行更好的处理。这个代码的工作原理非常好,但有两个主要问题:




  • 如果我们超过112个字符的限制,gameerver崩溃实际控制台(此问题在正常的控制台消息中不会出现)


  • 在文本中使用颜色会导致字符串右移两个字符对于每个颜色代码




我希望解决第二个问题(虽然解决第一个问题将是一个巨大的好处) ,但没有一个线索如何做到这一点。



如果我没有9种颜色类型可供选择,这段代码可能很简单:

  * $ 1 =白色
* $ 2 =深蓝色
* $ 3 =绿色
* $ 4 =红色
* $ 5 =黄色
* $ 6 =浅蓝色
* $ 7 =紫色
* $ 8 =橙色
* $ 9 =灰色

我不想简单地查找'$',因为在某些情况下它被用作正常字符。



在插入空格时忽略$ 1- $ 9?







  int CScriptBind_GameRules :: CentreTextForConsole(IFunctionHandler * pH,const char * msg)
{
if(msg)
{
const int linelength = 200;
char newmsg [linelength + 1];
for(int i = 0; i newmsg [i] = 0;
for(int i = 0; i newmsg [i] ='';
newmsg [linelength] = 0;
int msglen = strlen(msg);
int startpos = 4 + linelength / 2-msglen / 2;
for(int i = msglen-1; i> = 0; i--)
newmsg [startpos + i] = msg [i]
msg = 0; // Null消息。
return pH-> EndFunction(newmsg);
}
else
{
return pH-> EndFunction();
}



示例



我现在不能到我的开发电脑,因为我的工作 - 我会在几个小时后,如果我回来,如果这个问题还没有解决的屏幕截图。



来自Lua的来电者:



  Core.CenteredConsole:All(g_gameRules.game :CentreTextForConsole($ 1rysis $ 4Wars Infinity iX 5.0)); 



这显示它应该是什么样子。



解决方案

您的程序崩溃,因为您的数组的定义的长度。适当调整 newmsg 的大小



同时检查 startpos + i> = 0&& ; <数组的长度 ...






对于您的第二个问题:



正如你所说:


在文本中使用颜色会导致字符串向右移动每个颜色代码两个字符


看起来在打印之前,颜色代码被删除,您的问题出现,因为您正在考虑包含颜色代码的消息的长度...


如果是这样,我该如何处理? b

也许为 msg 上找到的每个颜色代码减少2个空格。


See the first image below for the problem. You can see that the first line of text correctly aligns, but the second does not.

A few months ago I wrote some code that centers the text in a string in order to process it nicely onto a console for a gameserver modification. This code works really nicely, but has two major problems:

  • The gameserver crashes if we go over the 112 character limit of the actual console (this problem doesn't occur in a normal console message)

  • Use of colours in the text causes the string to shift to the right by two characters for each colour code

I am looking to solve the second problem (although solving the first would be a massive benefit), but do not have a clue how to do this. I cannot simply take the colour coding out since this is needed to colour the text.

This code might be simple if I didn't have 9 colour types to choose from:

*$1= White
*$2= Dark Blue
*$3= Green
*$4= Red
*$5= Yellow
*$6= Light Blue
*$7= Purple
*$8= Orange
*$9= Grey

I don't want to simply look for the '$' either, since this is used as a normal character in some cases.

Is ignoring the $1-$9 when inserting the spaces possible? If so, how would I approach this?

The Code

int CScriptBind_GameRules::CentreTextForConsole(IFunctionHandler *pH, const char *msg)
{
    if (msg)
    {
        const int linelength=200;
        char newmsg[linelength+1];
        for(int i=0;i<linelength;i++)
            newmsg[i]=0;
        for(int i=0;i<linelength;i++)
            newmsg[i]=' ';
        newmsg[linelength]=0;
        int msglen=strlen(msg);
        int startpos=4+linelength/2-msglen/2;
        for(int i=msglen-1;i>=0;i--)
            newmsg[startpos+i]=msg[i];
        msg=0; //Null the message.
        return pH->EndFunction(newmsg); 
}
else
{
    return pH->EndFunction();
}

Example

I can't get to my development PC right now as I'm as work- I'll post a screenshot in a few hours when I get back if this issue hasn't been resolved.

Caller from Lua:

Core.CenteredConsole:All(g_gameRules.game:CentreTextForConsole("$1Crysis$4Wars Infinity iX 5.0"));

This shows what it should look like. The message only works properly if it is less than 80 characters long.

解决方案

Your program crashes because of the defined length of your arrays. Resize newmsg appropriately

Also check that startpos + i >= 0 && < length of array ...


As for your second question:

As you said:

Use of colours in the text causes the string to shift to the right by two characters for each colour code

Seems that colour codes are being removed before printed and your problems arises because you are taking into account a length of a message which includes color codes...

If so, how would I approach this?`

Maybe adding 2 less spaces for each color code found on the msg for example.

这篇关于计算一组字符的出现次数,并从字符串中减去空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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