In python there is an option to do this:
patterns = [s for s in"""
ATGCG
GCATG
CATGC
AGGCA
GGCAT
""".split() if s]
In c# I have a string like:
ATGCG
GCATG
CATGC
AGGCA
GGCAT
If I do
string patterns = "
ATGCG
GCATG
CATGC
AGGCA
GGCAT";
that would be incorrect
So I was thinking to use
string patterns = "ATGCG\nGCATG\nCATGC\nAGGCA\nGGCAT";
var elems = patterns.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
However I do not want to put \n
each new line, Is there a better way to do this?
Maybe reading from a file that string? How would c# code look like?
I was thinking on
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line);
}
}
string allines = sb.ToString();
would that be correct?
full answer is:
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string patterns = @"
ATGCG
GCATG
CATGC
AGGCA
GGCAT
";
var elems = patterns.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
var results = elems.SelectMany((p1, i1) => elems.Where((p2, i2) => i1 != i2).Select(p2 => new { p1, p2 }))
.Where(x => x.p1.Substring(1) == x.p2.Substring(0, x.p2.Length - 1)).ToList();
string result = "";
foreach (var pair in results)
{
result += pair.p1 + " -> " + pair.p2 +"\n";
}
System.IO.File.WriteAllText("out.txt", result);
}
}
}