正则表达式分割指定长度的字符串
现在有一字符串比如"IBM发布基于人脑特性设计的全新计算架构和编程语言";怎么通过正则表达式得到一组为5个字符长的字符串数组,实例结果为:
"IBM发布",
"基于人脑特",
"性设计的全",
"新计算架构",
"和编程语言"。
望解析...谢谢 正则表达式
[解决办法]
正则我也不会 帮你顶一下吧
[解决办法]
var value = Regex.Matches("IBM发布基于人脑特性设计的全新计算架构和编程语言", @"\w{5}");
using System;
public class SubStringTest
{
public static void Main()
{
string[] info = { "Name: Felica Walker", "Title: Mz.", "Age: 47", "Location: Paris", "Gender: F" };
int found = 0;
Console.WriteLine("The initial values in the array are:");
foreach (string s in info)
Console.WriteLine(s);
Console.WriteLine("{0}We want to retrieve only the key information. That is:", Environment.NewLine);
foreach (string s in info)
{
found = s.IndexOf(":");
Console.WriteLine(s.Substring(found + 1).Trim());
}
}
}
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System.Text.RegularExpressions;
namespace cop
{
class HelloWorld
{
public static void Main(String[] args)
{
string test = "123abc";
Regex reg = new Regex(@"[a-z]", RegexOptions.IgnoreCase);
string str = "";
MatchCollection mc = reg.Matches(test);
foreach (Match m in mc)
{
str += m.Value + "\n";
}
Console.WriteLine(str);
/*
*输出结果 a b c
*/
Console.ReadKey();
}
}
}
string str="IBM发布基于人脑特性设计的全新计算架构和编程语言";
var result=str.ToCharArray().GroupBy(x=>str.IndexOf(x)/5).Select(x=>new string(x.ToArray()));
string str="IBM发布基于人脑特性设计的全新计算架构和编程语言";
var result=str.ToCharArray().Select((x,i)=>new {x,i}).GroupBy(x=>x.i/5).Select(g=>new string(g.Select(s=>s.x).ToArray()));