Podział stringa na części

Jakiś czas temu współtworzyłem aplikację biznesową (opartą na SQL Server), która wymienia informacje z systemem księgowym (DOS) za pomocą plików DBF. W bazie SQL Servera wszystkie dane takie jak np. nazwa kontrahenta są przechowywane w jednym długim polu (o długości np. 250 znaków). Niestety w przypadku pliku DBF pole kontrahenta (i niestety kilka innych) zostało rozbite na 3 mniejsze po 30 znaków (przykład: KH_NAZWA_1, KH_NAZWA_2, KH_NAZWA_3). Kopiowanie nazwy kontrahenta można oczywiście zakodować na sztywno i stworzyć kod w stylu:

if (customer.Name.Length <= 30)
{
    KH_NAZWA_1 = customer.Name;
}
else if (customer.Name.Length > 30 && customer.Name.Length <= 60)
{
    KH_NAZWA_1 = customer.Name.Substring(0, 30);
    KH_NAZWA_2 = customer.Name.Substring(30);
}
else if (customer.Name.Length > 60 && customer.Name.Length <= 90)
{
    KH_NAZWA_1 = customer.Name.Substring(0, 30);
    KH_NAZWA_2 = customer.Name.Substring(30, 30);
    KH_NAZWA_3 = customer.Name.Substring(60);
}
else
{
    KH_NAZWA_1 = customer.Name.Substring(0, 30);
    KH_NAZWA_2 = customer.Name.Substring(30, 30);
    KH_NAZWA_3 = customer.Name.Substring(60, 30);
}

co oczywiście spełni swoje zadanie, jednak biorąc pod uwagę to, że podobne czynności musimy wykonać również w kilku innych miejscach, warto pokusić się o napisanie krótkiej funkcji, która dzielenie zrobi nam automatycznie. Oczywiście napiszemy ją w postaci metody rozszerzającej klasę System.String:

public static List SplitIntoParts(this string input, int partLength)
{
    var result = new List();
    int partIndex = 0;
    int length = input.Length;
    while (length > 0)
    {
        var tempPartLength = length >= partLength ? partLength : length;
        var part = input.Substring(partIndex * partLength, tempPartLength);
        result.Add(part);
        partIndex++;
        length -= partLength;
    }
    return result;
}
Poniżej dwa przykłady zastosowania metody SplitIntoParts.
class Program
{
    static void Main(string[] args)
    {
        string longString = "This is a very long string, which we want to split into smaller parts every max. 30 characters long.";
        var partLength = 30;
        var parts = longString.SplitIntoParts(partLength);

        Console.WriteLine("String: " + longString);
        Console.WriteLine("Total length: " + longString.Length);
        Console.WriteLine("Part length: " + partLength);
        Console.WriteLine();
        Console.WriteLine("All parts");
        Console.WriteLine("Total parts: " + parts.Count);
        Console.WriteLine("Parts:");
        foreach (var part in parts)
        {
            Console.WriteLine("{0}: {1}", part.Length.ToString("D3"), part);
        }
        Console.WriteLine();

        var part1 = parts.Count > 0 ? parts[0] : string.Empty;
        var part2 = parts.Count > 1 ? parts[1] : string.Empty;
        var part3 = parts.Count > 2 ? parts[2] : string.Empty;
        Console.WriteLine("Parts in variables");
        Console.WriteLine("Total parts: " + 3);
        Console.WriteLine("Parts:");
        Console.WriteLine("{0}: {1}", part1.Length.ToString("D3"), part1);
        Console.WriteLine("{0}: {1}", part2.Length.ToString("D3"), part2);
        Console.WriteLine("{0}: {1}", part3.Length.ToString("D3"), part3);
        Console.ReadLine();
    }
}
Niestety, ze względu na to, że w .NET brakuje funkcji podobnej do list() z języka PHP, przypisanie do zmiennych musimy zrobić ręcznie.
Metodę tą znajdziecie również tutaj.

Tagi:, ,

Nie ma jeszcze komentarzy.

Dodaj komentarz

Uzupełnij * Time limit is exhausted. Please reload the CAPTCHA.