split a string with max character limit
I'm attempting to split a string into many strings (List) with each one having a maximum limit of characters. So say if I had a string of 500 characters, and I want each string to have a max of 75, there would be 7 strings, and the last one would not have a full 75.
I've tried some of the examples I have foun开发者_开发百科d on stackoverflow, but they 'truncate' the results. Any ideas?
You can write your own extension method to do something like that
static class StringExtensions
{
public static IEnumerable<string> SplitOnLength(this string input, int length)
{
int index = 0;
while (index < input.Length)
{
if (index + length < input.Length)
yield return input.Substring(index, length);
else
yield return input.Substring(index);
index += length;
}
}
}
And then you could call it like this
string temp = new string('@', 500);
string[] array = temp.SplitOnLength(75).ToArray();
foreach (string x in array)
Console.WriteLine(x);
I think this is a little cleaner than the other answers:
public static IEnumerable<string> SplitByLength(string s, int length)
{
while (s.Length > length)
{
yield return s.Substring(0, length);
s = s.Substring(length);
}
if (s.Length > 0) yield return s;
}
I would tackle this with a loop using C# String.Substring method.
Note that this isn't exact code, but you get the idea.
var myString = "hello world";
List<string> list = new List();
int maxSize
while(index < myString.Length())
{
if(index + maxSize > myString.Length())
{
// handle last case
list.Add(myString.Substring(index));
break;
}
else
{
list.Add(myString.Substring(index,maxSize));
index+= maxSize;
}
}
When you say split, are you referring to the split function? If not, something like this will work:
List<string> list = new List<string>();
string s = "";
int num = 75;
while (s.Length > 0)
{
list.Add(s.Substring(0, num));
s = s.Remove(0, num);
}
i am assuming maybe a delimiter - like space character.
search on the string (instr) until you find the next position of the delimiter.
if that is < your substring length (75) then append to the current substring.
if not, start a new substring.
special case - if there is no delimiter in the entire substring - then you need to define what happens - like add a '-' then continue.
public static string SplitByLength(string s, int length)
{
ArrayList sArrReturn = new ArrayList();
String[] sArr = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string sconcat in sArr)
{
if (((String.Join(" ", sArrReturn.ToArray()).Length + sconcat.Length)+1) < length)
sArrReturn.Add(sconcat);
else
break;
}
return String.Join(" ", sArrReturn.ToArray());
}
public static string SplitByLengthOld(string s, int length)
{
try
{
string sret = string.Empty;
String[] sArr = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string sconcat in sArr)
{
if ((sret.Length + sconcat.Length + 1) < length)
sret = string.Format("{0}{1}{2}", sret, string.IsNullOrEmpty(sret) ? string.Empty : " ", sconcat);
}
return sret;
}
catch
{
return string.Empty;
}
}
精彩评论