如何分割字符串并设置到表中 [英] how to diveide string and set in to the table

查看:51
本文介绍了如何分割字符串并设置到表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,
实际上
我有1000个字符的字符串...我必须将其切成150个字符的插槽并将其放入表格中...
但一个条件是,在将字符串分成150个字符的插槽时,拼写没有中断....

i have faced one proble,
actualy
i have a string of 1000 character...i have to cut it in to 150 character slot and put it into the table...
but one condition is spelling is not breaking while divide the string into 150 character slot....

推荐答案

这是从许多解决方案中将字符串拆分为和平的一种方法. ..
Here is the one from many solutions to break string into peaces...
public static class StringExtensions
{
	public static IEnumerable<string> SplitInGroups(this String str, int chunkSize)
	{
		if (null == str)
			throw new ArgumentNullException("str", "String can't be null.");
			
		if (chunkSize <= 0)
			throw new ArgumentException("Chunk size must be positive.", "chunkSize");
			
		for (var i = 0; i < str.Length; i += chunkSize)
			yield return str.Substring(i, Math.Min(chunkSize, str.Length - i));
	}
}
</string>



然后,您可以在要中断的任何字符串上使用此扩展方法...



Then you can use this extension method on any string that you want to break...

string yourString ="Some string...";

var stringGroups = yourString.SplitInGroups(150);
foreach(string stringGroup in stringGroups)
{
	// Do something with stringGroup...
}



然后,您可以遍历可枚举的字符串,并希望每个组都想要......



Then you can iterate through enumerable of strings and do want you want with each group...


我假设您的插槽最多为150个字符.那么您就是要放入表中的子字符串,它是最长的一个以空格结尾并且小于或等于150个字符长的字符串.


Piet
I assume you have a slot of max 150 chars. Then you''re substring to put into the table, is the longest one that end with whitespace and is shorter then or equal to 150 chars long.


Piet


这篇关于如何分割字符串并设置到表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆