Reputation: 159
I need to find pattern in my string and then cut out whole part that contains that pattern. I'll give an example what I need to do:
I have string like this:
string text = "Some random words here EK/34 54/56/75 AB/12/34/56/BA1590/A and more random stuff...";
In that string I want to check if this pattern exists:
string whatImLookinFor = "12/34/56/";
And If it is in my string so I want to cut out whole code that contains my pattern and it is separated with spaces:
AB/12/34/56/BA1590/A
Upvotes: 0
Views: 137
Reputation: 51653
You can solve it using regular expressions or simply string operations.
This is using simple string operations:
using System;
using System.Linq;
public class Program
{
public static void Main()
{
var text = "Some random words here EK/34 54/56/75 AB/12/34/56/BA1590/A and more random stuff...";
var whatImLookinFor = "12/34/56/";
// check if text contains it _at all_
if (text.Contains(whatImLookinFor))
{
// split the whole text at spaces as specified and retain those parts that
// contain your text
var split = text.Split(' ').Where(t => t.Contains(whatImLookinFor)).ToList();
// print all results to console
foreach (var s in split)
Console.WriteLine(s);
}
else
Console.WriteLine("Not found");
Console.ReadLine();
}
}
Output:
AB/12/34/56/BA1590/A
Upvotes: 1