字符串问题!!!

sangengyi 2004-03-14 09:09:37
请问如何去处字符串中的空格和标点符号?
如去处"sdhj sdkf ??;' fgd;' ! df"中的空格及标点符号
...全文
46 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Aallonlin 2004-03-15
  • 打赏
  • 举报
回复
楼主的意思是不是只要取出里面的字符啊?
是的话,,

string strTemp = YourString;
string strResult = "";//保存结果
for(int i=0;i<urString.length;i++)
{

if(strTemp[i] > 'a' and strTemp[i] < 'z' || strTemp[i] > 'A' and strTemp[i] < 'Z'))
strResult += strTemp[i];
}
changqi 2004-03-14
  • 打赏
  • 举报
回复
我觉得你这个问题,可以用strng.split 方法先把空格和标点以外的字符取出来,再进行合并,这样要简单些。
如下:
using System;

class Class1
{
static void Main(string[] args)
{
string s1="sdhj sdkf ??;' fgd;' ! df";
string delimStr=" ?;'!";
char[] delimter=delimStr.ToCharArray();
string[] split =null;
split=s1.Split(delimter);
string s2=null;
foreach(string str in split)
{
s2=s2+str;
}
Console.WriteLine(s2);
}
}
daosh 2004-03-14
  • 打赏
  • 举报
回复
觉得大概可以这么做
string strTemp = urString.tolower();
string strResult = "";//保存结果
for(int i=0;i<urString.length;i++)
{

if(strTemp[i] > 'a' and strTemp[i] < 'z')
strResult += strTemp[i];
}
sangengyi 2004-03-14
  • 打赏
  • 举报
回复
还没看到正则表达式那一章,有没有别的方法?
showtimenow 2004-03-14
  • 打赏
  • 举报
回复
用正则表达式匹配@"\s+"

代码如下,已经调试过

string cutText = 预设值;
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\s+");
System.Text.RegularExpressions.MatchCollection match = regex.Matches(cutText);
int stringLen = match.Count+1;
string[] s = new string [stringLen];
int len = 0;
for(int i=0;i<match.Count;i++)
{
len = i== 0?match[i].Index - len:match[i].Index - match[i-1].Index - match[i-1].Value.Length;
s[i] = cutText.Substring(0,len);
len = len +match[i].Value.Length;
cutText = cutText.Substring(len);

}
s[stringLen-1] = cutText;

结果在 s[] 里面
智商无下限 2004-03-14
  • 打赏
  • 举报
回复
例子就不给你了,附带MSDN里有和这个类似的例子
智商无下限 2004-03-14
  • 打赏
  • 举报
回复
使用正则表达式

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧