1

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);



        }
    }
}
edgarmtze
  • 24,683
  • 80
  • 235
  • 386

2 Answers2

4

I'm not sure I completely follow you, but if you're looking to define a variable over several lines, you could define the string using a verbatim string literal, and then split on Environment.NewLine (\r\n)

string patterns = @"
ATGCG
GCATG
CATGC
AGGCA
GGCAT";

Here's how you can get the individual lines out:

var elems = patterns.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • How should I change line `var elems = patterns.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);` – edgarmtze Dec 09 '13 at 04:31
  • I would split on any/all whitespace (with [`str.Split(new char[0])`](http://stackoverflow.com/questions/6111298/best-way-to-specify-whitespace-in-a-string-split-operation) or [Regex.Split](http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.split(v=vs.110).aspx)) - the editor used to edit the file might not be the same as the environment in which it is running .. and it's too easy to have an extra space hiding off the right. – user2864740 Dec 09 '13 at 04:37
  • @GrantWinney If that's not acceptable to split on all whitespace (although I think this is how `str.split()` in Python works), I'd still probably split on `[\r\n]+` (as opposed to the current environment newline). In any case, verbatim strings are the analogous syntax. – user2864740 Dec 09 '13 at 04:41
1
string allines  = File.ReadAllText(path);
Damith
  • 62,401
  • 13
  • 102
  • 153