C#winform中怎么实现左截取字符串和右截取字符串

2025-05-12 15:25:45
推荐回答(1个)
回答(1):

写两个函数

     string LeftString(string s, int length)
        {
            return s.Substring(0, length);
        }
        string RightString(string s, int length)
        {
            if (length >= s.Length) return s;
            return s.Substring(s.Length - length, length);
        }

测试

 string s = "123456";
 string s1 = LeftString(s, 4);  //<-- s1 = 1234
 sring s2 = RightString(s, 4);  //<-- s2 = 3456