Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing string into desired output
    primarykey
    data
    text
    <p>Sample input is (note this is a one line string, I used a quote here to make it more readable):</p> <blockquote> <p>Level 1/129/1 Murray Ave &amp; 15A&amp;B ERICA AVENUE &amp; 12 HARVEY STREET &amp; 34 VICTORIA STREET &amp; 3/56 ST LEONARDS ST, MOSMAN PARK ( John). 78/10 WELLINGTON ST MOSMAN PARK (Rambo)</p> </blockquote> <p>My current output is:</p> <pre><code>1/129/1 - Murray - Ave - 15A - - - B - ERICA - AVENUE - 12 - HARVEY - STREET - 34 - VICTORIA - STREET - 3/56 - ST LEONARDS - ST - MOSMAN PARK 78/10 - WELLINGTON - ST - MOSMAN PARK </code></pre> <p>Desired output is:</p> <pre><code>1/129/1 - Murray - Ave - 15A - ERICA - AVENUE - 15B - ERICA - AVENUE - 12 - HARVEY - STREET - 34 - VICTORIA - STREET - 3/56 - ST LEONARDS - ST - MOSMAN PARK 78/10 - WELLINGTON - ST - MOSMAN PARK </code></pre> <p>If the first property only contains the number, it should inherit the information from the next record and if the next record number only contains a letter it show backwards inherit the number of the previous record, for example:</p> <pre><code> 15A - Erica - Avenue 15B - Erica - Avenue </code></pre> <p><strong>Which gives me the desired above output, how can I archive that ?</strong></p> <p>Here is my code (NOTE: suffixes is a <code>List&lt;string&gt;</code>):</p> <pre><code>static void Main(string[] args) { List&lt;ResultData&gt; result = new List&lt;ResultData&gt;(); string myColumn = "Level 1/129/1 Murray Ave &amp; 15A&amp;B ERICA AVENUE &amp; 12 HARVEY STREET &amp; 34 VICTORIA STREET &amp; 3/56 ST LEONARDS ST, MOSMAN PARK ( John). 78/10 WELLINGTON ST MOSMAN PARK (Rambo)"; // dot replaced with &amp; as they are to be split myColumn = myColumn.Replace('.', '&amp;'); // I don't need the Level word which means // each property starts with numbers now myColumn = myColumn.Replace("Level", ""); // Removes anything in between parentheses and the parentheses myColumn = RemoveBetween(myColumn, '(', ')'); string[] splitResult = myColumn.Split('&amp;'); foreach (string item in splitResult) { string property = item.Trim(); if (property.IndexOf(' ') &gt; 0) { string area = string.Empty; string locationType = string.Empty; string number = property.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).First(); property = property.Replace(number, "").Trim(); // When comma is present, area is always the last // and locationType always before it if (property.IndexOf(',') &gt; 0) { area = property.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Last().Trim(); property = property.Replace(area, "").Replace(",", "").Trim(); locationType = property.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Last().Trim(); property = property.Replace(" " + locationType, "").Trim(); } else { // When comma is not present I have to check // if the string contains a given street suffix // and pick up from there string found = suffixes.Find(x =&gt; property.Trim().Contains(" " + x, StringComparison.OrdinalIgnoreCase)); if (!string.IsNullOrEmpty(found)) found = " " + found; // need the space otherwise it will delete // places like ST LEONARD. locationType = property.Substring(property.ToLower().IndexOf(found.ToLower()), found.Length).Trim(); int total = property.ToLower().IndexOf(found.ToLower()) + found.Length; if (property.ToLower().IndexOf(found.ToLower()) &gt; 0 &amp;&amp; total &lt; property.Length) area = property.Substring(total, property.Length - total).Trim(); property = property.Replace(",", "").Trim().Replace(locationType, "").Trim(); if (!string.IsNullOrEmpty(area)) property = property.Replace(area, "").Trim(); } string name = property; result.Add(new ResultData() { Number = number, Name = name, LocationType = locationType, Area = area }); } else { result.Add(new ResultData() { Number = property }); } } string save = string.Empty; foreach (ResultData item in result) { Console.WriteLine(item.Number + " - " + item.Name + " - " + item.LocationType + " - " + item.Area); save += item.Number + " - " + item.Name + " - " + item.LocationType + " - " + item.Area + Environment.NewLine; } System.IO.File.WriteAllLines(@"save.txt", save.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)); Console.WriteLine(Environment.NewLine + "Press any key to leave..."); Console.ReadKey(); } /// &lt;summary&gt; /// Remove from the string the pattern and what is in between it /// more format double space to single /// &lt;/summary&gt; static string RemoveBetween(string s, char begin, char end) { Regex regex = new Regex(string.Format("\\{0}.*?\\{1}", begin, end)); return new Regex(" +").Replace(regex.Replace(s, string.Empty), " "); } public static bool Contains(this string source, string toCheck, StringComparison comp) { return source.IndexOf(toCheck, comp) &gt;= 0; } </code></pre> <p><strong>If you know anything I can do to improve the above code would love to know as well, write me a comment.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload