• C#的引用类型及stringbuilder类(增补)


    在这里我们先找补一下命名空间的概念,这个对于我们的类的概念十分重要。

    namespace命名空间:用于解决重名问题,可以看作类的文件夹。

    如果在当前项目中没有这个类的命名空间,需要我们手动的导入这个类的所在的命名空间。快捷方式如下:

    1)用鼠标去点

    2)alt+shift+F10

    在一个项目中引用另一个项目的类

    1)添加引用。

    2)引用命名空间。


    值类型和引用类型

    区别:1.值类型和引用类型在内存上存储的地方不一样

          2.在传递值类型和传递引用类型的时候,传递的方式不一样。值类型为值传递,引用类型为引用传递。

    值类型:int double bool char decimal struct enum

    引用类型:string 自定义类 数组

    值类型存储在内存的栈上,引用类型存储在内存的堆中。

    字符串的不可变性

    当你给一个字符串重新赋值之后,老值并没有销毁, 而是重新开辟一块空间存储新值。无论老值还是新值都还保存在堆中

    但是,栈中的地址没刷新成新地址。

    当程序结束后,GC扫描整个内存,如果发现有的内存空间没有被指向,则立即把他销毁。

    我们可以将string看作是char类型的只读数组。所以我们可以通过下标访问字符串中额某一个元素。(无法赋值:只读属性)

    如果想要给字符串的某个元素赋值方法如下:

    1.首先将string转换成char类型的数组。调用方法stringName.ToCharArray();

    2.然后获得相应索引的元素,进行赋值操作。

    3.再将字符数组转换回string。new string(char);

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace Panel
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string s = "abcdefg";
    14 
    15             char[] chs = s.ToCharArray();
    16             chs[0]= 'b';
    17             s = new string(chs);
    18             Console.WriteLine(s);
    19             Console.ReadKey();
    20         }
    21     }
    22 }

    字符串提供的各种方法

    (1)Length:获得当前字符串中字符的个数。

    (2)ToUpper();将字符串转换成大写形式。

    (3)ToLower();将字符串转换成小写形式。

    (4)Equals(stringName,stringComparision.OrdinalIgnoreCase):比较两个字符串,以各种格式。

    (5)Split(char[],StringSplitOptions.RemoveEmptyEnties):分割字符串,返回字符串类型的数组。

    (6)Replace(string oldValue,string newValue):将字符串中大的oldValue的地方替换成newValue。

    (7)Contains(string value):判断字符串中是否含有子字符串value。

    (8)Substring(int startIndex):取从位置startIndex开始一直到最后的字符串。

    (9)Substring(int startIndex,int Length):取从位置startIndex开始长度为length的子字符串,如果

         子字符串的长度不足length则报错。

    (10)StartWith(string value):判断字符串是否以子串value开始。

    (11)EndWith(string value):判断字符串是否以子串value结束。

    (12)IndexOf(string value):取子串value第一次出现的位置。找不到返回-1.

    (13)Trim():移除所有前导空白字符和尾部空白字符。

    (14)string.IsNullOrEmpty():判断一个字符串是否为空或者null.

    (15)string.Join():将数组按照指定的字符串连接,返回一个字符串。

    (16)string.Format():格式字符串中各种格式化定义字符。

    练习1:接受用户输入的字符串,将其中的字符与输入的顺序输出。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 草稿
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string str = "abcdefg";
    14             char[] chs = str.ToCharArray();
    15 
    16             for (int i = 0; i < chs.Length/2; i++)
    17             {
    18                 char temp = chs[i];
    19                 chs[i] = chs[chs.Length - 1 - i];
    20                 chs[chs.Length - 1 - i] = temp;
    21             }
    22             str = new string(chs);
    23             Console.WriteLine(str);
    24             Console.ReadKey();
    25         }
    26     }
    27 }

    练习2:将“hello c sharp”反向输出

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 草稿
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string str = "hello c sharp";
    14             string[] strNew = str.Split(new char[] { ' '},StringSplitOptions.RemoveEmptyEntries);
    15             for (int i = 0; i < strNew.Length/2; i++)
    16             {
    17                 string temp = strNew[i];
    18                 strNew[i] = strNew[strNew.Length-1-i];
    19                 strNew[strNew.Length - 1 - i] = temp;
    20             }
    21             for (int i = 0; i < strNew.Length; i++)
    22             {
    23                 Console.Write(strNew[i]+" ");
    24             }
    25             Console.ReadKey();
    26         }
    27     }

    练习3:从email中提取用户名和域名:abc@163.com(要动态截取)

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 草稿
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             string email = "abc@163.com";
    14             int index = email.IndexOf('@');
    15             string userName = email.Substring(0,index);
    16             string yuMing = email.Substring(index+1);
    17             Console.WriteLine(userName);
    18             Console.WriteLine(yuMing);
    19             Console.ReadKey();
    20         }
    21     }
    22 }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  • 相关阅读:
    QtqGetPtrHelper
    频谱仪原理
    相位噪声
    上行频率和下行频率
    频谱仪显示:IF OVLD
    C++模板定义时: error: undefined reference to XXX
    Js键值对类型
    策略模式
    射频通信领域的带宽与频率
    Constructor for '' must explicitly initialize the reference member
  • 原文地址:https://www.cnblogs.com/LiyuLi/p/12121449.html
Copyright © 2020-2023  润新知