static void Main(string[] args) { StringDS s = new StringDS("I am isscuss"); StringDS i = new StringDS("nice"); StringDS r = new StringDS("student"); StringDS a = new StringDS("iss"); Console.WriteLine(s.GetLength()); Console.WriteLine(i.GetLength()); Console.WriteLine(r.GetLength()); StringDS s2 = s.SubString(8, 4); Console.WriteLine(s2.ToString()); StringDS s3 = i.SubString(2, 1); Console.WriteLine(s3.ToString()); Console.WriteLine(s.IndexOf(a)); Console.ReadKey(); }
public int IndexOf(StringDS s) { for (int i = 0; i <= this.GetLength() - s.GetLength(); i++) { bool isEqual = true; for (int j = i; j < s.GetLength() + i; j++) { if (this[j] != s[j - i]) { isEqual = false; } } if (isEqual) { return(i); } else { continue; } } return(-1); }
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)); }
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); }
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); } } } }
/// <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); } } } }
/// <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; } } } }
public void exercises() { StringDS s = new StringDS("i am cccc"); StringDS c = new StringDS("am cc"); StringDS r = new StringDS(" bbb"); int di = 0; while (true) { di++; int i = s.IndexOf(c); Console.WriteLine(s[i]); s[i] = r[2]; Console.WriteLine(s[i]); if (di == s.GetLength()) { break; } } }
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; break; } } if (IsEqual) { return i; } else { continue; } } return -1; }