Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate neighboring characters of a special character "-"

i am developing an application using c#.net in which i need that if a input entered by user contains the character '-'(hyphen) then i want the immediate neighbors of the hyphen(-) to be concatenated for example if a user enters

A-B-C then i want it to be replaced with ABC
AB-CD then i want it to be replaced like BC
ABC-D-E then i want it to be replaced like CDE
AB-CD-K then i want it to be replaced like BC and DK both separated by keyword and

after getting this i have to prepare my query to database.

i hope i made the problem clear but if need more clarification let me know. Any help will be appreciated much.

Thanks, Devjosh

like image 873
Devjosh Avatar asked Feb 08 '12 09:02

Devjosh


People also ask

How do you concatenate special characters in Excel?

Use the ampersand & character instead of the CONCATENATE function. The ampersand (&) calculation operator lets you join text items without having to use a function. For example, =A1 & B1 returns the same value as =CONCATENATE(A1,B1).

How do you concatenate a character?

The strcat() method is used to concatenate strings in C++. The strcat() function takes char array as input and then concatenates the input values passed to the function.

Can we concatenate string and character?

Concatenating strings would only require a + between the strings, but concatenating chars using + will change the value of the char into ascii and hence giving a numerical output.

Which character is used to concatenation of two strings?

The ampersand symbol is the recommended concatenation operator. It is used to bind a number of string variables together, creating one string from two or more individual strings.


1 Answers

Use:

string[] input = {
                        "A-B-C",
                        "AB-CD",
                        "ABC-D-E",
                        "AB-CD-K"
                    };

var regex = new Regex(@"\w(?=-)|(?<=-)\w", RegexOptions.Compiled);

var result = input.Select(s => string.Concat(regex.Matches(s)
    .Cast<Match>().Select(m => m.Value)));

foreach (var s in result)
{
    Console.WriteLine(s);
}

Output:

ABC
BC
CDE
BCDK
like image 155
Kirill Polishchuk Avatar answered Sep 30 '22 08:09

Kirill Polishchuk