从C#中的每个字符串中获取前三个字母

以下是我们在列表中的字符串-

List<object> list = new List<object> { "keyboard", "mouse", "joystick", "monitor" };

若要使用前3个字母,请使用子字符串方法,并在Linq Select方法下使用它。

IEnumerable<string> res = list.AsQueryable() .Cast<string>() .Select(str => str.Substring(0, 3));

示例

using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
   static void Main() {
      List<object> list = new List<object> { "keyboard", "mouse", "joystick", "monitor" };
      //从每个字符串中获取前3个字母
      IEnumerable<string> res = list.AsQueryable() .Cast<string>() .Select(str =>       str.Substring(0,3));
      foreach (string str in res) {
         Console.WriteLine(str);
      }
   }
}

输出结果

key
mou
joy
mon