简体   繁体   中英

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...}";

See code below:

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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