C # Get a substring with a specific pattern from a string

I have a list of lines like this:

List<string> list = new List<string>();
list.Add("Item 1: #item1#");
list.Add("Item 2: #item2#");
list.Add("Item 3: #item3#");

How can I get and add substrings # item1 #, # item2 #, etc. to the new list?

I can get the full line only if it contains "#":

foreach (var item in list)
{
    if(item.Contains("#"))
    {
        //Add item to new list
    }
}
+5
source share
7 answers

You can take a look Regex.Match. If you know a little about regular expressions (in your case this will be a fairly simple pattern:) "#[^#]+#", you can use it to extract all elements starting and ending with '#'any number of other characters except '#'between them.

Example:

Match match = Regex.Match("Item 3: #item3#", "#[^#]+#");
if (match.Success) {
    Console.WriteLine(match.Captures[0].Value); // Will output "#item3#"
}
+8
source

LINQ. ( , , .)

var list = new List<string> ()
{
    "Item 1: #item1#",
    "Item 2: #item2#",
    "Item 3: #item3#",
    "Item 4: #item4#",
    "Item 5: #item5#",
};

var pattern = @"#[A-za-z0-9]*#";

list.Select (x => Regex.Match (x, pattern))
    .Where (x => x.Success)
    .Select (x => x.Value)
    .ToList ()
    .ForEach (Console.WriteLine);

:

# item1 #

# item2 #

# item3 #

# item4 #

# Item5 #

+2

LINQ will do the job nicely:

var newList = list.Select(s => '#' + s.Split('#')[1] + '#').ToList();

Or, if you prefer query expressions:

var newList = (from s in list
               select '#' + s.Split('#')[1] + '#').ToList();

Alternatively, you can use regular expressions as suggested with Botz3000 and combine them with LINQ:

var newList = new List(
    from match in list.Select(s => Regex.Match(s, "#[^#]+#"))
    where match.Success
    select match.Captures[0].Value
);
+1
source

The code will solve your problem. But if the string does not contain #item# , then the original string will be used.

var inputList = new List<string>
    {
        "Item 1: #item1#",
        "Item 2: #item2#",
        "Item 3: #item3#",
        "Item 4: item4"
    };

var outputList = inputList
    .Select(item =>
        {
            int startPos = item.IndexOf('#');
            if (startPos < 0)
                return item;

            int endPos = item.IndexOf('#', startPos + 1);
            if (endPos < 0)
                return item;
            return item.Substring(startPos, endPos - startPos + 1);
        })
    .ToList();
+1
source

How about this:

List<string> substring_list = new List<string>();
foreach (string item in list)
{
    int first = item.IndexOf("#");
    int second = item.IndexOf("#", first);
    substring_list.Add(item.Substring(first, second - first);
}
0
source

You can do this simply by using:

    List<string> list2 = new List<string>();
    list.ForEach(x => list2.Add(x.Substring(x.IndexOf("#"), x.Length - x.IndexOf("#"))));
0
source

try it.

var itemList = new List<string>();
foreach(var text in list){
string item = text.Split(':')[1];
itemList.Add(item);


}
0
source

All Articles