截取字符串Substring

本文介绍了一种批量扫描条码并处理相关数据的方法,包括字符串操作、数据集管理和错误提示。通过截取和判断输入字符串,实现对条码范围的识别与验证,以及对特定格式(如“-”或“/”分隔)的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

截取字符串(第一个字符串,最后一个,某个位置字符串)
string strGroupBatchCodeSub = strGroupBatchCode.Substring(0, strGroupBatchCode.IndexOf('_'));
判断是否含有某个字符串(“,”,“-”,“/”)
 if (strTextBoxValue.Contains(strTextBoxSub1))
#region yehy 批量扫条码
                        DataSet dsAllWorkJob = null;
                        if (textBox1.Text.Trim().ToString() != "")
                        {
                            DataSet dsAll = m_PlanOverAndBackLogic.GetInformation(strCurrentWorkType, strCraftplanNo, strDeptAll);
                            if (dsAll.Tables[0].Rows.Count > 0)
                            {
                                string strGroupBatchCode = "";
                                foreach (DataRow dst in dsAll.Tables[0].Rows)
                                {
                                    strGroupBatchCode = dst["Groupbatchcode"].ToString().Trim();
                                }
                                //组批次
                                string strGroupBatchCodeSub = strGroupBatchCode.Substring(0, strGroupBatchCode.IndexOf('_'));
                                //过程卡序号值
                                string strTextBoxValue = textBox1.Text.Trim().ToString();
                                //判断第一位和最后一位
                                //截取是否是 - /
                                string strTextBoxSub1 = "-";
                                string strTextBoxSub2 = "/";
                                //判断输入 是否含有 - 字符串
                                if (strTextBoxValue.Contains(strTextBoxSub1))
                                {
                                    string[] strTextBoxValueSub = strTextBoxValue.Split('-');
                                    string strGroupPin1 = "";
                                    string strGroupPin2 = "";
                                    int ij = 0;
                                    for (int i = 0; i < strTextBoxValueSub.Length; i++)
                                    {
                                        if (i ==0)
                                        {
                                            strGroupPin1 = strTextBoxValueSub[0];
                                        }
                                        else
                                        {
                                            strGroupPin2 = strTextBoxValueSub[1];
                                        }
                                        ij++;
                                    }
                                    //判断是否为两位
                                    if (ij == 2)
                                    {
                                        int intGroupPin1 = Convert.ToInt32(strGroupPin1);
                                        int intGroupPin2 = Convert.ToInt32(strGroupPin2);
                                        //判断第一个是否小于第二个
                                        if (intGroupPin1 < intGroupPin2)
                                        {
                                            string strGroupPinhe1 = strGroupBatchCodeSub + "_" + strGroupPin1;
                                            string strGroupPinhe2 = strGroupBatchCodeSub + "_" + strGroupPin2;
                                            dsAllWorkJob = m_PlanOverAndBackLogic.GetInformation1(strCurrentWorkType, strCraftplanNo, strDeptAll, strGroupPinhe1, strGroupPinhe2);
                                        }
                                        else
                                        {
                                            UserMessages.ShowInfoBox("输入格式如下(6-8)!");
                                        }

                                    }
                                    else
                                    {
                                        UserMessages.ShowInfoBox("输入格式如下(6-8)!");
                                    }
                                    
                                }
                                //判断输入 是否含有 / 字符串
                                else if (strTextBoxValue.Contains(strTextBoxSub2))
                                {
                                    string[] strTextBoxValueSub = strTextBoxValue.Split('/');
                                    string strTextBoxValueZhuH = "";
                                    for (int i = 0; i < strTextBoxValueSub.Length; i++)
                                    {
                                        strTextBoxValueZhuH += "'"+strGroupBatchCodeSub + "_" + strTextBoxValueSub[i] +"'"+ ",";
                                    }
                                    //将最后一个逗号去掉
                                    string strTextBoxValueZhuHZ = strTextBoxValueZhuH.Substring(0, strTextBoxValueZhuH.Length - 1);
                                    dsAllWorkJob = m_PlanOverAndBackLogic.GetInformation2(strCurrentWorkType, strCraftplanNo, strDeptAll, strTextBoxValueZhuHZ);
                                }
                                else
                                {
                                    UserMessages.ShowInfoBox("输入格式不正确!");
                                }
                            }
                            else
                            {
                                Timer("该条码下没有您的生产任务!");
                                this.KWTextBox.Text = "";
                                this.textBox1.Text = "";
                            }
                        }
                        #endregion
回答: 在C#中,可以使用Substring方法来截取字符串。该方法接受两个参数,第一个参数是起始位置的索引,第二个参数是要截取的字符数。例如,oriString.Substring(0, 2)将从第一个字符开始截取2个字符,输出结果为"He"。而oriString.Substring(2, 3)将从第一个'l'开始截取3个字符,输出结果为"llo"。如果只传入一个参数,即oriString.Substring(6),则从第七个字符开始截取字符串的末尾,输出结果为"Kitty!"。需要注意的是,当指定的索引超出字符串的长度时,会发生字符串截取越界的问题,因此在使用Substring方法时需要注意参数的合法性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [C#截取字符串 Substring 使用方法](https://blog.csdn.net/ba_wang_mao/article/details/107505387)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Java截取字符串substring)](https://blog.csdn.net/weixin_40052298/article/details/121868965)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值