Reputation: 960
I need to get a characters after certain character match in a string. Please consider my Input string with expected resultant character set.
Sample String
*This is a string *with more than *one blocks *of values.
Resultant string
Twoo
I have done this
string[] SubIndex = aut.TagValue.Split('*');
string SubInd = "";
foreach (var a in SubIndex)
{
SubInd = SubInd + a.Substring(0,1);
}
Any help to this will be appreciated.
Thanks
Upvotes: 3
Views: 1821
Reputation: 1500
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);
Upvotes: 3
Reputation: 32797
String.Join("",input.Split(new char[]{'*'},StringSplitOptions.RemoveEmptyEntries)
.Select(x=>x.First())
);
Upvotes: 1
Reputation: 368
please see below...
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());
Upvotes: 0
Reputation: 101681
LINQ
solution:
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);
Upvotes: 5
Reputation: 8104
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();
Upvotes: 0
Reputation: 19830
Below code should work
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++;
}
Upvotes: 2