繁体   English   中英

从String中的某个字符后获取字符

[英]Get character after certain character from a String

我需要在字符串中的某个字符匹配后获取字符。 请考虑我的输入字符串与预期的结果字符集。

示例字符串

*This is a string *with more than *one blocks *of values.

结果字符串

Twoo

我做到了这一点

string[] SubIndex = aut.TagValue.Split('*');
            string SubInd = "";
            foreach (var a in SubIndex)
            {
                SubInd = SubInd + a.Substring(0,1);
            }

任何帮助将不胜感激。

谢谢

LINQ解决方案:

var str = "*This is a string *with more than *one blocks *of values.";
var chars = str.Split(new char[] {'*'}, StringSplitOptions.RemoveEmptyEntries)
               .Select(x => x.First());
var output = String.Join("", chars);
string s = "*This is a string *with more than *one blocks *of values.";
string[] splitted = s.Split(new char[] { '*' }, StringSplitOptions.RemoveEmptyEntries);
string result = "";
foreach (string split in splitted)
    result += split[0];
Console.WriteLine(result);

下面的代码应该工作

var s = "*This is a string *with more than *one blocks *of values."
while ((i = s.IndexOf('*', i)) != -1)
{
    // Print out the next char
    if(i<s.Length)
            Console.WriteLine(s[i+1]);

    // Increment the index.
    i++;
}
String.Join("",input.Split(new char[]{'*'},StringSplitOptions.RemoveEmptyEntries)
                    .Select(x=>x.First())
           );
string strRegex = @"(?<=\*).";
Regex myRegex = new Regex(strRegex, RegexOptions.Multiline | RegexOptions.Singleline);
string strTargetString = "*This is a string *with more than *one blocks *of values.";
StringBuilder sb = new StringBuilder();    
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
    if (myMatch.Success) sb.Append(myMatch.Value);
}
string result = sb.ToString();

请看下面......

char[] s3 = "*This is a string *with more than *one blocks *of values.".ToCharArray();
StringBuilder s4 = new StringBuilder();
for (int i = 0; i < s3.Length - 1; i++)
{
   if (s3[i] == '*')
     s4.Append(s3[i+1]);
}
Console.WriteLine(s4.ToString());

暂无
暂无

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

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