예제 #1
0
 public static StringDS Concat(StringDS s1, StringDS s2)//连接字符串
 {
     char[] newData = new char[s1.GetLength() + s2.GetLength()];
     for (int i = 0; i < s1.GetLength(); i++)
     {
         newData[i] = s1[i];
     }
     for (int i = s1.GetLength(); i < newData.Length; i++)
     {
         newData[i] = s2[i - s1.GetLength()];
     }
     return(new StringDS(newData));
 }
예제 #2
0
        /// <summary>
        /// 字符串比较
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public int Compare(StringDS s)
        {
            int len   = this.GetLength() < s.GetLength() ? this.GetLength() : s.GetLength(); //取得两个字符串中,长度更小的字符串长度
            int index = -1;                                                                  //存储不相等的字符的索引位置

            for (int i = 0; i < len; i++)
            {
                if (this[i] != s[i])
                {
                    index = i;
                    break;
                }
            }
            if (index != -1)
            {
                if (this[index] > s[index])
                {
                    return(1);
                }
                else
                {
                    return(-1);
                }
            }
            else
            {
                if (this.GetLength() == s.GetLength())
                {
                    return(0);
                }
                else
                {
                    if (this.GetLength() > s.GetLength())
                    {
                        return(1);
                    }
                    else
                    {
                        return(-1);
                    }
                }
            }
        }
예제 #3
0
        static void Main(string[] args)
        {
            StringDS s = new StringDS("I am a teacher");
            StringDS i = new StringDS("excellent");
            StringDS r = new StringDS("student");

            Console.WriteLine(s.GetLength());
            Console.WriteLine(i.GetLength());
            Console.WriteLine(r.GetLength());
            StringDS s2 = s.SubString(8, 4);

            Console.WriteLine(s2.ToString());
            StringDS i2 = i.SubString(2, 1);

            Console.WriteLine(i2.ToString());
            Console.WriteLine(s.IndexOf(new StringDS("tea")));
            Console.WriteLine(i.IndexOf(new StringDS("cell")));
            Console.ReadKey();
        }
예제 #4
0
 public int IndexOf(StringDS s)
 {
     for (int i = 0; i <= this.GetLength() - s.GetLength(); i++)
     {
         bool isEqual = true;
         for (int j = i; j < i + s.GetLength(); j++)
         {
             if (this[j] != s[j - i])
             {
                 isEqual = false;
             }
         }
         if (isEqual)
         {
             return(i);
         }
         else
         {
             continue;
         }
     }
     return(-1);
 }