DonMax
DonMax

Reputation: 960

Get character after certain character from a String

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

Answers (6)

bdn02
bdn02

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

Anirudha
Anirudha

Reputation: 32797

String.Join("",input.Split(new char[]{'*'},StringSplitOptions.RemoveEmptyEntries)
                    .Select(x=>x.First())
           );

Upvotes: 1

Amit Tiwari
Amit Tiwari

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

Selman Gen&#231;
Selman Gen&#231;

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

Vojtěch Dohnal
Vojtěch Dohnal

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

Piotr Stapp
Piotr Stapp

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

Related Questions