C# implements the String class. After the study, I have a deeper understanding of data structures and C#. Friends who are studying are welcome to discuss. QQ: 848367854
namespace _stringStudy
{
class StringDS
{
private char[] data; //String array is used to store characters
public StringDS(char[] array) //Construction
{
data = new char[array.Length];
for(int i =0; i < data.Length; i++)
{
data[i] = array[i];
}
}
public StringDS(string str) //construction
{
data = new char[str.Length];
for(int i=0; i< str.Length; i++)
{
data[i] = str[i];
}
}
public char this[int index] //The indexer for accessing characters according to the index
{
get { return data[index]; }
}
public int GetLength() //The method to get the length of the string
{
return data.Length;
}
// / <summary>
/// 1. If the current string is equal to s, return 0
/// 2. If the current character is greater than s, return 1
/// 3. If the current character is less than s, return -1
/// 4. If The current string length is greater than the length of s, return 1, otherwise return -1
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public int Compare(StringDS s)
{
int index = -1; //index
int len = this.GetLength() < s.GetLength() ? this.GetLength() : s.GetLength();//Trinary operator, find the length of the string with the smallest length
for (int i = 0; i < len; i++)
{
if (this[i] != s[i]) //As long as one character is different, assign the character index value to index
{
index = i;
break ; //As soon as the difference is found, jump out of the loop immediately
}
}
if(index != -1) //The first len length of the string finds different characters, and records the index value of different characters index
{
if (this[index]> s[index]) //If the value of the character at the index position of the current string is greater than the value of the index position of the s string, return 1
{
return 1;
}
else //If the current string is at the index position The character value of the position is less than the value of the index position of the s string, return -1
{
return -1;
}
}
else //The first len characters of the string are the same, and there are 3 cases
{
if(this.GetLength() = = s.GetLength()) // Case 1: The current string length is equal to the s string length, return 0
{
return 0;
}
else
{
if (this.GetLength() > s.GetLength()) // case 2: current string length is greater than s string, return 1
{
return 1;
}
else // case 3: current string length is less than s characters String, return -1
{
return -1;
}
}
}
}
public StringDS SubString(int index, int length)
{
char[] newChar = new char[length];
for(int i = index; i < index + length; i++ )
{
newChar[i - index] = data[i];
}
return new StringDS(newChar);
}
public StringDS Concat(StringDS s1,StringDS s2)
{
char[] newChar = new char[s1.GetLength() + s2.GetLength()];
for(int i = 0; i < s1.GetLength(); i++)
{
newChar[i] = s1[i];
}
for(int i = s1.GetLength(); i < newChar.Length; i++)
{
newChar[i] = s2[i - s1.GetLength()];
}
return new StringDS(newChar);
}
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;
}
}
{
class Program
{
static void Main(string[] args)
{
char[] array = new char[] { 'A','B' ,'C'};
Console.WriteLine(new StringDS(array).ToString()); //ABC
StringDS s1 = new StringDS("abc");
StringDS s2 = new StringDS("c");
int j = s1.IndexOf(s2);
Console.WriteLine(j); //2
StringDS s3 = new StringDS("I am a girl");
Console.WriteLine(s3.ToString()); //I am a girl
StringDS s4 = new StringDS("the great");
StringDS s5 = new StringDS(" wall");
Console.WriteLine(s3.GetLength()); //11
Console.WriteLine(s4.GetLength()); //9
Console.WriteLine(s5.GetLength()); // 4
StringDS s6 = s3.SubString(7, 4);
Console.WriteLine(s6.ToString()); //girl
StringDS s7 = s4.SubString(3, 2);
Console.WriteLine(s7.ToString()); // g
Console.WriteLine(StringDS.Concat(s4, s5).ToString());//the great wall
Console.WriteLine(s4.Compare(new StringDS("the")));//1
Console.WriteLine(s4.Compare(new StringDS("a")));//1
Console.WriteLine(s4.Compare(new StringDS("z")));//-1
Console.WriteLine(s4.Compare(new StringDS("the great")));//0
Console.WriteLine(s5.Compare(new StringDS(" wall")));//0
Console.WriteLine(s3.IndexOf(new StringDS("girl"))); //7
Console.WriteLine(s4.IndexOf(new StringDS("the"))); //0
Console.ReadKey();
}
}
}
{
class StringDS
{
private char[] data; //String array is used to store characters
public StringDS(char[] array) //Construction
{
data = new char[array.Length];
for(int i =0; i < data.Length; i++)
{
data[i] = array[i];
}
}
public StringDS(string str) //construction
{
data = new char[str.Length];
for(int i=0; i< str.Length; i++)
{
data[i] = str[i];
}
}
public char this[int index] //The indexer for accessing characters according to the index
{
get { return data[index]; }
}
public int GetLength() //The method to get the length of the string
{
return data.Length;
}
// / <summary>
/// 1. If the current string is equal to s, return 0
/// 2. If the current character is greater than s, return 1
/// 3. If the current character is less than s, return -1
/// 4. If The current string length is greater than the length of s, return 1, otherwise return -1
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public int Compare(StringDS s)
{
int index = -1; //index
int len = this.GetLength() < s.GetLength() ? this.GetLength() : s.GetLength();//Trinary operator, find the length of the string with the smallest length
for (int i = 0; i < len; i++)
{
if (this[i] != s[i]) //As long as one character is different, assign the character index value to index
{
index = i;
break ; //As soon as the difference is found, jump out of the loop immediately
}
}
if(index != -1) //The first len length of the string finds different characters, and records the index value of different characters index
{
if (this[index]> s[index]) //If the value of the character at the index position of the current string is greater than the value of the index position of the s string, return 1
{
return 1;
}
else //If the current string is at the index position The character value of the position is less than the value of the index position of the s string, return -1
{
return -1;
}
}
else //The first len characters of the string are the same, and there are 3 cases
{
if(this.GetLength() = = s.GetLength()) // Case 1: The current string length is equal to the s string length, return 0
{
return 0;
}
else
{
if (this.GetLength() > s.GetLength()) // case 2: current string length is greater than s string, return 1
{
return 1;
}
else // case 3: current string length is less than s characters String, return -1
{
return -1;
}
}
}
}
public StringDS SubString(int index, int length)
{
char[] newChar = new char[length];
for(int i = index; i < index + length; i++ )
{
newChar[i - index] = data[i];
}
return new StringDS(newChar);
}
public StringDS Concat(StringDS s1,StringDS s2)
{
char[] newChar = new char[s1.GetLength() + s2.GetLength()];
for(int i = 0; i < s1.GetLength(); i++)
{
newChar[i] = s1[i];
}
for(int i = s1.GetLength(); i < newChar.Length; i++)
{
newChar[i] = s2[i - s1.GetLength()];
}
return new StringDS(newChar);
}
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;
}
}
}
{
class Program
{
static void Main(string[] args)
{
char[] array = new char[] { 'A','B' ,'C'};
Console.WriteLine(new StringDS(array).ToString()); //ABC
StringDS s1 = new StringDS("abc");
StringDS s2 = new StringDS("c");
int j = s1.IndexOf(s2);
Console.WriteLine(j); //2
StringDS s3 = new StringDS("I am a girl");
Console.WriteLine(s3.ToString()); //I am a girl
StringDS s4 = new StringDS("the great");
StringDS s5 = new StringDS(" wall");
Console.WriteLine(s3.GetLength()); //11
Console.WriteLine(s4.GetLength()); //9
Console.WriteLine(s5.GetLength()); // 4
StringDS s6 = s3.SubString(7, 4);
Console.WriteLine(s6.ToString()); //girl
StringDS s7 = s4.SubString(3, 2);
Console.WriteLine(s7.ToString()); // g
Console.WriteLine(StringDS.Concat(s4, s5).ToString());//the great wall
Console.WriteLine(s4.Compare(new StringDS("the")));//1
Console.WriteLine(s4.Compare(new StringDS("a")));//1
Console.WriteLine(s4.Compare(new StringDS("z")));//-1
Console.WriteLine(s4.Compare(new StringDS("the great")));//0
Console.WriteLine(s5.Compare(new StringDS(" wall")));//0
Console.WriteLine(s3.IndexOf(new StringDS("girl"))); //7
Console.WriteLine(s4.IndexOf(new StringDS("the"))); //0
Console.ReadKey();
}
}
}