snippetcsharpMinor
Convert string to multiline text
Viewed 0 times
converttextmultilinestring
Problem
I made this method that takes any string and transforms it into a multiline text l; each line having the max (character) length specified by the
How can I make this better, functionality and code wise?
There is currently one known issue. It sometimes adds an empty line at the end because I have to add the
```
public static string ConvertToMultiLineText(string value, int rowLength)
{
var multiLine = new StringBuilder();
string[] lines = value.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
var filterLines = lines.Where(line => !line.IsNullEmptyOrWhiteSpace());
foreach (var line in filterLines)
{
if (line.IsNullEmptyOrWhiteSpace())
continue;
if (line.Length > rowLength)
{
int startIndex = 0;
var number = (decimal)line.Length / rowLength;
var rowNumber = Math.Ceiling(number);
for (int i = 0; i -1)
{
var lastOccurence = (lastSpace <= startIndex) ? startIndex - lastSpace : lastSpace - startIndex;
if (i == rowNumber - 1)
multiLine.Append(line.Substring(startIndex, lastOccurence) + Environment.NewLine + line.Substring(lastSpace, line.Length - lastSpace));
else
multiLine.Append(line.Substring(startIndex, lastOccurence) + Environment.NewLine);
startIndex = lastSpace;
}
else
{
if (i == rowNumber - 1)
multiLine.Append(line.Substring(startIndex, (line.Length - rowLength * ((int)rowNumber - 1))));
else
multiLine.Append(line.Substring(startIndex, rowLength) + Environment.NewLine);
startIndex += rowLengt
rowLength parameter.How can I make this better, functionality and code wise?
There is currently one known issue. It sometimes adds an empty line at the end because I have to add the
Enviroment.NewLine call. Check it out, please, and give some suggestions. ```
public static string ConvertToMultiLineText(string value, int rowLength)
{
var multiLine = new StringBuilder();
string[] lines = value.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
var filterLines = lines.Where(line => !line.IsNullEmptyOrWhiteSpace());
foreach (var line in filterLines)
{
if (line.IsNullEmptyOrWhiteSpace())
continue;
if (line.Length > rowLength)
{
int startIndex = 0;
var number = (decimal)line.Length / rowLength;
var rowNumber = Math.Ceiling(number);
for (int i = 0; i -1)
{
var lastOccurence = (lastSpace <= startIndex) ? startIndex - lastSpace : lastSpace - startIndex;
if (i == rowNumber - 1)
multiLine.Append(line.Substring(startIndex, lastOccurence) + Environment.NewLine + line.Substring(lastSpace, line.Length - lastSpace));
else
multiLine.Append(line.Substring(startIndex, lastOccurence) + Environment.NewLine);
startIndex = lastSpace;
}
else
{
if (i == rowNumber - 1)
multiLine.Append(line.Substring(startIndex, (line.Length - rowLength * ((int)rowNumber - 1))));
else
multiLine.Append(line.Substring(startIndex, rowLength) + Environment.NewLine);
startIndex += rowLengt
Solution
First Impressions
I haven't gone through the functionality in detail but a few things strike me immediately.
Why explicitly include
The check for an empty 'line' inside the loop
should be redundant as we have previously filtered the lines
I would need to do up some performance checks to be sure but I reckon that the use of the StringBuilder (presumably for performance reasons) will be negated by the calls to
I can't tell for certain what the requirements are - I would guess
Max line Length of n
but it look like a linear progression through the string, with some back tracking to deal with split words/spaces, might work and appending the individual chars might give you better performance.
The final return should be
there is no need to create another string and place the result of multiLine.ToString() inside it and then return the anonymous string.
I haven't gone through the functionality in detail but a few things strike me immediately.
Why explicitly include
StringSplitOptions.None. They default value is none.IsNullEmptyOrWhiteSpace() seems to be a (not included) extension method for string that performs the same function as String.IsNullOrWhiteSpace. Does it do something else as well?The check for an empty 'line' inside the loop
if (line.IsNullEmptyOrWhiteSpace())
continue;should be redundant as we have previously filtered the lines
var filterLines = lines.Where(line => !line.IsNullEmptyOrWhiteSpace());I would need to do up some performance checks to be sure but I reckon that the use of the StringBuilder (presumably for performance reasons) will be negated by the calls to
line.SubString(). I can't tell for certain what the requirements are - I would guess
Max line Length of n
- Do not split words
- Do not end on a space
- Do not start on a space
but it look like a linear progression through the string, with some back tracking to deal with split words/spaces, might work and appending the individual chars might give you better performance.
The final return should be
return multiLine.ToString();there is no need to create another string and place the result of multiLine.ToString() inside it and then return the anonymous string.
Code Snippets
if (line.IsNullEmptyOrWhiteSpace())
continue;var filterLines = lines.Where(line => !line.IsNullEmptyOrWhiteSpace());return multiLine.ToString();Context
StackExchange Code Review Q#54697, answer score: 7
Revisions (0)
No revisions yet.