简体   繁体   English

我如何拆分字符串动态 c# 的前 2 个单词

[英]How can i split first 2 words of string dynamic c#

i need to split first 2 words of dynamic string.我需要拆分动态字符串的前 2 个单词。

string ex: 11 PM EDT WED JUL 11 2001

string ex: 1100 PM AST TUE AUG 18 2015

string ex: multi formats

I need split like this:我需要像这样拆分:

str1:  11 PM
str2:  1100 PM

my code:我的代码:

int o = 1; myResults[3] = "";
    while (resultList[4].Substring(0, o++).Last() != 'M')
            myResults[3] = resultList[4].Substring(0, o).Trim(); //Time

result:结果:

11
11

尝试这个 :

myResults[3] = resultList[4].Split('M')[0] + "M";

Please try this code.请试试这个代码。

string a="11 PM EDT WED JUL 11 2001";
    int firstSpace = a.IndexOf(" ")+1;
    int secondSpace = a.IndexOf(" ", firstSpace);
    Console.WriteLine(a.Substring(0, secondSpace));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM