Reputation: 5044
I have a string which has say 3000 characters, now I want to split the same into array, where each index holding not more than 500 characters, I am not getting the logic for the same. I tried the below one but its not working..
say string arr holding 3000 characters...
I am using a loop to get the characters length of 500
here i am storing the value returned by the loop in an array...
say
ArrayList ar = new ArrayList();
for(int i=0; i < arr.Length; i+=500)
{
ar.Add(arr.Substring(i,500));
}
Response.Write(ar[0].ToString());
but this throws an error message saying
Index and length must refer to a location within the string.
Please reply with the appropriate code for the same, any reply would be greatly appreciated.
Upvotes: 2
Views: 2804
Reputation: 5044
hey guys i have got an easy solution for the same, as said by "J. Vermeire", that the my string never has 500 characters in the end, it might be less than 300, thats why the application is throwing an error..
so applying that logic i made a simple loop which could check whats the current length of the string and accordingly set the output.... here we go..
here we assume string str contains 34905 characters.
for(int i=0; i < str.Length; i += 500)
{
if (str.Length > i+500)
{
Response.Write(str.Substring(i, 500));
}
else
{
Response.Write(str.Substring(i, str.Length-i));
}
}
This has done my task.. Thanks "J. Vermeire" for giving your logic, its all your credit... Thanks a lot.
Upvotes: 0
Reputation: 41539
Couple of points:
you might need to test "i < arr.length - 1" as when you reach end the last index of the string is 2999 and the length is 3000.
You also might need to generalize the end point as you're assuming that the string is whole numbers of 500 long. If your string is 2900 long then the last string should be arr.Substring(i,400).
Maybe try
for(int i=0; i < arr.Length; i+=500)
{
ar.Add(arr.Substring(i, Math.Min(arr.length - i,500)));
}
Upvotes: 7
Reputation: 158299
In addition to the given answers, here is an alternative approach:
List<string> ar = new List<string>();
int charcount = 500;
int index = 0;
do
{
ar.Add(new string(input.Skip(index).Take(charcount).ToArray()));
index += charcount;
} while (index < input.Length);
First, it uses List<string>
instead of ArrayList
(ArrayList
is not type-safe in the same manner and should basically not be used; it's still around mostly for backwards compatibility reasons).
Second, since String
implements IEnumerable<char>
this opens up for using LINQ extensions. So I use Skip
and Take
to slice up the string into 500-character arrays that are turned into strings and added to the list.
Upvotes: 0
Reputation: 2191
string str = "Sample Text";
List<string> lst = new List<string>();
while (str.Length > 500)
{
var temp = str.Substring(0, 500);
str = str.Remove(0, temp.Length);
lst.Add(temp);
}
lst.Add(str);
string[] arrayString = lst.ToArray();
Console.ReadLine();
Upvotes: 0
Reputation: 505
Try this...
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
string str = string.Empty;
for (int i = 0; i < 3001; i++)
sb.Append("a");
str = sb.ToString();
ArrayList arrayList = new ArrayList();
int length = str.Length;
int size = 500;
int start = 0;
while (length > 0)
{
if (length >= size)
arrayList.Add(str.Substring(start, size));
else
arrayList.Add(str.Substring(start, length));
start += size;
length -= size;
}
Console.Write(arrayList[0].ToString());
}
Upvotes: 0
Reputation: 3299
The problem is that your arr-string isn't always 500 long. If you are getting to the end of it, it might only be 300 chars long. Trying to substring 500 chars out of it will result in an out of range error.
You should check the length of your arr when you substring it. If it's shorter than your 500-charlength strings you should just take the whole string instead of trying to substring it further.
Upvotes: 2
Reputation: 5719
Try changing to
for(int i=0; i < arr.Length; i+=500)
{
int len=500;
if(arr.Length<i+500)
{
len=arr.Length;
}
ar.Add(arr.Substring(i,len));
}
Upvotes: 0
Reputation: 4699
Change to
for(int i=0; i < arr.Length-1; i+=500)
{
ar.Add(arr.Substring(i,500));
}
Upvotes: 0