我有以下代码:
void dbSvc_retrievepopularhawkerCompleted(object sender, RouteServiceRef.retrievepopularhawkerCompletedEventArgs e)
{
List<RouteServiceRef.Hawker> recommendPlaceList;
recommendPlaceList = e.Result.Cast<RouteServiceRef.Hawker>().ToList();
string hawkername = "";
string address = "";
string postal = "";
double coordX = 0.0;
double coordY = 0.0;
double popularity = 0;
foreach (RouteServiceRef.Hawker rp in recommendPlaceList)
{
hawkername = rp.hawkername;
address = rp.address;
postal = rp.postal;
coordX = rp.xcoord;
coordY = rp.ycoord;
popularity = rp.popularity;
cities.Add(new City() { Name = hawkername, Population = popularity });
}
ColumnSeries bs = ChartControl.Series[0] as ColumnSeries; bs.ItemsSource = cities;
}
如何提取hawkername字符串中的前三个单词? hawkername的结果是:
我需要输出前三个字符串。
答案 0 :(得分:2)
var firstThreeWords = hawkername.Split(' ').Take(3);
如果您还没有使用using System.Linq;
,请不要忘记:
答案 1 :(得分:1)
这样的事情会:
var input = "ldsk bkfd badk klsfdl";
var result = string.Join(" ", input.Split(' ').Take(3));
Console.WriteLine(result);