菜鸟请教字符串处理算法 一不规则字符串(全汉字),要求每30个汉字截断,使用循环输出被截断的每个字符串,若用SUBSTRING则最后一字符串可能出错,要考虑程序运行速度,不允许使用异常处理。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SUBSTRING前加正则表达式判断,不够30位就不要截取了. 先算总长度再算出循环多少次再算最后一个字符串SUBSTRING时应该取多少位?总感觉有点复杂 string str = "该公司的发第三件看法的上飞机千日分年年元角角发挥那可获得起而言和发卡行法高速钢复色光三个权热法机会热和人啊汉卡可让诶我去阿金卡你在那就擦"; string[] strarr = Regex.Replace(str, @"[\u4e00-\u9fa5]{30}", @"$&~").TrimEnd('~').Split('~'); foreach (string s in strarr) Console.WriteLine(s); 楼主的三个问题都是求源码哈! internal static void PrintText30(string text) { do { Console.WriteLine(text.Length > 30 ? text.Substring(0, 30) : text); text = text.Length < 30 ? "": text.Substring(30); } while (text.Length > 0); } internal static void PrintText30(string text) { if (text.Length > 30) { Console.WriteLine(text.Substring(0, 30)); TextDel(text.Substring(30)); } else Console.WriteLine(text); }两个都可以用 internal static void PrintText30(string text) { do { Console.WriteLine(text.Length > 30 ? text.Substring(0, 30) : text); text = text.Length < 30 ? "": text.Substring(30); } while (text.Length > 0); } internal static void PrintText30(string text) { if (text.Length > 30) { Console.WriteLine(text.Substring(0, 30)); PrintText30(text.Substring(30)); } else Console.WriteLine(text); }楼上 弟二个方法内TextDel(text.Substring(30));这里错了 啊哦 晕! 有问题!!要<= >= 要不长度刚好是30倍数的时候 会无限了!! internal static void PrintText30(string text) { do { Console.WriteLine(text.Length >= 30 ? text.Substring(0, 30) : text); text = text.Length <= 30 ? "": text.Substring(30); } while (text.Length > 0); } internal static void PrintText30(string text) { if (text.Length >= 30) { Console.WriteLine(text.Substring(0, 30)); PrintText30(text.Substring(30)); } else Console.WriteLine(text); }不好意思 发太多了! using System;public class Test{ static void Main() { Print("第四纪咖啡馆看三大件敢死队公开赛大将军公司大家根据四大皆空第四纪敢死队"); Console.ReadKey(); } static void Print(string str) { int length=str.Length; while(length>30) { string strvalue = str.Substring(0,30); str = str.Remove(0, 30); length = str.Length; Console.WriteLine(strvalue); } Console.WriteLine(str); }} static void Print(string str) { byte removelength = 30; int length=str.Length; while(length>30) { Console.WriteLine(str.Substring(0, removelength)); str = str.Remove(0, removelength); length -= removelength; } Console.WriteLine(str); } 这样做先遍历替换一次,再遍历分割一次,影响效率,字符串越长,效率越低string str = "该公司的发第三件看法的上飞机千日分年年元角角发挥那可获得起而言和发卡行法高速钢复色光三个权热法机会热和人啊汉卡可让诶我去阿金卡你在那就擦";MatchCollection mc = Regex.Matches(str, @".{30}|.{1,30}$");foreach (Match m in mc){ Console.WriteLine(m.Value);}Console.ReadLine();也可以这样,也是遍历一遍string str = "该公司的发第三件看法的上飞机千日分年年元角角发挥那可获得起而言和发卡行法高速钢复色光三个权热法机会热和人啊汉卡可让诶我去阿金卡你在那就擦";List<string> result = new List<string>();result.Add(Regex.Replace(str, ".{30}", delegate(Match m){ result.Add(m.Value); return "";}));foreach(string s in result){ Console.WriteLine(s);}Console.ReadLine();不用正则也一样简单string str = "该公司的发第三件看法的上飞机千日分年年元角角发挥那可获得起而言和发卡行法高速钢复色光三个权热法机会热和人啊汉卡可让诶我去阿金卡你在那就擦";List<string> result = new List<string>();while (str.Length >= 30){ result.Add(str.Substring(0, 30)); str = str.Substring(30);}result.Add(str);foreach (string s in result){ Console.WriteLine(s);}Console.ReadLine(); 如何用C#发送XML给一个URL? DataGridView Datasource的问题 获取计算机的IP地址? 帮忙给一些C#的手机邮件收发的资料或者源码 Socket心跳包 关于命名控件的问题 关于跨年拆分的问题 关于owc应用的一个问题 一个简单问题。。调试c#中出现得问题。。 菜鸟问题:企业级是什么?什么样的项目才叫企业级?C#是如何实现企业级的项目的? 各位C#/VB高手,能否做到“播放内存中MP3文件中指定时间或帧的某一段”? SQL2000出现SSL安全错误的问题,谁能帮解决~?
string[] strarr = Regex.Replace(str, @"[\u4e00-\u9fa5]{30}", @"$&~").TrimEnd('~').Split('~');
foreach (string s in strarr)
Console.WriteLine(s);
{
do
{
Console.WriteLine(text.Length > 30 ? text.Substring(0, 30) : text);
text = text.Length < 30 ? "": text.Substring(30);
} while (text.Length > 0);
}
internal static void PrintText30(string text)
{
if (text.Length > 30)
{
Console.WriteLine(text.Substring(0, 30));
TextDel(text.Substring(30)); }
else
Console.WriteLine(text);
}两个都可以用
internal static void PrintText30(string text)
{
do
{
Console.WriteLine(text.Length > 30 ? text.Substring(0, 30) : text);
text = text.Length < 30 ? "": text.Substring(30);
} while (text.Length > 0);
}
internal static void PrintText30(string text)
{
if (text.Length > 30)
{
Console.WriteLine(text.Substring(0, 30));
PrintText30(text.Substring(30)); }
else
Console.WriteLine(text);
}
楼上 弟二个方法内
TextDel(text.Substring(30));
这里错了
internal static void PrintText30(string text)
{
do
{
Console.WriteLine(text.Length >= 30 ? text.Substring(0, 30) : text);
text = text.Length <= 30 ? "": text.Substring(30);
} while (text.Length > 0);
}
internal static void PrintText30(string text)
{
if (text.Length >= 30)
{
Console.WriteLine(text.Substring(0, 30));
PrintText30(text.Substring(30)); }
else
Console.WriteLine(text);
}
不好意思 发太多了!
public class Test
{
static void Main()
{
Print("第四纪咖啡馆看三大件敢死队公开赛大将军公司大家根据四大皆空第四纪敢死队");
Console.ReadKey();
} static void Print(string str)
{
int length=str.Length;
while(length>30)
{
string strvalue = str.Substring(0,30);
str = str.Remove(0, 30);
length = str.Length;
Console.WriteLine(strvalue);
}
Console.WriteLine(str);
}
}
{
byte removelength = 30;
int length=str.Length;
while(length>30)
{
Console.WriteLine(str.Substring(0, removelength));
str = str.Remove(0, removelength);
length -= removelength;
}
Console.WriteLine(str);
}
这样做先遍历替换一次,再遍历分割一次,影响效率,字符串越长,效率越低string str = "该公司的发第三件看法的上飞机千日分年年元角角发挥那可获得起而言和发卡行法高速钢复色光三个权热法机会热和人啊汉卡可让诶我去阿金卡你在那就擦";
MatchCollection mc = Regex.Matches(str, @".{30}|.{1,30}$");
foreach (Match m in mc)
{
Console.WriteLine(m.Value);
}
Console.ReadLine();也可以这样,也是遍历一遍
string str = "该公司的发第三件看法的上飞机千日分年年元角角发挥那可获得起而言和发卡行法高速钢复色光三个权热法机会热和人啊汉卡可让诶我去阿金卡你在那就擦";
List<string> result = new List<string>();
result.Add(Regex.Replace(str, ".{30}", delegate(Match m){ result.Add(m.Value); return "";}));
foreach(string s in result)
{
Console.WriteLine(s);
}
Console.ReadLine();不用正则也一样简单
string str = "该公司的发第三件看法的上飞机千日分年年元角角发挥那可获得起而言和发卡行法高速钢复色光三个权热法机会热和人啊汉卡可让诶我去阿金卡你在那就擦";
List<string> result = new List<string>();
while (str.Length >= 30)
{
result.Add(str.Substring(0, 30));
str = str.Substring(30);
}
result.Add(str);
foreach (string s in result)
{
Console.WriteLine(s);
}
Console.ReadLine();