How to convert string abcabc to abc in C# -


how convert string

"abcabc"

to

"abc"

here want achieve

andsqlp47andsqlp47\ctoprod8r2

to

andsqlp47\ctoprod8r2

you can use regex find repeating pattern , replacement:

var regex = new regex(@"(\w+)\1\\(\w+)"); var result = regex.replace(@"andsqlp47andsqlp47\ctoprod8r2", @"$1\$2"); //result: andsqlp47\ctoprod8r2 

regex explanation:

(\w+) : match sequence of characters (first capture group $1)  \1 : match same sequence of characters first capture group \\ : match '\' character (\w+) : match sequence of characters (second capture group $2) 

you can more info regex on msdn

edit:

to match string 2 repeated words, use following regex:

var regex = new regex(@"^(\w+)\1$"); var result = regex.replace(@"abcabc", @"$1"); //result: abc 

^ , $ denote start , end of string, matches if whole text repetition of 2 words.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

how to prompt save As Box in Excel Interlop c# MVC 4 -

xslt 1.0 - How to access or retrieve mets content of an item from another item? -