// Remove String in Column Content public Boolean Remove_String_In_Column_Content(String InputFile, String ColumnName, String StringToRemove) { CsvUtils cu = new CsvUtils(); cu.SetFile(InputFile); int idx = cu.Get_Column_Index(ColumnName); if (idx < 0) { return(false); } foreach (KeyValuePair <int, List <String> > entry in cu.dict) { if (entry.Key > 0) // dont process the column header line { if (idx > -1) { String MyContent = cu.Get_Cell_Content(ColumnName, entry.Key); String NewValue = MyContent.Replace(StringToRemove, ""); cu.Save_Cell_Value_No_Save(ColumnName, entry.Key, NewValue); } } } cu.Save_File_As_CSV(InputFile); return(true); }
// Transforms the content of an entire column (by replacing it with a RegEx MATCH from a regular expression) public String Transform_Column_Content(String InputFile, String ColumnName, String RegExPattern) { CsvUtils cu = new CsvUtils(); cu.SetFile(InputFile); int colIdx = cu.Get_Column_Index(ColumnName); if (colIdx < 0) { return("Column Does Not Exist."); } int NbOfMatches = 0; foreach (KeyValuePair <int, List <String> > entry in cu.dict) { if (entry.Key > 0) // dont process the column header line { String MyContent = cu.Get_Cell_Content(ColumnName, entry.Key); String NewValue = MyContent; var pattern = @RegExPattern; var matches = Regex.Matches(MyContent, pattern); if (matches.Count > 0 && matches[0].Groups.Count > 1) { NewValue = matches[0].Groups[1].Value; NbOfMatches++; } else { } cu.Save_Cell_Value_No_Save(ColumnName, entry.Key, NewValue); } } cu.Save_File_As_CSV(InputFile); if (NbOfMatches == 0) { return("No Match Found or No Regex Group defined in Regular Expression Pattern"); } else { return("Number of Matching Cells Found and transformed: " + NbOfMatches); } }