字符串的处理 就是78.34 这样的数字不能分开截取的 第一个abcdefgh78.34 那字符个数就13个了 所以就把它移到第二个来 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System.Linq;using System.Text;using System.Text.RegularExpressions;namespace ConsoleApplication1{ class Class1 { public static void Main() { List<string> items = ConvertToList("abcdefgh78.34abcdefgh"); foreach (string item in items) { System.Console.WriteLine(item); } System.Console.ReadLine(); } public static List<string> ConvertToList(string content) { List<string> items = null; if (content.Length > 0) { items = new List<string>(); } string temp = null; string temp2 = null; for (int i = 0; i < content.Length; i++) { temp2 = temp + content[i].ToString(); if (IsCanUse(temp2)) { if (temp2.Length % 10 != 0) { temp += content[i].ToString(); } else { temp += content[i].ToString(); items.Add(temp); temp = temp2 = null; } } else { items.Add(temp); temp = content[i].ToString(); temp2 = null; } } if (temp != null) { items.Add(temp); } return items; } public static bool IsCanUse(string item) { return Regex.Match(item, @"^[a-zA-Z]{1,10}$").Success || Regex.Match(item, @"^[\d]{1,10}$").Success || Regex.Match(item, @"^[\d]{1,10}\.\d{1,9}$").Success || Regex.Match(item, @"^\d\S{1,8}[^\d]$").Success; } }} 运行结果abcdefgh78.34abcdefgh 你把字符串改下ab78.34cdefghabcdefgh 结果就变成 ab 78.34cdefg habcdefgh 了其实我要的结果应该是 ab78.34cde fghabcdefg h String str = "abcdefgh78.34abcdefgh"; Int32 length = str.Length; List<String> list = new List<String>(); while (true) { if(str.Length <= 10) { list.Add(str); break; } else if (str[9] >= '0' && str[9] <= '9') { int i; for (i = 8; i >= 0; i--) { if (!(str[i] >= '0' && str[i] <= '9') && str[i] != '.') break; } if (i == 8) { if (str.Length > 10 && !(str[10] >= '0' && str[10] <= '9') && str[10] != '.') { list.Add(str.Substring(0,10)); str = str.Substring(10); } if (str.Length > 11 && (str[11] >= '0' && str[11] <= '9') && str[10] == '.') { list.Add(str.Substring(0,9)); str = str.Substring(9); } } else { list.Add(str.Substring(0,i + 1)); str = str.Substring(i + 1); } } else if (str[9] == '.') { if (str.Length > 10 && str[8] > '0' && str[8] < '9' && str[10] > '0' && str[10] < '9') { int i; for (i = 7; i >= 0; i--) { if (!(str[i] >= '0' && str[i] <= '9')) break; } list.Add(str.Substring(0, i + 1)); str = str.Substring(i); } else { list.Add(str.Substring(0, 10)); str = str.Substring(10); } } else { list.Add(str.Substring(0, 10)); str = str.Substring(10); } } foreach (String s in list) { Console.WriteLine(s); } 发贴放分: 手工修改 vs2008 方案文件, 工程文件为 2005 用 2:问一个关于Form创建的小问题,在线等待,很快给分。 如何实现高效匹配? C# 关于内存污染的 再放100分,求服装企业ERP原代码。。。。。。 急:一个邮件地址的链接问题 超简单!!这段代码哪儿错了啊???? c#高效有选择性的处理字符串分割的问题 .net framework 类库调试遇到问题 各位帮忙解答一下,输入一个公式,如:(a+b*c)/2,在C#中如何解析进行计算? 怎样在软件上实现刷新功能呢?使用.net工具C#写的 c#关于打印的问题
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Class1
{
public static void Main()
{
List<string> items = ConvertToList("abcdefgh78.34abcdefgh");
foreach (string item in items)
{
System.Console.WriteLine(item);
} System.Console.ReadLine(); } public static List<string> ConvertToList(string content)
{
List<string> items = null; if (content.Length > 0)
{
items = new List<string>();
} string temp = null;
string temp2 = null; for (int i = 0; i < content.Length; i++)
{
temp2 = temp + content[i].ToString();
if (IsCanUse(temp2))
{
if (temp2.Length % 10 != 0)
{
temp += content[i].ToString();
}
else
{
temp += content[i].ToString();
items.Add(temp);
temp = temp2 = null;
}
}
else
{
items.Add(temp);
temp = content[i].ToString();
temp2 = null;
}
}
if (temp != null)
{
items.Add(temp);
} return items;
} public static bool IsCanUse(string item)
{
return
Regex.Match(item, @"^[a-zA-Z]{1,10}$").Success ||
Regex.Match(item, @"^[\d]{1,10}$").Success ||
Regex.Match(item, @"^[\d]{1,10}\.\d{1,9}$").Success ||
Regex.Match(item, @"^\d\S{1,8}[^\d]$").Success;
} }
}
78.34abcde
fgh
你把字符串改下ab78.34cdefghabcdefgh
结果就变成
ab
78.34cdefg
habcdefgh 了其实我要的结果应该是 ab78.34cde
fghabcdefg
h
while (true)
{
if(str.Length <= 10)
{
list.Add(str);
break;
}
else if (str[9] >= '0' && str[9] <= '9')
{
int i;
for (i = 8; i >= 0; i--)
{
if (!(str[i] >= '0' && str[i] <= '9') && str[i] != '.')
break;
} if (i == 8)
{
if (str.Length > 10 && !(str[10] >= '0' && str[10] <= '9') && str[10] != '.')
{
list.Add(str.Substring(0,10));
str = str.Substring(10);
} if (str.Length > 11 && (str[11] >= '0' && str[11] <= '9') && str[10] == '.')
{
list.Add(str.Substring(0,9));
str = str.Substring(9);
}
}
else
{
list.Add(str.Substring(0,i + 1));
str = str.Substring(i + 1);
}
}
else if (str[9] == '.')
{
if (str.Length > 10 && str[8] > '0' && str[8] < '9' && str[10] > '0' && str[10] < '9')
{
int i;
for (i = 7; i >= 0; i--)
{
if (!(str[i] >= '0' && str[i] <= '9'))
break;
} list.Add(str.Substring(0, i + 1));
str = str.Substring(i);
}
else
{
list.Add(str.Substring(0, 10));
str = str.Substring(10);
}
}
else
{
list.Add(str.Substring(0, 10));
str = str.Substring(10);
}
} foreach (String s in list)
{
Console.WriteLine(s);
}