Click here to Skip to main content
16,019,263 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have code that is taking in a DataTable, converting each DataRow into a string, and then removing certain symbols ("|", "~", etc). However, my code is not removing these correctly. Strangely enough however, when I remove the underscore "_" from my charToRemove array, it does remove the pipes correctly.

Image with Pipes (Does not have underscore in charToRemove array)[^]

Image with Pipes Not Removed (has underscore in charToRemove array)[^]

What I have tried:

public static List<string> CleanMainLogTable(string liteConString)
{
    string[] charToRemove = new string[] { "*", "~", "|" , "_" };
    string cleanedEntry = "";
    SQLiteConnection liteCon = new SQLiteConnection(liteConString);
    liteCon.Open();
    DataTable dtbl = new DataTable();

    string select = "SELECT LogMessage FROM MainLog_Table";

    SQLiteCommand cmd = new SQLiteCommand(select, liteCon);
    SQLiteDataAdapter sqliteDAP = new SQLiteDataAdapter(cmd);
    sqliteDAP.Fill(dtbl);

    List<string> table = dtbl.AsEnumerable().Select(r => r.Field<string>(0)).ToList();
    int i = 0;

    foreach (string entry in table)
    {
        foreach(string c in charToRemove)
        {
            cleanedEntry = entry.Replace(c, string.Empty);
        }
        Console.WriteLine(i + " " + cleanedEntry);
        i++;
    }
    return null;
}
Posted
Updated 21-Sep-20 6:19am

Step through your code in the debugger and you should see what is going on. In your outer foreach "entry" will be something in your datatable, let's say "∗test". Your inner foreach declares "cleanedEntry" to be "entry" with "*" removed so cleanedEntry becomes "test" but entry is still "∗test". The next loop will remove any "~" in "entry", but entry is still "*test" so cleanedEntry is now "∗test" and so on. What you want to do is all the removals on the same string variable

C#
foreach (string entry in table)
{
    // define cleanedEntry to be the original text
    cleanedEntry = entry;

    foreach (string c in charToRemove)
    {
        // remove the char from cleanedEntry
        cleanedEntry = cleanedEntry.Replace(c, string.Empty);
    }
    Console.WriteLine(i + " " + cleanedEntry);
    i++;
}


That probably gives you another problem though, your orignal "table" List will remain unchanged as when you enumerate through the values you get a copy of the value rather than a reference to the value so your removal is done on a copy of the string in the List, not the List itself so that stays unchanged. To update the actual List;

C#
List<string> table = dtbl.AsEnumerable().Select(r => r.Field<string>(0)).ToList();

for(int i = 0; i < table.Count; i++)
{
    foreach (string c in charToRemove)
    {
        table[i] = table[i].Replace(c, string.Empty);
    }
    Console.WriteLine(i + " " + table[i]);
}
 
Share this answer
 
v2
C#
foreach (string entry in table)
{
    foreach(string c in charToRemove)
    {
        cleanedEntry = entry.Replace(c, string.Empty);
    }
    Console.WriteLine(i + " " + cleanedEntry);
    i++;
}

Each time you call replace, you use the original string as the source, and the cleanedEntry string as the destination. So the final result will be the original string with the underscores removed.

Try:
C#
foreach (string entry in table)
{
    cleanedEntry = entry;
    foreach(string c in charToRemove)
    {
        cleanedEntry = cleanedEntry.Replace(c, string.Empty);
    }
    Console.WriteLine(i + " " + cleanedEntry);
    i++;
}
 
Share this answer
 
Simple: your inner loop processes the same input string each time.
Change it to this:
C#
foreach (string entry in table)
    {
    cleanedEntry = entry;
    foreach (string c in charToRemove)
        {
        cleanedEntry = cleanedEntry.Replace(c, string.Empty);
        }
    Console.WriteLine(i + " " + cleanedEntry);
    i++;
    }
And it'll work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900