【LABVIEW到C#】4》String的操作之Search and Replace.vi

C#封装如下:

public class SearchAndRepalce : Darrenstring
        {            
            public bool replaced;
            private string stringout;
            public SearchAndRepalce()
            {
            }
            public string searchandreplace(string inputstring, string searchstring, string replacestring, bool replaceall)
            {
                if (replaceall)
                {
                    replaced=true;
                    stringout = inputstring.Replace(searchstring, replacestring);
                    return stringout;
                }
                else
                {
                    Matchpattern A = new Matchpattern(inputstring, searchstring);
                    if (A.ismatch)
                    {
                        stringout = A.Before() + replacestring+A.After();
                        replaced=true;
                        return stringout;
                    }
                    else
                    {
                        replaced = false;
                        return null;                       
                    }                
                }               
            }
        }

上述C#是根据项目常用的方式进行封装的,由于我是做工控软件的,所以是根据自己的实际情况进行封装。

在项目中number of replacement 通常会和0做比较以此来确定,string是否含searchstring。所以在C#的再封装中 直接使用bool。

做个winform程序来验证

实现代码

private void Confirm_Click(object sender, EventArgs e)
        {
            Darrenstring.SearchAndRepalce A = new Darrenstring.SearchAndRepalce();
            Textout.Text = A.searchandreplace(Text.Text, Searchstring.Text, Replacestring.Text, Rplaceall.Checked);
        }  

图片如下

感觉挺好用的,简简单单的语言思维移植。Labview的此VI和这个类库的用法相同。

posted @ 2015-03-05 18:00  梦小邪  阅读(792)  评论(0编辑  收藏  举报