something about deal with string

来源:互联网 发布:worldwind java 教程 编辑:程序博客网 时间:2024/05/22 04:25

           in our project,we have to spend a lot of care on deal with string.now ,i want to say some method in string class in c#.

           in c# ,string provide remove,substring,insert method to transact string.look a example,now if we want to change "1984:12:21:*"  to "1984122100" code is:

            string ss = "1984:*:21:*";
            int i = 0;
            int count = 0;
            for (i = 0; i < ss.Length; i++)
            {
                if (ss.Substring(i, 1) == ":")
                {
                    count++;
                    ss = ss.Remove(i, 1);
                    if (ss.Substring(i - 1, 1) == "*")
                    {
                        int count = 0;
                        for (i = 0; i < ss.Length; i++)
                        {
                            if (ss.Substring(i, 1) == ":")
                            {
                                count++;
                                ss = ss.Remove(i, 1);
                                if (ss.Substring(i - 1, 1) == "*")
                                {
                                    switch (count)
                                    {
                                        case 1:
                                            ss = ss.Remove(i - 1, 1);
                                            ss = ss.Insert(i - 1, "0000");
                                            i = 0;
                                            break;
                                        case 2:
                                            ss = ss.Remove(i - 1, 1);
                                            ss = ss.Insert(i - 1, "00");
                                            i = 0;
                                            break;
                                        case 3:
                                            ss = ss.Remove(i - 1, 1);
                                            ss = ss.Insert(i - 1, "00");
                                            i = 0;
                                            break;
                                        case 4:
                                            ss = ss.Remove(i - 1, 1);
                                            ss = ss.Insert(i - 1, "00");
                                            i = 0;
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                        }

                        if (ss.Substring(ss.Length - 1, 1) == "*")
                        {
                            ss = ss.Remove(ss.Length - 1, 1);
                            ss = ss.Insert(ss.Length - 1, "00");
                        }
                    }
                }
            }

through this code ,we complish change ;we just  used remove,insert,substring method,so i think if you are c# coder, you are very luck ,because we just waste a littele time can deal with string.

原创粉丝点击