比较字符串时出错(窗口应用程序) [英] Error to compare string(window application)

查看:64
本文介绍了比较字符串时出错(窗口应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要显示F,我想一直显示到F,然后停下来并打印,直到允许F空间为止,但是我不知道.用这段代码写什么?
如果您还有其他方法,也请告诉.
在我给定的字符串中,我只需要FF FF FF.

这是我的代码:

I would like to display till F if any other character is there then stop and print till F space is allow, but I don''t know. What to write in this code?
If you have any other method then also tell.
In my given string I want only FF FF FF.

Here is my code:

string DATA="FF FF FF EF 01 02 FF FE"
System.Text.StringBuilder sb = new System.Text.StringBuilder(Data);

            for (int j = 0; j < sb.Length; j++)
            {
                if (System.Char.Equals("F")==(sb[j]) )
                    sb[j] = System.Char.(sb[j]);             
            }

推荐答案

我不确定您要做什么,但是请相信我,不是吗!

I am not sure what you are trying to do, but trust me, that isn''t it!

System.Text.StringBuilder sb = new System.Text.StringBuilder(Data);
 
                  for (int j = 0; j < sb.Length; j++)

将不执行任何操作-启动循环时StringBuilder为空,因此它将永远不会执行任何操作.

是一个愚蠢的想法-不要使用输出的大小来控制输入的循环! StringBuilders可以增长,如果可以的话,您很有可能进入永久循环.[/edit]

Will do nothing - the StringBuilder is empty when you start the loop, so it will never do anything.

[edit]Is a silly idea - do not use the size of the output to control looping round an input! StringBuilders can grow, and if it does, you stand a good chance of entering a permanent loop.[/edit]

System.Char.Equals("F")

不会编译,因为它是静态方法,需要进行比较. IE.

Is not going to compile, because it is a static method and requires something to compare against. I.e.

char c = "x";
if (c.Equals('F'))
   ...


此外,将char与字符串进行比较永远不会为您带来满意的结果...

坐吧深呼吸.
现在丢掉那很多,拿一张纸.
在纸上做作业,每次都要注意操作.
现在将其转换为代码.

然后回来再问我们您遇到的问题,因为目前我所能做的就是把它扔掉,然后为您做家庭作业-这对任何人都没有帮助!

[edit]不够咖啡-我错过了StringBuilder声明中的(数据),直到我按下提交! -OriginalGriff [/edit]


In addition comparing a char to a string is never going to have a happy results for you...

Sit back. Take a deep breath.
Now throw that lot away, and take a piece of paper.
Do the job on the paper, noting the operations each time.
Now convert that into code.

Then come back and ask us about what you are having problems with, because at the moment all I could do is throw it away and do you homework for you - which would help no-one!

[edit]Not enough coffeee - I missed the (Data) on the StringBuilder declaration until I pressed submit! - OriginalGriff[/edit]


请尝试以下代码.
Try as below code.
string DATA = "FF FF FF EF 01 02";
StringBuilder sbResult = new StringBuilder();
Char toCheck= 'F';

foreach (char c in DATA.ToCharArray())
{
  if (c != toCheck && c!= ' ')
   break;
        
  sbResult.Append(c);
}


有很多方法可以做到这一点!但这也许会让您对可以做的事情有所了解.

There are many ways to do this! But maybe this will give you an idea of something you could do.

string DATA = "FF FF FF EF 01 02";
string[] temp = DATA.Split(' ');
StringBuilder sb = new StringBuilder();
for (int i = 0; i < temp.Length; i++)
{
    if (temp[i] == "FF")
    {
        sb.Append(temp[i]);

        // uncomment if you want to keep spaces
        //sb.AppendFormat(" ", temp[i]);
    }
}

// uncomment if you want to remove space from end
//sb.Replace(" ", "", sb.Length - 1, 1);

MessageBox.Show(sb.ToString());


这篇关于比较字符串时出错(窗口应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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