C#中如何去掉字符串中的第一个空格 如题.比如 string temp = "<p> hello world ",我想要的结果是:temp = "<p> hello world " 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 void Main(){ string temp = "<p> hello world "; int i=1; temp=Regex.Replace(temp," ",delegate(Match m){ if(i==1) {i++;return "";} else{return " ";} }); Console.WriteLine(temp);//<p> hello world }论坛签名======================================================================silvertoss:你好!截至 2011-03-17 10:22:30 前:你已发帖 13 个, 未结贴 4 个;结贴率为: 69.23%当您的问题得到解答后请及时结贴.http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.htmlhttp://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.htmlhttp://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html如何给分和结贴?http://community.csdn.net/Help/HelpCenter.htm#结帖如何给自己的回帖中也加上签名?http://blog.csdn.net/q107770540/archive/2011/03/15/6250007.aspx //RT,删除字符串的第一个空格static string DeleteFirstSpace(string yourStr) { string strResult = ""; for (int i = 0; i < yourStr.Length; i++) { if (yourStr[i] == ' ') { if (i == 0) { strResult = yourStr.Substring(i + 1); } else if(i!=yourStr.Length-1) { strResult = yourStr.Substring(0, i) + yourStr.Substring(i + 1); } else if (i == yourStr.Length - 1) { strResult = yourStr.Substring(0, yourStr.Length - 2); } break; } } return strResult; }//测试:Console.WriteLine(DeleteFirstSpace("hello world! sss"));//输出结果为://helloworld! sss 正则上面有人说了我给你个更简单的呵呵:string temp = "<p> hello world ";temp=temp.Replace("<p> ","<p>"); 直接用String对象的Trim()方法不可以吗? str.Replace(' ',''); 就可以了啊 有必要那么复杂么 哈哈,来个正则版 的:string temp = "<p> hello world ";temp = Regex.replace(temp,@"(?<! .*) ") string temp = "<p> hello world "; if (temp .charAt(0) == " ") { //如果字串左边第一个字符为空格 str = str.slice(1);//将空格从字串中去掉 //这一句也可改成 str = str.substring(1, str.length); str = lTrim(str); //递归调用 } 陣列值排序方向轉換 很急,谢谢 'a' 附近有语法错误。 winfom的datagirdview拖动垂直滚动条时候触发什么事件 如何实现在文本里的插入操作?? 怎么让用VS 生成 __doPostBack('','') 函数啊???? 给出解决方案,不仅给分,还可以给钱! 关于System.Text.Encoding.Default.GetBytes的问题 问一个 Matrix的问题 简单问题,请大家帮忙!System.Windows.Forms命名空间不存在?! 请教一个DataGrid的问题, 谢谢帮助 一个数组生成的问题? WinForm程序如何检测自己卡死的情况????
void Main()
{
string temp = "<p> hello world ";
int i=1;
temp=Regex.Replace(temp," ",delegate(Match m){
if(i==1) {i++;return "";}
else{return " ";}
});
Console.WriteLine(temp);//<p> hello world
}论坛签名======================================================================silvertoss:你好!
截至 2011-03-17 10:22:30 前:
你已发帖 13 个, 未结贴 4 个;
结贴率为: 69.23%
当您的问题得到解答后请及时结贴.
http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html如何给分和结贴?
http://community.csdn.net/Help/HelpCenter.htm#结帖如何给自己的回帖中也加上签名?
http://blog.csdn.net/q107770540/archive/2011/03/15/6250007.aspx
//RT,删除字符串的第一个空格
static string DeleteFirstSpace(string yourStr)
{
string strResult = "";
for (int i = 0; i < yourStr.Length; i++)
{
if (yourStr[i] == ' ')
{
if (i == 0)
{
strResult = yourStr.Substring(i + 1);
}
else if(i!=yourStr.Length-1)
{
strResult = yourStr.Substring(0, i) + yourStr.Substring(i + 1);
}
else if (i == yourStr.Length - 1)
{
strResult = yourStr.Substring(0, yourStr.Length - 2);
}
break;
}
}
return strResult;
}//测试:
Console.WriteLine(DeleteFirstSpace("hello world! sss"));
//输出结果为:
//helloworld! sss
我给你个更简单的呵呵:
string temp = "<p> hello world ";
temp=temp.Replace("<p> ","<p>");
temp = Regex.replace(temp,@"(?<! .*) ")
if (temp .charAt(0) == " ")
{
//如果字串左边第一个字符为空格
str = str.slice(1);//将空格从字串中去掉
//这一句也可改成 str = str.substring(1, str.length);
str = lTrim(str); //递归调用
}