List<string> names = "Tom,Scott,Bob".Split(',').Reverse().ToList();
This one works.
Try this:
List<string> names = new List<string>("Tom,Scott,Bob".Split(','));
names.Reverse();
If you are trying to
following should work:
string str = "Tom Cruise, Scott, ,Bob | at";
IEnumerable<string> names = str
.Split(new char[]{',', '|'})
.Where(x=>x!=null && x.Trim().Length > 0)
.Select(x=>x.Trim());
Output
Now you can obviously reverse the order as others suggested.
What your missing here is that .Reverse() is a void method. It's not possible to assign the result of .Reverse() to a variable. You can however alter the order to use Enumerable.Reverse() and get your result
var x = "Tom,Scott,Bob".Split(',').Reverse().ToList<string>()
The difference is that Enumerable.Reverse() returns an IEnumerable<T> instead of being void return
List<string> names = "Tom,Scott,Bob".Split(',').Reverse().ToList();
This one works.
Try this:
List<string> names = new List<string>("Tom,Scott,Bob".Split(','));
names.Reverse();
What your missing here is that .Reverse() is a void method. It's not possible to assign the result of .Reverse() to a variable. You can however alter the order to use Enumerable.Reverse() and get your result
var x = "Tom,Scott,Bob".Split(',').Reverse().ToList<string>()
The difference is that Enumerable.Reverse() returns an IEnumerable<T> instead of being void return
Try this:
List<string> names = new List<string>("Tom,Scott,Bob".Split(','));
names.Reverse();
List<string> names = "Tom,Scott,Bob".Split(',').Reverse().ToList();
This one works.
I realize that this question is quite old, but I had a similar problem, except my string had spaces included in it. For those that need to know how to separate a string with more than just commas:
string str = "Tom, Scott, Bob";
IList<string> names = str.Split(new string[] {","," "},
StringSplitOptions.RemoveEmptyEntries);
The StringSplitOptions removes the records that would only be a space char...
List<string> names = "Tom,Scott,Bob".Split(',').Reverse().ToList();
This one works.
Try this:
List<string> names = new List<string>("Tom,Scott,Bob".Split(','));
names.Reverse();
If you are trying to
following should work:
string str = "Tom Cruise, Scott, ,Bob | at";
IEnumerable<string> names = str
.Split(new char[]{',', '|'})
.Where(x=>x!=null && x.Trim().Length > 0)
.Select(x=>x.Trim());
Output
Now you can obviously reverse the order as others suggested.
What your missing here is that .Reverse() is a void method. It's not possible to assign the result of .Reverse() to a variable. You can however alter the order to use Enumerable.Reverse() and get your result
var x = "Tom,Scott,Bob".Split(',').Reverse().ToList<string>()
The difference is that Enumerable.Reverse() returns an IEnumerable<T> instead of being void return
I realize that this question is quite old, but I had a similar problem, except my string had spaces included in it. For those that need to know how to separate a string with more than just commas:
string str = "Tom, Scott, Bob";
IList<string> names = str.Split(new string[] {","," "},
StringSplitOptions.RemoveEmptyEntries);
The StringSplitOptions removes the records that would only be a space char...
Source: Stackoverflow.com