C
C#6mo ago
itsnotabbas

Trailing whitespace problem

I'm doing a codewars problem, and i keep getting this error but i dont know how to fix it. Any help? Thanks
using System;
using System.Linq;
using System.Text.RegularExpressions;
public class StripCommentsSolution
{
public static string StripComments(string text, string[] symbols)
{

string[] lines = text.Split(new string[] { "\n" }, StringSplitOptions.None);


string newStr = "";

foreach (var line in lines)
{
int index = -1;
foreach (var letter in line)
{
if (symbols.Contains(letter.ToString()))
{
index = line.IndexOf(letter.ToString());

newStr += line.Substring(0, index).TrimEnd() + "\n";

break;
}
}
if (index == -1)
{
newStr += line.TrimEnd() + "\n";
}
}

newStr = newStr.TrimEnd();

return newStr;
}
}
using System;
using System.Linq;
using System.Text.RegularExpressions;
public class StripCommentsSolution
{
public static string StripComments(string text, string[] symbols)
{

string[] lines = text.Split(new string[] { "\n" }, StringSplitOptions.None);


string newStr = "";

foreach (var line in lines)
{
int index = -1;
foreach (var letter in line)
{
if (symbols.Contains(letter.ToString()))
{
index = line.IndexOf(letter.ToString());

newStr += line.Substring(0, index).TrimEnd() + "\n";

break;
}
}
if (index == -1)
{
newStr += line.TrimEnd() + "\n";
}
}

newStr = newStr.TrimEnd();

return newStr;
}
}
2 Replies
itsnotabbas
itsnotabbasOP6mo ago
Expected: "...\n\n\n\nA\n\nD\n\nAB\n\nC\n\nCE\n\nF\n\nB\n\nA\n\nF\n\n\nF\n\nC\n\nD\n\nC\nFC\n\nC\n\nE\n\n\nF\nE\n\n"
But was: "...\n\n\n\nA\n\nD\n\nAB\n\nC\n\nCE\n\nF\n\nB\n\nA\n\nF\n\n\nF\n\nC\n\nD\n\nC\nFC\n\nC\n\nE\n\n\nF\nE"
Expected: "...\n\n\n\nA\n\nD\n\nAB\n\nC\n\nCE\n\nF\n\nB\n\nA\n\nF\n\n\nF\n\nC\n\nD\n\nC\nFC\n\nC\n\nE\n\n\nF\nE\n\n"
But was: "...\n\n\n\nA\n\nD\n\nAB\n\nC\n\nCE\n\nF\n\nB\n\nA\n\nF\n\n\nF\n\nC\n\nD\n\nC\nFC\n\nC\n\nE\n\n\nF\nE"
ParaLogia
ParaLogia6mo ago
do you need the last newStr = newStr.TrimEnd();? Also it would be helpful to see the actual problem statement for context
Want results from more Discord servers?
Add your server