当前位置: 首页 > article >正文

zy91_C#中StringBuilder类以及char类

文章目录

  • 1.StringBuilder类
    • 1.1程序代码
  • 2.char类
    • 2.1程序代码

1.StringBuilder类

1.1程序代码

static void Main(string[] args)
{
    //1.
    StringBuilder bf1 = new StringBuilder();
    StringBuilder bf2 = new StringBuilder(10);
    StringBuilder bf3 = new StringBuilder("Hello Kitty!");
    StringBuilder bf4 = new StringBuilder("1234567890");
    Console.WriteLine("{0} {1} {2} {3}", bf1.Length, bf2.Length, bf3.Length, bf4.Length);
    Console.WriteLine("{0} {1} {2} {3}", bf1.Capacity,bf2.Capacity, bf3.Capacity,bf4.Capacity);
    //2.
    StringBuilder bf5 = new StringBuilder("Hello Kitty!");
    bf5.EnsureCapacity(28);
    Console.WriteLine(bf5.Length);
    Console.WriteLine(bf5.Capacity);
    bf5.Length = 5;
    bf5.Capacity = 9;
    Console.WriteLine(bf5);
    StringBuilder bf6 = new StringBuilder("Hello Kitty!");
    bf6.Length = 20;
    Console.WriteLine(bf6 + "尾");

    //Apepend()向字符串的尾部添加字符
    StringBuilder bf7 = new StringBuilder("Kitty is ");
    int intValue = 3;
    bf7.Append(intValue);
    string stringValue = " years old";
    bf7.Append(stringValue);
    char charValue = ',';
    bf7.Append(charValue);
    char[] charArray = { ' ', 's', 'h', 'e',' ','i', 's',' ' };
    bf7.Append(charArray);
    double doubleValue = 2.5;
    bf7.Append(doubleValue);
    bf7.Append("Kg. That is ");
    bool boolValue = true;
    bf7.Append(boolValue);
    bf7.Append('!');
    Console.WriteLine(bf7);

    Appende()的Length和Capacity关系
    StringBuilder bf8 = new StringBuilder(new string('a', 15));
    Console.WriteLine("Length={0}\tCapacity={1}",bf8.Length,bf8.Capacity);
    bf8.Append(new string('a', 1));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 1));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 14));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 1));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 1));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 30));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 1));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 1));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 62));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 1));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 132));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    Console.WriteLine(bf8);

    /AppendFormat()向字符串尾部添加指定的格式的字符
    StringBuilder bf9 = new StringBuilder();
    decimal i = 19.23m;
    decimal j = 73.7m;
    bf9.AppendFormat("{0,8:c2}\n+{1,7:c2}\n---------\n{2,8:c2}",i,j,i+j);
    Console.WriteLine(bf9);

    Insert()在字符串的任何位置插入各种类型的数据
    StringBuilder bf10 = new StringBuilder("Kitty   year old,she is Kg,that is !");
    string stringValue1 = "is";
    bf10.Insert(6,stringValue1);
    int intValue1 = 3;
    bf10.Insert(9,intValue1);
    char charValue1 = 's';
    bf10.Insert(15,charValue1);
    double doubleValue1 = 2.5;
    bf10.Insert(28,doubleValue1);
    bool boolValue1 = true;
    bf10.Insert(42,boolValue1);
    Console.WriteLine(bf10);

    ///Replace()
    StringBuilder bf11 = new StringBuilder("Happy birthday Jack");
    bf11.Replace("Jack", "Kitty");
    Console.WriteLine(bf11);

    Remove()
    StringBuilder bf12 = new StringBuilder("Harvard is a famous university!");
    bf12.Remove(13, 7);
    Console.WriteLine(bf12);
}

2.char类

2.1程序代码

private void button1_Click(object sender, EventArgs e)
{
    char character=Convert.ToChar(textBox1.Text);
    string output = "";
    output += "为数字:" + char.IsDigit(character) + "\r\n";
    output += "为字母:" + char.IsLetter(character) + "\r\n";
    output += "为小写:" + char.IsLower(character) + "\r\n";
    output += "为大写:" + char.IsUpper(character) + "\r\n";
    output += "为标点:" + char.IsPunctuation(character) + "\r\n";
    output += "转大写:" + char.ToUpper(character) + "\r\n";
    output += "转小写:" + char.ToLower(character) + "\r\n";
    textBox2.Text=output;
}

http://www.kler.cn/news/336835.html

相关文章:

  • 虚拟机ip突然看不了了
  • 跨平台游戏的特点
  • 地图箭头方向检测系统源码分享
  • SM2无证书及隐式证书公钥机制签名和加密过程详解(二)
  • 上传文件失败,请检查阿里云配置信息:[The specified bucket is not valid.
  • 【力扣 | SQL题 | 每日四题】力扣1571, 1715, 1699, 1445, 1495, 1683
  • Spring Cloud Netflix Ribbon 负载均衡详解和案例示范
  • SQLCMD命令行工具导入数据并生成对应的日志文件
  • VGG16模型实现MNIST图像分类
  • 【Docker】04-Docker部署Java后端
  • 【大语言模型-论文精读】谷歌-BERT:用于语言理解的预训练深度双向Transformers
  • Linux ssh 免密登录配置
  • 算法与数据结构--二分查找
  • Redis篇(最佳实践)(持续更新迭代)
  • Android Framework(八)WMS-窗口动效概述
  • 单链表基本操作(2)
  • BI小白速成课:免费!零基础入门,数据分析新手也能快速上手!
  • 系统架构设计师-论文题(2022年下半年)
  • Java性能调优:实战技巧与最佳实践
  • 从编程视角看生命、爱、自由、生活的排列顺序