Reputation: 23
I need to read 70 character chunks and send to a terminal emulator. I'm really not sure how to do this when substring length, cannot be greater amount of data in string (microsoft generates error). The last line in the textbox is always less than 70.
Does anyone know of a better way to do this?
TextBox can accept 1000 characters with word wrap turned on.
int startIndex = 0;
int slength = 70;
for (int i = 0; i < body.Text.Length; i += 70)
{
if(body.Text.Length < 70)
{
String substring1 = body.Text.Substring(startIndex, body.Text.Length);
CoreHelper.Instance.SendHostCommand("¤:5H¤NCCRMKS / " + body.Text, UIHelper.Instance.CurrentTEControl, true, true);
};
if (body.Text.Length > 70)
{
String substring1 = body.Text.Substring(startIndex, slength);
CoreHelper.Instance.SendHostCommand("¤:5H¤NCCRMKS / " + body.Text, UIHelper.Instance.CurrentTEControl, true, true);
};
}
Upvotes: 2
Views: 1241
Reputation: 31721
I found that traditional splitting is unnatural, I want the smarts to split on whitespace between words, not characters. So if you want to split on words before a specific demarcation size, use this extension:
/// <summary>Use this method like string.Split but to split on a word and not a character.
/// Like WordWrap in that no words will be split down the middle; only split on whitespace..</summary>
/// Note if the a word is longer than the maxcharacters it will be trimmed from the start.
/// <param name="initial">The string to parse.</param>
/// <param name="MaxCharacters">The maximum size of each result line.</param>
/// <remarks>This function will remove some white space at the end of a line, but it allow for a blank line to be contained.</remarks>
///
/// <returns>An array of strings split at the demarcation size.</returns>
public static List<string> SplitOn( this string initial, int MaxCharacters )
{
List<string> lines = new List<string>();
if ( string.IsNullOrEmpty( initial ) == false )
{
string targetGroup = "Line";
string pattern = string.Format( @"(?<{0}>.{{1,{1}}})(?:\W|$)", targetGroup, MaxCharacters );
lines = Regex.Matches(initial, pattern, RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.Compiled)
.OfType<Match>()
.Select(mt => mt.Groups[targetGroup].Value)
.ToList();
}
return lines;
}
Usage
var text = @"
The rain in spain
falls mainly on the
plain of Jabberwocky
falls.";
foreach ( string line in text.SplitOn( 11 ) )
Console.WriteLine( line );
/* Result
The rain in
spain falls
mainly on
the plain
of
Jabberwocky
falls.
*/
This is from my blog article C#: String Extension SplitOn to Split Text into Specific Sizes « OmegaMan's Musings
Upvotes: 2
Reputation: 30052
Method 1 (Traditional SubString) - The fastest:
string str = "123456789";
int currentIndex = 0;
int pageSize = 7;
List<string> results = new List<string>();
while(true)
{
int length = Math.Min(pageSize, str.Length - currentIndex);
string subStr = str.Substring(currentIndex, length);
results.Add(subStr);
if (currentIndex + pageSize >= str.Length - 1)
break;
currentIndex += pageSize;
}
Method 2 (Linq):
A combination of Linq's Skip and Take does the trick also. This is called Paging:
String str = "123456789";
int page = 0;
int pageSize = 7; // change this to 70 in your case
while(true)
{
string subStr = new string(str.Skip(page * pageSize).Take(pageSize).ToArray());
Console.WriteLine(subStr);
page++;
if (page * pageSize >= str.Length)
break;
}
Prints:
1234567
89
Upvotes: 2
Reputation: 16623
This Substring overload accepts start index and length. You are passing length of body.Text.Length for the second (length) argument, which is likely the length of the entire string. You should pass body.Text.Length - startIndex
.
Upvotes: 0