Resolved Splitting an array and attempting to remove newline

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
I wanted to create a list that had predetermined prefixes, suffixes and numbering applied to a list of items.

I created 2 PreFix boxes and 2 Suffix boxes.

I then put the results together with this code:
C#:
List<string> prefixes = new List<string>();
List<string> suffixes = new List<string>();

for (var i = 0; i < 4; i++)
{
    var controlNumber = i + 1;

    if (Controls[$"Suffix{controlNumber}"].Text != "")
    {
        suffixes.Add(Controls[$"Suffix{controlNumber}"].Text.ToString());
    }
}
I then put the results into a string and trim them:
C#:
string outputPrefixes = string.Join("", prefixes);
string PreFixResult = outputPrefixes.Trim('\n');
string outputSuffixes = string.Join("", suffixes);
string SuffixResult = outputSuffixes.Trim('\n');
I then put in a list of items separated by \n and put the prefix before the item and the suffix after the item using this code - I also number the items 001, 002 etc:
C#:
string[] inputNames;

if (nameInputTB.Text != "")
{
    inputNames = nameInputTB.Text.Split('\n');

    foreach (string item in inputNames)
    {
        j = j + 1;
        string nameResult = item.Trim('\n');

        outputbox.Text += j.ToString("D3") + " " + PreFixResult + " " + nameResult;
        outputbox.Text += j.ToString("D3") + " " + PreFixResult + " " + nameResult + " " + SuffixResult;
    }
}
When I copy this into an excel, the "SuffixResult" appears at the start of the next item.

So if my PreFix is Q. and MN and my Suffix is Hold and my items are Apple, Orange, Pear I get this:

001 Q. MN Apple
001 Q. MN Apple
Hold002. Q. MN Orange
002 Q. MN Orange
Hold003 Q. MN Pear
003 Q. MN Pear
Hold

You can see I am throwing around a few trims, but it doesn't seem to get rid of the '\n' from the inputNames variable.

Just wondered if anyone had encountered similar or had any knowledge to impart about how to deal with it?
 
Last edited by a moderator:
A Windows line break is a pair of characters - one carriage return and one line feed. You are only addressing line feeds in your code so you may well be leaving carriage returns in your data. You should use "\r\n" when splitting and trimming and see whether that addresses your issue.
 
By the way, this seems kinda silly:
C#:
for (var i = 0; i < 4; i++)
{
    var controlNumber = i + 1;
Why not just do this instead:
C#:
for (var i = 1; i < 5; i++)
{
and then use i in the loop?

Also, there's not much point calling ToString on the Text property of a control when it is already type string. It's not going to hurt but it shows that you're not really paying close attention to your data types.

Also, it's generally considered poor form to repeat the same complex expression multiple times in your code. You should generally evaluate the expression once and store the result, then use that, e.g.
C#:
var controlText = Controls[$"Suffix{controlNumber}"].Text;

if (controlText != string.Empty)
{
    suffixes.Add(controlText);
}
and:
C#:
var str = j.ToString("D3") + " " + PreFixResult + " " + nameResult;

outputbox.Text += str;
outputbox.Text += str + " " + SuffixResult;
Finally, I would recommend that you start using string interpolation instead of concatenation when there's more than two substrings as it tends to improve readability, e.g.
C#:
var str = $"{j:D3} {PreFixResult} {nameResult}";
You were already using string interpolation elsewhere in your code but probably didn't realise it because you copied and pasted it from elsewhere.
 
Back
Top Bottom