Rico Mico
Rico Mico

Reputation: 21

replacing part of a string by position in c#

I have a string: ===FILE CONTENT==\r\n@something here... }\r\n\r\n@something here 2... }\t\r\n\r\n\r\n\r\n@something here 3... }\r@something here 4...} \n

using c#, I want get all strings inside this string that starts with '@' and ends with '}', but I having a problem with getting the position of '@' and '}' since newline and tabs are not fix. thank you in advance

here is the sample output:

new string 1 = "@something here... }";
 new string 2 = "@something here 2... }";
 new string 3="@something here 3... }";
 new string 4="@something here 4...}";

Upvotes: 0

Views: 79

Answers (2)

Riad Baghbanli
Riad Baghbanli

Reputation: 3319

See code below:

string[] getSubstrings(string str)
{
    return str.Split('@')
        .Select(s => "@" + s.Substring(0, 1 + s.IndexOf('}')))
        .ToArray();
}

Upvotes: 1

Mike Jerred
Mike Jerred

Reputation: 10555

You can use a regex:

var regex = new Regex(@"@[^}]*}");
var listOfMatches = new List<string>();
for (var match = regex.Match(inputString); match.Success; match = match.NextMatch())
{
    listOfMatches.Add(match.Value);
}

Upvotes: 0

Related Questions