简体   繁体   中英

C# Advanced String Manipulation or LINQ - How To

I am not sure how to go about this. I have a dynamic comma delimited string:

A - B, Hello, C - D, World

I need to take that string and remove/filter out all instances of items that have a " - " in it. So the desired result would be a new string that would look like this:

Hello, World

Now if this cannot be done using string manipulation, the string comes from a

IEnumerable<string>

using linq. So maybe there is a way to get my desired result by using LINQ.

Here is the code:

var apps = lbAppGroup.Items.Cast<ListItem>().Where(i => i.Selected).Select(i => i.Value);
string selectedAppValues = String.Join(", ", apps.ToArray());

This returns my initial example.

I am using C# 3.5. Any direction or examples would be greatly appreciated!

Just do it with LINQ like you're already doing:

var apps = lbAppGroup.Items.Cast<ListItem>().Where(i => i.Selected && !i.Value.Contains("-")).Select(i => i.Value);
string selectedAppValues = String.Join(", ", apps.ToArray());

Also, you could use a regular expression like this:

Regex re = new Regex("(?<=[,^]\s*)[^-]+(?=[,$])");

But you would only do that if you only had the string.

This will return all strings in a sequence that do not contain a dash:

apps.Where(s => !s.Contains("-"));

you can chain your LINQ statements so you can either add it to the select like this:

var apps = lbAppGroup.Items.Cast<ListItem>().Where(i => i.Selected).Where(s => !s.Contains("-")).Select(i => i.Value);

or use it in the next statement:

string selectedAppValues = String.Join(", ", apps.Where(s => !s.Contains("-")).ToArray());

Personally, I have an extension method

// takes any IEnumerable, not just strings (like an int array for example)
public static string Join(this IEnumerable source, string delimiter = ", ")
{
   return string.Join(
            delimiter, 
            source.OfType<object>()
            .Select(i => i == null ? "null" : i.ToString()).ToArray()
           )
}

If you really want to go LINQ all the way

lbAppGroup.Items.Cast<ListItem>().Where(i => i.Selected)
.Where(i => i.Value.Contains("-"))
.Select(i => i.Value).Aggregate((x, y) => x + ", " + y);

For really large collections this won't perform as well as string.Join though.

I cant comment in LINQ particullary, but I can tell you how i'd go about manipulating this string.

"A - B, Hello, C - D, World"

If each string is seperated by a comma, then each string is an item in a list of strings.

The first step is to parse out each item. Add each character, starting from the beginning, to a seperate string. Once you hit a comma, add this string to a string array, clear the string, and move to the next character (Or next character after the space). Like so:

Begin   -> ""
[0] = A -> "A"
[1] =   -> "A "
[2] = - -> "A -"
...
[5] = , -> "A - B"
list<string>.add("A - B")

Once you have the list of strings, you then iterate through the list and say

if (list.get(x).contains("-") list.remove(x)

Or whatever.

Now that this is all set up, you can just hack off the whole list stuff and just do it on the fly: 1. Determine "current" item 2. If it has - in it, do not add it to the final string 3. Otherwise, add it with the comma 4. Move to next item and repeat until finished

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