maybe using the latest technologies, in 3.5 SP1, is there a way to short this?
// Bruno: add the empty row with column names // -- select [CRM5].[USERGROUP].[UserGroup_id], [CRM5].[USERGROUP].[name] AS [name] if (col.ToLower().Contains('] as [')) { // [CRM5].[USERGROUP].[name] AS [name] splitColumn.Clear(); // clear the list splitColumn.AddRange(col.Trim().Split(' ')); emptyRow.Append(String.Format(' '' AS {0},', splitColumn[splitColumn.Count - 1])); } else { // [CRM5].[USERGROUP].[UserGroup_id] splitColumn.Clear(); // clear the list splitColumn.AddRange(col.Trim().Split('.')); emptyRow.Append(String.Format(' '' AS {0},', splitColumn[splitColumn.Count - 1]));
}
In other words, is there a way to retrieve the last element of a String when split?
maybe
col.Trim().Split('.').Find(x => x.LastItem);
col is a String
splitColumn is a List < String >
Assuming that you’ve imported System.Linq:
To elaborating the above code snippet… this do the trick:
Trim() is unnecessary if you specify StringSplitOptions.RemoveEmptyEntries. The above code will not throw if the string is null or empty.