网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C#数组和串操作有哪些

这篇文章主要介绍“C#数组和串操作有哪些”,在日常操作中,相信很多人在C#数组和串操作有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C#数组和串操作有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

站在用户的角度思考问题,与客户深入沟通,找到崆峒网站设计与崆峒网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、成都网站制作、外贸网站建设、企业官网、英文网站、手机端网站、网站推广、主机域名、网页空间、企业邮箱。业务覆盖崆峒地区。

关于C#数组和C#串操作:
1)串是由连续存储的字符组成
2)C#中的串具有恒定不变的特性,即 一旦被创建,就不能改变长度或者改变其中任何的字符。
3)串的连接、插入和删除等操作都是生成了新串而没有改变原串。
4)继承自 System.object。所以是引用类型(int,bool,char 等都是struct 不是class,是值类型)。
5)System.String 是密封类,所以不能被继承。
6)虽然System.String 是引用类型,但C#中将String 看作是基元类型,所以不用 new操作符创建实例,而是使用字符串驻留的机制。
7)System.String 继承自 IComparable, ICloneable, IConvertible, IComparable, IEnumerable, IEnumerable, IEquatable
8)C#提供了StringBuilder类型来支持高效地动态创建字符串。

下面是自定义一个string类,类中包含一个字段,用以存放字符序列的C#数组,还有一些常用的C#串操作。

public class StringDS  {  private char[] data;//char数组  //索引器  public char this[int index]  {  get   {  return data[index];  }  set  {  data[index] = value;  }  }  //构造函数  public StringDS(char[] arr)  {  data = new char[arr.Length];  for (int i = 0; i < arr.Length; i++)  {  data[i] = arr[i];  }  }  //构造函数   public StringDS(int len)  {  char[] arr = new char[len];  data = arr;  }  //求串长   public int GetLength()  {  return data.Length;  }   //串比较   public int Compare(StringDS s)   {  int len=((this.GetLength()<=s.GetLength())?   this.GetLength():s.GetLength());   int i = 0;   for (i = 0; i < len; ++i)   {   if (this[i] != s[i])   {   break;   }   }   if (i <= len)  {  if (this[i] < s[i])  {  return -1;  }  else if (this[i] > s[i])  {  return 1;  }  }  else if (this.GetLength() == s.GetLength())  {  return 0;  }  else if (this.GetLength() < s.GetLength())  {  return -1;  }   return 1;  }   //求子串   public StringDS SubString(int index, int len)   {   if ((index<0) || (index>this.GetLength()-1) || (len<0) || (len>this.GetLength()-index))   {   Console.WriteLine("Position or Length is error!");   return null;   }    StringDS s = new StringDS(len);    for (int i = 0; i < len; ++i)   {   s[i] = this[i + index-1];   }    return s;   }   //串连接   public StringDS Concat(StringDS s)  {  StringDS s1 = new StringDS(this.GetLength() +s.GetLength());  for (int i = 0; i < this.GetLength(); ++i)  {  s1.data[i] = this[i];  }   for (int j = 0; j < s.GetLength(); ++j)  {  s1.data[this.GetLength() + j] = s[j];  }   return s1;  }   //串插入   public StringDS Insert(int index, StringDS s)   {   int len = s.GetLength();   int lenlen2 = len + this.GetLength();   StringDS s1 = new StringDS(len2);   if (index < 0 || index > this.GetLength() - 1)   {   Console.WriteLine("Position is error!");   return null;   }   for (int i = 0; i < index; ++i)   {   s1[i] = this[i];   }   for(int i = index; i < index + len ; ++i)   {   s1[i] = s[i - index];   }   for (int i = index + len; i < len2; ++i)   {   s1[i] = this[i - len];   }  return s1;  }  //串删除   public StringDS Delete(int index, int len)  {  if ((index < 0) || (index > this.GetLength() - 1)  || (len < 0) || (len > this.GetLength() - index))  {  Console.WriteLine("Position or Length is error!");  return null;  }   StringDS s = new StringDS(this.GetLength() - len);   for (int i = 0; i < index; ++i)  {  s[i] = this[i];  }   for (int i = index + len; i < this.GetLength(); ++i)  {  s[i] = this[i];  }   return s;  }   //串定位   public int Index(StringDS s)   {   if (this.GetLength() < s.GetLength())   {   Console.WriteLine("There is not string s!");   return -1;   }      int i = 0;   int len = this.GetLength() - s.GetLength();   while (i < len)   {   if (this.Compare(s) == 0)   {  break;  }  }   if (i <= len)  {  return i;  }   return -1;  }   }

到此,关于“C#数组和串操作有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


网站栏目:C#数组和串操作有哪些
本文网址:http://bjjierui.cn/article/jdhpjc.html

其他资讯