Hi guys,
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:
var lastOrNull = "".Split(' ').LastOrDefault();To elaborating the above code snippet... this do the trick:
col = (col ?? "").Split(new char[] { ' ' }, int.MaxValue, StringSplitOptions.RemoveEmptyEntries) .LastOrDefault(); if (col != null) { emptyRow.Append(String.Format(" '' AS {0},", col); }Trim() is unnecessary if you specify StringSplitOptions.RemoveEmptyEntries. The above code will not throw if the string is null or empty.
-
Try this:
string last = col.Split('.').Last(); -
If you do not need the split result array for other reasons, I would consider using col.lastindexof('.') in combination with the substring method.
Due to the -1 that is return if the '.' is not found:
col.substring(col.lastindexof('.')+1) -
Try this.
This should also make it so you don't have to use a trim before the split.
string col = "select [CRM5].[USERGROUP].[UserGroup_id], [CRM5].[USERGROUP].[name] AS [name]"; if (col.ToLower().Contains("] as [")) { string lastObj = col.Trim().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).Last(); }
0 comments:
Post a Comment