日期:2014-05-18  浏览次数:20653 次

c#中一个字符串中的一个小问题

比如:c#中定义一个字符串:
string s="hello,world!";
我想取前面的前5个字符,也就是hello 应该怎么实现?



------解决方案--------------------
string s="hello,world!"; 
s=s.SubString(0,5);
------解决方案--------------------
探讨
string s="hello,world!";
s=s.SubString(5);

------解决方案--------------------
如果你不知道.net类库中有SubString这个函数,你也可以自己去实现啊:
C# code

            string s = "hello,world!";
            string substring = "";
            for (int i = 0; i < 5; i++)
            {
                substring = substring + s[i].ToString();
            }
            return substring;