コード例 #1
0
        /// <summary>
        /// Create object MyString
        /// </summary>
        public static void Main()
        {
            MyString myStr  = new MyString('a', 'b', 'c');
            MyString myStr2 = new MyString(new char[] { '2', '3', '4', '5' });

            Console.WriteLine($"MySting = {myStr.ToString()}");
            Console.WriteLine($"MyString2 = {myStr2.ToString()}");
            Console.WriteLine("Equals? = " + myStr.Equals(myStr2));
            Console.WriteLine("Concat = " + myStr.ConCat(myStr2));
            Console.WriteLine("Seach char, index  = " + myStr.SearchChar('a'));

            Console.WriteLine($"Checking index before = {myStr[0]}");
            myStr[0] = '%';
            Console.WriteLine($"Checking index after = {myStr[0]}");

            string str1 = "1234567890";

            myStr = str1;
            Console.WriteLine($"Checking string {str1} - > MyString {myStr.ToString()}");
            myStr[0] = 'a';
            str1     = myStr;
            Console.WriteLine($"Checking MyString  {myStr.ToString()}  - > string {str1}");

            StringBuilder str2 = new StringBuilder();

            str2.AppendFormat("qwerty", 0);
            myStr = str2;
            Console.WriteLine($"Checking StringBuilder {str2} - > MyString {myStr.ToString()}");
            myStr[0] = '1';
            str2     = myStr;
            Console.WriteLine($"Checking  MyString {myStr.ToString()} - > StringBuilder {str2}");
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: SergioPonomarev/XT-2018Q4
        private static void Main()
        {
            Console.WriteLine("Greetings! In this program you will see MyString class demonstration.");
            Console.WriteLine();

            Console.WriteLine("Class has three constructors.");
            Console.WriteLine("First constructor takes char array as a parameter: MyString(chars).");
            char[] chars = { 'H', 'e', 'l', 'l', 'o' };
            Console.Write("Char array has: ");

            foreach (char ch in chars)
            {
                Console.Write(ch + ", ");
            }

            MyString myString = new MyString(chars);

            Console.WriteLine($"Value of MyString instance is: {myString}");
            Console.WriteLine();

            Console.WriteLine("Second constructor takes char array from start index in array to length: MyString(char, 2, 3).");

            myString = new MyString(chars, 2, 3);
            Console.WriteLine($"New value of MyString instance is: {myString}");
            Console.WriteLine();

            Console.WriteLine("Third constructor takes specified char as first parameter and integer as a count: MyString('a', 5).");
            myString = new MyString('a', 5);
            Console.WriteLine($"New value of MyString instance is: {myString}");
            Console.WriteLine();

            Console.WriteLine("To get a specified char from MyString instance by index use indexer: myString[int index].");

            myString = new MyString(chars);
            Console.WriteLine($"Value of MyString instance: {myString}");

            Console.WriteLine($"The char form MyString instance by index 1 'myString[1]': {myString[1]}");
            Console.WriteLine();

            Console.WriteLine("Static Concat method has 4 overloads, can take as paramenters: one, two, three object instances or object array.");

            object obj = "Hi ";

            Console.WriteLine($"Object instance: {obj.ToString()}");

            myString = MyString.Concat(obj);
            Console.WriteLine($"myString = MyString.Concat(obj): {myString}");

            object obj1 = 4;

            Console.WriteLine($"Second object instance: {obj1.ToString()}");

            myString = MyString.Concat(obj, obj1);
            Console.WriteLine($"myString = MyString.Concat(obj, obj1): {myString}");

            object obj2 = " times";

            Console.WriteLine($"Third object instance: {obj2.ToString()}");

            myString = MyString.Concat(obj, obj1, obj2);
            Console.WriteLine($"myString = MyString.Concat(obj, obj1, obj2): {myString}");

            object obj3 = " again";

            object[] objects =
            {
                obj,
                obj1,
                obj2,
                obj3
            };

            myString = MyString.Concat(objects);
            Console.WriteLine($"myString = MyString.Concat(objects): {myString}");
            Console.WriteLine();

            Console.WriteLine("Concat method has same 4 overloads with MyString type.");
            Console.WriteLine();

            Console.WriteLine("Contains method defines if MyString instance contains another MyString instance: bool Contains(MyString value).");
            MyString ms = "times";

            Console.WriteLine($"'{myString}' contains '{ms}': {myString.Contains(ms)}");
            Console.WriteLine();

            Console.WriteLine("Static method MyString.Copy(MyString value) returns a copy of value instance.");

            MyString copy = MyString.Copy(myString);

            Console.WriteLine($"Value of myString: {myString}");
            Console.WriteLine($"Value of copy: {copy}");
            Console.WriteLine($"Object.ReferenceEquals(myString, copy): {object.ReferenceEquals(myString, copy)}");
            Console.WriteLine();

            char[] destination = new char[4];
            myString.CopyTo(5, destination, 0, 4);
            Console.WriteLine("Void CopyTo(int sourceIndex, char[] destintion, int destionationIndex, int count) copies specified part of chars in MyString instance to char array.");
            Console.Write("After 'myString.CopyTo(5, destination, 0, 4)' destination array contents: ");
            foreach (char ch in destination)
            {
                Console.Write(ch + ", ");
            }

            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("EndsWith method serves to find out if MyString instance ends with another MyString instance: bool EndsWith(value).");
            Console.WriteLine($"Value of myString: {myString}");
            MyString addString = "again";

            Console.WriteLine($"Value of addString: {addString}");
            Console.WriteLine($"myString ends with addString - myString.EndsWith(addString): {myString.EndsWith(addString)}");
            Console.WriteLine();

            Console.WriteLine("There is three overloads for IndexOf method:");
            Console.WriteLine($"int IndexOf(char) - myString.IndexOf('i'): {myString.IndexOf('i')}");
            Console.WriteLine($"int IndexOf(char, startIndex) - myString.IndexOf('i', 5): {myString.IndexOf('i', 5)}");
            Console.WriteLine($"int IndexOf(char, startIndex, count) - myString.IndexOf('i', 7, 4): {myString.IndexOf('i', 7, 4)}");
            Console.WriteLine();

            Console.WriteLine("Three overloads for IndexOf method taking MyString instance as a parameter:");
            Console.WriteLine($"myString.IndexOf(addString): {myString.IndexOf(addString)}");
            Console.WriteLine($"myString.IndexOf(addString, 11): {myString.IndexOf(addString, 11)}");
            Console.WriteLine($"myString.IndexOf(addString, 10, 5): {myString.IndexOf(addString, 10, 5)}");
            Console.WriteLine();

            Console.WriteLine("There is three overloads for IndexOfAny method taking char array as a parameter to search:");
            chars = new char[] { 't', 'H', 'i' };
            Console.Write("Char array named 'chars' has: ");
            foreach (char ch in chars)
            {
                Console.Write(ch + ", ");
            }

            Console.WriteLine();
            Console.WriteLine($"myString.IndexOfAny(chars): {myString.IndexOfAny(chars)}");
            Console.WriteLine($"myString.IndexOfAny(chars, 3): {myString.IndexOfAny(chars, 3)}");
            Console.WriteLine($"myString.IndexOfAny(chars, 7, 5): {myString.IndexOfAny(chars, 7, 5)}");
            Console.WriteLine();

            Console.WriteLine("Insert method takes an integer as a start index and MyString value to insert to calling MyString instance. Reternes new MyString instance:");
            MyString insertMS = "again ";

            Console.WriteLine($"Value to insert: '{insertMS}'");
            myString = myString.Insert(11, insertMS);
            Console.WriteLine($"myString = myString.Insert(11, insertMS): {myString}");
            Console.WriteLine();

            Console.WriteLine("There is three overloads for LastIndexOf method:");
            Console.WriteLine($"myString.LastIndexOf('g'): {myString.LastIndexOf('g')}");
            Console.WriteLine($"myString.LastIndexOf('g', 16): {myString.LastIndexOf('g', 16)}");
            Console.WriteLine($"myString.LastIndexOf('g', 16, 3): {myString.LastIndexOf('g', 16, 3)}");

            Console.WriteLine("Three overloads for LastIndexOf method taking MyString instance as a parameter:");
            Console.WriteLine($"myString.LastIndexOf(addString): {myString.LastIndexOf(addString)}");
            Console.WriteLine($"myString.LastIndexOf(addString, 11): {myString.LastIndexOf(addString, 16)}");
            Console.WriteLine($"myString.LastIndexOf(addString, 10, 5): {myString.LastIndexOf(addString, 16, 5)}");
            Console.WriteLine();

            Console.WriteLine("There is three overloads for LastIndexOfAny method taking char array as a parameter to search:");
            Console.Write("Char array named 'chars' has: ");
            foreach (char ch in chars)
            {
                Console.Write(ch + ", ");
            }

            Console.WriteLine();
            Console.WriteLine($"myString.LastIndexOfAny(chars): {myString.LastIndexOfAny(chars)}");
            Console.WriteLine($"myString.LastIndexOfAny(chars, 3): {myString.LastIndexOfAny(chars, 3)}");
            Console.WriteLine($"myString.LastIndexOfAny(chars, 7, 5): {myString.LastIndexOfAny(chars, 7, 5)}");
            Console.WriteLine();

            Console.WriteLine("Remove method has two overloads - remove from start index to end of MyString instance and remove from start count chars. Reterns new MyString instance:");
            myString = myString.Remove(10, 6);
            Console.WriteLine($"myString = myString.Remove(10, 6): '{myString}'");
            myString = myString.Remove(10);
            Console.WriteLine($"myString = myString.Remove(10): '{myString}'");
            Console.WriteLine();

            Console.WriteLine("There is two overloads for Replace method - first replaces old char for new char int whole MyString instance ind second replaces old MyString value to new MyString value. Returns new MyString instance:");
            myString = "Hi bi mi bi";
            Console.WriteLine($"Value of myString: '{myString}'");
            myString = myString.Replace('i', 'e');
            Console.WriteLine($"Value after myString = myString.Replace('i', 'e'): '{myString}'");
            MyString oldValue = "be";

            Console.WriteLine($"MyString instance to be changed (oldValue): '{oldValue}'");
            MyString newValue = "big";

            Console.WriteLine($"MyString instance which is to replace the old value (newValue): '{newValue}'");
            myString = myString.Replace(oldValue, newValue);
            Console.WriteLine($"After myString = myString.Replace(oldValue, newValue): '{myString = myString.Replace(oldValue, newValue)}'");

            Console.WriteLine("StartsWith method defines if MyString instance starts with specified MyString value:");
            MyString startsWith = "He";

            Console.WriteLine($"MyString instance to compare 'startWith' named: '{startsWith}'");
            Console.WriteLine($"myString.StartsWith(startsWith): {myString.StartsWith(startsWith)}");
            Console.WriteLine();

            Console.WriteLine("There is two overloads for SubMyString method - from start index to end of MyString instance and from start index to count. Returns new MyString instance:");
            Console.WriteLine($"myString.SubMyString(3): '{myString.SubMyString(3)}'");
            Console.WriteLine($"myString.SubMyString(3, 3): '{myString.SubMyString(3, 3)}'");
            Console.WriteLine();

            Console.WriteLine($"ToLower method returns new MyString instance with all lower case chars: '{myString.ToLower()}'");
            Console.WriteLine($"And ToUpper method with all upper case chars: '{myString.ToUpper()}'");
            Console.WriteLine();

            Console.WriteLine($"ToCharArray method returns char array with all chars form MyString instance: chars = myString.ToCharArray(){chars = myString.ToCharArray()}");
            Console.Write("Chars array consists of: ");
            foreach (char ch in chars)
            {
                Console.Write(ch + ", ");
            }

            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("There is implicit casts from MyString to string, from string to MyString, from MyString to StringBuilder, from StringBuilder to MyString.");
            Console.WriteLine();

            Console.WriteLine("Also ToString(), GetHashCode() overrided methods, IEquatable<MyString>, IComparable<MyString> and IComparable implementation, '==' and '!=' operatiors.");

            MyString ms1 = "Hello";
            MyString ms2 = "Hello";

            Console.WriteLine($"MyString instance named ms1: '{ms1}'");
            Console.WriteLine($"MyString instance named ms2: '{ms2}'");
            Console.WriteLine($"Object.ReferenceEquals(ms1, ms2): {object.ReferenceEquals(ms1, ms2)}");
            Console.WriteLine($"ms1.Equals(ms2): {ms1.Equals(ms2)}");
            Console.WriteLine();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: KovalenkoGleb/XT-2018Q4
        public static void Main(string[] args)
        {
            string stringstr1;

            Console.Write($"Welcom to MyString Test Application{Environment.NewLine}Enter string line: ");
            stringstr1 = Console.ReadLine();
            MyString mystr1 = new MyString(stringstr1);

            Console.WriteLine($"MyString from string line is: {mystr1}{Environment.NewLine}It's length is {mystr1.Length}{Environment.NewLine}");

            Console.Write("Enter another two MyString lines to compare them. First one: ");
            mystr1 = new MyString(Console.ReadLine());
            Console.Write("Second one: ");
            MyString mystr2 = new MyString(Console.ReadLine());

            switch (MyString.Compare(mystr1, mystr2))
            {
            case 1:
                Console.WriteLine("First one is bigger");
                break;

            case -1:
                Console.WriteLine("Second one is bigger");
                break;

            case 0:
                Console.WriteLine("They are the same");
                break;
            }

            Console.WriteLine();

            Console.Write("Enter another two MyString lines to concatenate them. First one: ");
            mystr1 = new MyString(Console.ReadLine());
            Console.Write("Second one: ");
            mystr2 = new MyString(Console.ReadLine());
            Console.WriteLine($"The concatenation is: {MyString.Concat(mystr1, mystr2)}");
            Console.WriteLine();

            Console.Write("Enter another two MyString lines to find out, if they are equal. First one: ");
            mystr1 = new MyString(Console.ReadLine());
            Console.Write("Second one: ");
            mystr2 = new MyString(Console.ReadLine());
            if (mystr1.Equals(mystr2))
            {
                Console.WriteLine("They are equal");
            }
            else
            {
                Console.WriteLine("They are not equal");
            }

            Console.WriteLine();

            Console.Write("Enter another two MyString lines to find if the first contains second. First one: ");
            mystr1 = new MyString(Console.ReadLine());
            Console.Write("Second one: ");
            mystr2 = new MyString(Console.ReadLine());
            if (mystr1.Contains(mystr2))
            {
                Console.WriteLine("It does");
            }
            else
            {
                Console.WriteLine("It doesn't");
            }

            Console.WriteLine();

            Console.Write("Enter another two MyString lines to find if the first starts with second. First one: ");
            mystr1 = new MyString(Console.ReadLine());
            Console.Write("Second one: ");
            mystr2 = new MyString(Console.ReadLine());
            if (mystr1.StartsWith(mystr2))
            {
                Console.WriteLine("It does");
            }
            else
            {
                Console.WriteLine("It doesn't");
            }

            Console.WriteLine();

            Console.Write("Enter another two MyString lines to find if the first ends with second. First one: ");
            mystr1 = new MyString(Console.ReadLine());
            Console.Write("Second one: ");
            mystr2 = new MyString(Console.ReadLine());
            if (mystr1.EndsWith(mystr2))
            {
                Console.WriteLine("It does");
            }
            else
            {
                Console.WriteLine("It doesn't");
            }

            Console.WriteLine();

            Console.Write("Enter MyString to find the index of a sybmbol. MyString: ");
            mystr1 = new MyString(Console.ReadLine());
            Console.Write("Sybmbol: ");
            char symbol = char.Parse(Console.ReadLine());

            if (mystr1.IndexOf(symbol) == -1)
            {
                Console.WriteLine("There is no such symbol");
            }
            else
            {
                Console.WriteLine(mystr1.IndexOf(symbol));
            }

            Console.WriteLine();

            Console.Write("Enter MyString to find the last index of a sybmbol. MyString: ");
            mystr1 = new MyString(Console.ReadLine());
            Console.Write("Sybmbol: ");
            symbol = char.Parse(Console.ReadLine());
            if (mystr1.LastIndexOf(symbol) == -1)
            {
                Console.WriteLine("There is no such symbol");
            }
            else
            {
                Console.WriteLine(mystr1.LastIndexOf(symbol));
            }

            Console.WriteLine();

            Console.Write("Enter MyString to find the first index of any symbol in char array. MyString: ");
            mystr1 = new MyString(Console.ReadLine());
            Console.Write("Sybmbols: ");
            char[] symbols = Console.ReadLine().ToCharArray();
            if (mystr1.IndexOfAny(symbols) == -1)
            {
                Console.WriteLine("There is no such symbols");
            }
            else
            {
                Console.WriteLine(mystr1.IndexOfAny(symbols));
            }

            Console.WriteLine();

            Console.Write("Enter MyString to find the last index of any symbol in char array. MyString: ");
            mystr1 = new MyString(Console.ReadLine());
            Console.Write("Sybmbols: ");
            symbols = Console.ReadLine().ToCharArray();
            if (mystr1.LastIndexOfAny(symbols) == -1)
            {
                Console.WriteLine("There is no such symbols");
            }
            else
            {
                Console.WriteLine(mystr1.LastIndexOfAny(symbols));
            }

            Console.WriteLine();

            Console.Write("Enter MyString to make it to upper: ");
            mystr1 = new MyString(Console.ReadLine());
            mystr1.ToUpper();
            Console.WriteLine($"The result is: {mystr1}");
            Console.WriteLine();

            Console.Write("Enter MyString to make it to lower: ");
            mystr1 = new MyString(Console.ReadLine());
            mystr1.ToLower();
            Console.WriteLine($"The result is: {mystr1}");
            Console.WriteLine();

            Console.Write("Enter another two MyString lines and position to insert second in the first. First one: ");
            mystr1 = new MyString(Console.ReadLine());
            Console.Write("Second one: ");
            mystr2 = new MyString(Console.ReadLine());
            Console.Write("Position: ");
            int position = int.Parse(Console.ReadLine());

            Console.WriteLine($"The result is: {mystr1.Insert(position, mystr2)}");
            Console.WriteLine();

            Console.Write("Enter another MyString line and start and end positions to remove elements. MyString: ");
            mystr1 = new MyString(Console.ReadLine());
            Console.Write("Start: ");
            position = int.Parse(Console.ReadLine());
            Console.Write("End: ");
            int position1 = int.Parse(Console.ReadLine());

            Console.WriteLine($"The result is: {mystr1.Remove(position, position1)}");
            Console.WriteLine();

            Console.Write("Enter another MyString line and start and end positions to generate SubMyString. MyString: ");
            mystr1 = new MyString(Console.ReadLine());
            Console.Write("Start: ");
            position = int.Parse(Console.ReadLine());
            Console.Write("End: ");
            position1 = int.Parse(Console.ReadLine());
            Console.WriteLine($"The result is: {mystr1.SubMyString(position, position1)}");
            Console.WriteLine();
        }