サンプル入力は次のとおりです (これは 1 行の文字列であることに注意してください。読みやすくするためにここで引用符を使用しています)。
Level 1/129/1 Murray Ave & 15A&B ERICA AVENUE & 12 HARVEY STREET & 34 VICTORIA STREET & 3/56 ST LEONARDS ST, MOSMAN PARK ( John). 78/10 WELLINGTON ST MOSMAN PARK (ランボー)
私の現在の出力は次のとおりです。
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
望ましい出力は次のとおりです。
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
最初のプロパティに数字のみが含まれている場合は、次のレコードから情報を継承する必要があります。次のレコード番号に文字のみが含まれている場合は、前のレコードの番号を逆に継承します。次に例を示します。
15A - Erica - Avenue
15B - Erica - Avenue
上記の目的の出力が得られますが、それをアーカイブするにはどうすればよいですか?
これが私のコードです(注:接尾辞はaですList<string>
):
static void Main(string[] args)
{
List<ResultData> result = new List<ResultData>();
string myColumn = "Level 1/129/1 Murray Ave & 15A&B ERICA AVENUE & 12 HARVEY STREET & 34 VICTORIA STREET & 3/56 ST LEONARDS ST, MOSMAN PARK ( John). 78/10 WELLINGTON ST MOSMAN PARK (Rambo)";
// dot replaced with & as they are to be split
myColumn = myColumn.Replace('.', '&');
// 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('&');
foreach (string item in splitResult)
{
string property = item.Trim();
if (property.IndexOf(' ') > 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(',') > 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 => 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()) > 0 && total < 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();
}
/// <summary>
/// Remove from the string the pattern and what is in between it
/// more format double space to single
/// </summary>
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) >= 0;
}
上記のコードを改善するために私ができることを知っている場合は、コメントを書いてください.