Загрузка данных


using System;
using System.Text.RegularExpressions;

namespace SupportAndTesting
{
    /// <summary>
    /// Класс для обработки строк
    /// </summary>
    public static class StringProcessor
    {
        /// <summary>
        /// Удаляет лишние пробелы по краям и заменяет множественные пробелы между словами на один
        /// </summary>
        public static string RemoveExtraSpaces(string input)
        {
            if (string.IsNullOrEmpty(input))
                return string.Empty;

            // Заменяем последовательности из двух и более пробелов на один и убираем пробелы по краям
            return Regex.Replace(input.Trim(), @"\s+", " ");
        }

        /// <summary>
        /// Обрезает строку до targetLength или дополняет пробелами справа, если она короче
        /// </summary>
        public static string TruncateOrPad(string input, int targetLength)
        {
            if (targetLength < 0)
                throw new ArgumentOutOfRangeException(nameof(targetLength), "Длина не может быть отрицательной.");

            input ??= string.Empty;

            if (input.Length > targetLength)
            {
                return input.Substring(0, targetLength);
            }
            
            return input.PadRight(targetLength);
        }

        /// <summary>
        /// Проверяет наличие подстроки с возможностью игнорирования регистра
        /// </summary>
        public static bool ContainsSubstring(string source, string substring, bool ignoreCase = true)
        {
            if (source == null || substring == null)
                return false;

            StringComparison comparison = ignoreCase 
                ? StringComparison.OrdinalIgnoreCase 
                : StringComparison.Ordinal;

            return source.Contains(substring, comparison);
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Поддержка и тестирование: StringProcessor";

            while (true)
            {
                Console.Clear();
                Console.WriteLine("=== ТЕСТИРОВАНИЕ КЛАССА StringProcessor ===");
                Console.WriteLine("1. Тест RemoveExtraSpaces()");
                Console.WriteLine("2. Тест TruncateOrPad()");
                Console.WriteLine("3. Тест ContainsSubstring()");
                Console.WriteLine("0. Выход");
                Console.Write("\nВыберите действие: ");

                string choice = Console.ReadLine();
                Console.WriteLine();

                switch (choice)
                {
                    case "1":
                        TestRemoveExtraSpaces();
                        break;
                    case "2":
                        TestTruncateOrPad();
                        break;
                    case "3":
                        TestContainsSubstring();
                        break;
                    case "0":
                        return;
                    default:
                        Console.WriteLine("Неверный пункт меню!");
                        break;
                }

                Console.WriteLine("\nНажмите любую клавишу для возврата в меню...");
                Console.ReadKey();
            }
        }

        static void TestRemoveExtraSpaces()
        {
            Console.WriteLine("--- Тестирование RemoveExtraSpaces ---");
            Console.Write("Введите строку: ");
            string input = Console.ReadLine();

            string result = StringProcessor.RemoveExtraSpaces(input);
            Console.WriteLine($"Результат: \"{result}\"");
        }

        static void TestTruncateOrPad()
        {
            Console.WriteLine("--- Тестирование TruncateOrPad ---");
            Console.Write("Введите строку: ");
            string input = Console.ReadLine();

            Console.Write("Введите желаемую длину: ");
            if (int.TryParse(Console.ReadLine(), out int length))
            {
                try
                {
                    string result = StringProcessor.TruncateOrPad(input, length);
                    Console.WriteLine($"Результат: \"{result}\" (Длина: {result.Length})");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Ошибка: {ex.Message}");
                }
            }
            else
            {
                Console.WriteLine("Ошибка: некорректное числовое значение.");
            }
        }

        static void TestContainsSubstring()
        {
            Console.WriteLine("--- Тестирование ContainsSubstring ---");
            Console.Write("Введите исходную строку: ");
            string source = Console.ReadLine();

            Console.Write("Введите подстроку для поиска: ");
            string sub = Console.ReadLine();

            bool result = StringProcessor.ContainsSubstring(source, sub);
            Console.WriteLine($"Подстрока найдена: {result}");
        }
    }
}