关于字符串分割

xhmbldsh 2008-05-07 08:21:39
str="[111]jdfjf34[drr]4545[erer][9rfj0r]";
要求将以[]为标志的分割字符串
即:分割成[111], [drr],[erer],[9rfj0r]这样4个字串
谢谢
...全文
47 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
用正则

using System;

using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
string subjectString = "[111]jdfjf34[drr]4545[erer][9rfj0r]";
Regex regexObj = new Regex(@"(\[[^\]]*\])");
Match matchResults = regexObj.Match(subjectString);
while (matchResults.Success)
{
// matched text: matchResults.Value
// match start: matchResults.Index
// match length: matchResults.Length
Console.WriteLine(matchResults.Value);
matchResults = matchResults.NextMatch();
}
}
catch (ArgumentException ex)
{
// Syntax error in the regular expression
}


}
}
}
LeoMaya 2008-05-07
  • 打赏
  • 举报
回复

string str = "[111]jdfjf34[drr]4545[erer][9rfj0r]";
string pattern = @"\[(?<region>.*?)\]";
Regex regex = new Regex(pattern);
for (Match match = regex.Match(str); match.Success; match = match.NextMatch())
{
Console.WriteLine(string.Format("[{0}]", match.Groups["region"].Value));
}

110,534

社区成员

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

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

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