コード例 #1
0
 //字符串的衔接
 public static StringDS Contact(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
ファイル: StringDS.cs プロジェクト: vin120/c-shap-code
        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);
        }
コード例 #3
0
ファイル: StringDS.cs プロジェクト: vin120/c-shap-code
        /// <summary>
        /// 如果两个字符串一样,返回0
        /// 如果当前字符串小于s,那么返回 -1
        /// 如果大于s,返回1
        /// </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;
                    }
                }
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: vin120/c-shap-code
        static void Main(string[] args)
        {
            StringDS s = new StringDS("hei what are you doing");
            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("you")));
            Console.WriteLine(i.IndexOf(new StringDS("cell")));
            Console.ReadKey();
        }
コード例 #5
0
        //比较两个字符串
        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);
                    }
                }
            }
        }
コード例 #6
0
ファイル: StringDS.cs プロジェクト: Physics-EA/Algorithms
        /// <summary>
        /// 如果两个字符串一样 那么返回0
        /// 如果当前字符串小于s,那么返回-1
        /// 如果当前字符串大于s,那么返回1
        /// </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);
                    }
                }
            }
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: Physics-EA/Algorithms
        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();
        }
コード例 #8
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);
 }
コード例 #9
0
ファイル: StringDS.cs プロジェクト: vin120/c-shap-code
 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;
 }