//字符串的衔接 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)); }
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); }
/// <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 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 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); }
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(); }
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(); }
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; }