【问题标题】:How to get first and last values from list<string>?如何从 list<string> 中获取第一个和最后一个值?
【发布时间】:2013-09-24 12:14:56
【问题描述】:

我只想从List&lt;string&gt; 获取first 和last 值。

List<String> _ids = ids.Split(',').ToList();

上面的代码给了我所有, 分隔值

(aaa,bbb,ccc,ddd,)

我只需要获取并显示第一个和最后一个值,我该怎么做?

output  aaa,ddd

我尝试使用first 和last,但我想消除字符串末尾的, :(

【问题讨论】:

  • 我猜你还没有为此尝试任何东西。
  • 以逗号分隔时,末尾不应有逗号。如果您认为有,那一定是因为您没有向我们展示某些代码。请发布一个可编译的示例,在末尾给出额外的逗号。
  • 我同意 Matthew Watson 的观点,输入字符串的末尾是否有空格。你如何测试字符串值。输出来自哪里(你在做string.Join)?
  • , 即将结束,因为当我发送参数时,我在每个参数后添加,,以便在.cs 文件中它会出现:(
  • ashuthinks,你为什么不能只使用string.Join,它不会添加最后一个,

标签: c# asp.net-mvc c#-4.0


【解决方案1】:

您可以将List&lt;string&gt; 用作数组;

List<string> _ids = new List<string>() { "aaa", "bbb", "ccc", "ddd" };
var first = _ids[0]; //first element
var last = _ids[_ids.Count - 1]; //last element

使用 LINQ,您可以使用 Enumerable.First 和 Enumerable.Last 方法。

List<string> _ids = new List<string>() { "aaa", "bbb", "ccc", "ddd" };
var first = _ids.First();
var last = _ids.Last();
Console.WriteLine(first);
Console.WriteLine(last);

输出将是;

aaa
ddd

这里是 DEMO。

注意:正如 Alexander Simonov pointed,如果您的 List&lt;string&gt; 为空,First() 和 Last() 将抛出异常。注意FirstOrDefault() 或.LastOrDefault() 方法。

【讨论】:

  • 奇怪的是,你使用_ids[_ids.Count - 1];而不是_ids.Last()有什么原因吗?
  • 其实.First()和.Last()可能会在_ids列表为空时抛出异常。所以最好使用.FirstOrDefault()和.LastOrDefault()。
  • @AlexanderSimonov 或先检查边界。
  • 完全正确,但我可以用其他方式代替 linq,因为我想消除它,它位于字符串的末尾;(
  • 我的问题是:为什么 OP 说他最后得到了一个“,”?
【解决方案2】:

简单的答案是使用 Linq

string[] idsTemp = ids.Split(',');
List<string> _ids = new List<string> { {idsTemp.First()}, {idsTemp.Last()}};

您可能需要更复杂一点,因为如果长度为 0,则会引发异常,如果长度为 1,则返回相同的值两次。

public static class StringHelper {
  public List<string> GetFirstLast(this string ids) {
    string[] idsTemp = ids.Split(',');
    if (idsTemp.Length == 0) return new List<string>();
    return (idsTemp.Length > 2) ?
       new List<string> {{ idsTemp.First() }, { idsTemp.Last() }} :
       new List<string> {{ idsTemp.First() }};
  }
}

然后您可以使用此扩展方法。

List<string> firstLast = ids.GetFirstLast();

编辑 - 非 Linq 版本

public static class StringHelper {
  public List<string> GetFirstLast(this string ids) {
    string[] idsTemp = ids.Split(',');
    if (idsTemp.Length == 0) return new List<string>();
    return (idsTemp.Length > 2) ?
       new List<string> { {idsTemp[0] }, { idsTemp[idsTemp.Length-1] }} :
       new List<string> {{ idsTemp[0] }};
  }
}

编辑 - 删除尾随 ,

使用您可能想要执行的任一前述方法,Linq 或 NonLinq。

List<string> firstLast = ids.Trim(new[]{','}).GetFirstLast();

【讨论】:

  • 我发现在这种情况下使用 Linq 有点矫枉过正,但它在技术上是正确的,所以 +1。
  • 完全正确,但我可以用其他方式代替 linq,因为我想消除字符串末尾的 , ;(
  • @Renan 这可能被认为是矫枉过正,但我​​发现idsTemp.Last() 比idsTemp[idsTemp.Length - 1] 更具表现力。 Linq 确实让您的代码更容易表达为您的意思。
  • @ashuthinks 如果您尝试删除尾随的,,您是否想过使用Trim(new[] {','})
【解决方案3】:
var first = _ids.First();
var last = _ids.Last();

【讨论】:

    【解决方案4】:
    _ids.First()
    _ids.Last()
    

    根据“列表类”文档 http://msdn.microsoft.com/library/vstudio/s6hkc2c4.aspx

    【讨论】:

    • 这不是类的实际部分,它是一个 Linq 扩展,需要您使用 System.Linq 命名空间。虽然答案在技术上并没有错,但它可能会导致一些混乱!
    【解决方案5】:

    手工:

    string first = null;
    string last = null;
    if (_ids.Count > 0)
    {
        first = _ids[0];
        last = _ids[_ids.Count - 1];
    }
    

    通过 LINQ:

    string first = _ids.FirstOrDefault();
    string last = _ids.LastOrDefault();
    

    【讨论】:

    • 完全正确,但是我可以用其他方式代替 linq,因为我想消除它,它位于字符串的末尾;(
    • @ashuthinks,名为By hands 的版本完全没有LINQ。
    • 我怎样才能消除字符串末尾的, :(
    • @ashuthinks,请致电 ids.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries) 而不是 ids.Split(',')。
    【解决方案6】:

    回应 OP 的最后一条评论:

    ",最后会出现,因为当我发送参数时,我会在每个参数之后添加,以便在 .cs 文件中它会出现"

    您似乎正试图从字符串数组中生成一个包含逗号分隔值的字符串。

    您可以使用string.Join() 执行此操作,如下所示:

    string[] test = {"aaaa", "bbbb", "cccc"};
    
    string joined = string.Join(",", test);
    
    Console.WriteLine(joined); // Prints "aaaa,bbbb,cccc"
    

    【讨论】:

      猜你喜欢
      • 2019-03-09
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      相关资源
      最近更新 更多