找到字符串问题c# [英] finding string problem c#

查看:63
本文介绍了找到字符串问题c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,我发现一个字符串是通过机器来的,并将值放在datagrid中。机器是一种纸币分拣机,有时它不会给出拒绝纸币。我的问题是,当它给出拒绝我的代码工作正常。但是当它没有给出任何拒绝时,它会提供一些我不想要的其他数据,我希望零显示。以下是我该如何做的。

  string  o =  @ 拒绝计数值
------------------------ --------;

int a = str.IndexOf(o)+ o.Length;
// if(
// {
string p = 总计:;
int b = str.IndexOf( p,a);
string b = str.Substring(a,ba);
dataGridView1.Rows [n] .Cells [ 5 ]。值= str6;

解决方案

虽然您的代码示例似乎很难阅读并且可以更清楚 - 我认为这就是你所追求的:



  string  o =  @ 拒绝计数值; 

/ / 更改此代码
// 获取字符串中字符串O的索引
// int a = str。 IndexOf(o)+ o.Length;

int a = str.IndexOf(o);

// 结束更改代码

< span class =code-comment> // 添加代码检查是否找到字符串o = @Reject count value
if (a!= -1)
{

// 最终添加代码


// 更改代码 -
// 将字符串添加到int a [Index + String o length]

a + = o.Length;

// 结束更改代码


string p = 总计:;
// 从字符串str中的a点开始
// 获取字符串P的索引
int b = str.IndexOf(p,a);

// 字符串b? b已被定义为int - 让我们调用它最好
string best = str.Substring(a,b-a);
dataGridView1.Rows [n] .Cells [ 5 ]。Value = str;


// 已添加代码
}
否则 // o未找到拒绝计数值
{
// 执行非拒绝所需的操作 - 将值设置为0
str = 0 ;

}
// 最终添加代码


I have a problem that I am finding a string that is coming via machine and putting the values in datagrid. Machine is a note sorting machine and sometimes it gives reject notes sometimes not. My question is when it is giving rejects my code is working fine. However when it is not giving any rejects it gives some other data that i dont want i want zero to be displayed. Here is the con how can i do it.

string o=@"Reject      count          value
--------------------------------";

                int a = str.IndexOf(o) + o.Length;
             //  if(
                //{
                    string p = "Total:";
                    int b = str.IndexOf(p, a);
                    string b = str.Substring(a, b-a);
                    dataGridView1.Rows[n].Cells[5].Value = str6;

解决方案

While your code example seems hard to read and could be more clear - I think this is what you are after:

string o=@"Reject      count          value";

// Changed this Code
// Gets the index of string O in the string
// int a = str.IndexOf(o) + o.Length;

int a = str.IndexOf(o);

// End Changed Code

// Added Code check if the string o=@"Reject      count          value" is even found
if(a != -1)
{

// End Added Code


// Changed Code -
// Adding th elenght of your string to int a [Index + String o length ]

a += o.Length;

// End Changed Code


string p = "Total:";
// Starting at point a in string str
// get index of string P
int b = str.IndexOf(p, a);

// String b? b is defined as int already - lets call it best
string best = str.Substring(a, b-a);
dataGridView1.Rows[n].Cells[5].Value = str;


// Added Code
}
Else  // o "Reject      count          value" was not found
{
// Do what you need to for a non reject - set the value to 0
str = 0;

}
// End Added Code


这篇关于找到字符串问题c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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