Tour rookie C # C # string -----

Namespace (namespace): for problem-solving class of the same name, it can be seen as "class folder"

If the code and the class is being used in a need to use the namespace Using

In different namespaces There are two ways to call the class: 

1: Write the full name of the namespace class name. 

2: first using a reference space, then call

In a project to use in other projects like

Method One: Right-click the project name ---- Add Reference   

                Use using the project name in the code, this time in the main function can be called up to participate in class 

                Example: Using _01 Object-Oriented

Method Two: Write the full name of the namespace class name.

String processing: 

can be seen as an array of read-only string of char, an important feature in C # string: immutability, the string can not be changed once the declaration

It can only be read by the index specified location char char can not formulate a position to make changes.

If you have to modify char, we can create a new string, you can use s.ToCharArray () method to get an array of char strings

For chestnuts to illustrate this fact: 

static void Main(string[] args)
        {

            string text = "哈哈小杨又帅了";
            text[3] = '啦';
            char ch = text[2];
            Console.WriteLine(ch);//显示的结果是小,说明序号是从0开始的
            Console.ReadKey();

        }
//这个时候如果直接给   text【3】赋值,是报错的
//所以说明了string可以看做是char的只读数组;

//想直接改变 类型为 string 类型的 text的字符串
//利用text【3】是不能实现的,因为是只读属性

可以用其他的方法将 字符串变成字符串数组:
String text="哈哈,赫于富又帅了";
Char[] chs=Text.ToCharArray();
Chs[5]='很'
String str=new String(chs);
Console.writeline(str);

Some common methods strings: 

Method name description
ToLower() Obtained lowercase string.
ToUpper() Get a string uppercase
s1.Equals(s2,stringComparison.OrdinallgnoreCase)

Compare two strings are not case sensitive

It is noted that: This method needs to be received with a variable of type bool

s1.Split ()

Remove the unwanted characters in the string

 

String str = "Ha ha ha -----, I have ---";

Char [] chs = new char [] { '', '-'} // not wanted inside the store

String []result=str.Split(chs);

 

This time split () method can achieve this functionality.

But this time will have blank entries, if we add:

chs,StringSplitOptions.RemoveEmptyEntries

.ToString()

All types can be converted to the string method call .ToString type

Int num = 10;

String s=num.ToString();

string Replace(string OldValue,string newValue)

The local oldvalue appear in a string replacement called newvalue

example:

String name = "it is very rich in the rich";

Name = name.replace ( 'very', 'no');

Console.readkey();

Bool Contains(string value)

Determining whether the character string contains the substring value 

Returns the logical value

example: 

 

Bool result = Name.Contains ( "Yang") // determines whether or not this string contains the substring

If (result)

{

      console.writeline ( "string string {0}", result)

}

Else

{

   console.writeline ( "string string {0}", result)

}

string substring(int startIndex)

 

Interception from the position startIndex start until the last string

bool StartsWith(String value)

bool EndsWith(String value)

 

If the value at the beginning of the string is determined substring / end
str.indexof(strr)

Strr determine whether there is str, if there is then put back to the index of the string, if the string is not found, the result returned is -1 

example:

String str="abcdddaa";

Int index =Str.indexof("a",5);

The value returned 6

 

Start looking to find a string from an index, returns the index to find, can not find it returns -1

str.Lastindexof() 找最后一个字符串的索引
str.Insert(位置,字符串) 在str字符串的指定位置处,插入规定的字符串

join 方法的使用:

//把{“阿道夫”,“阿斯蒂芬”,“大”}  变成: 

{“阿道夫|阿斯蒂芬|大”} 


String[] names={“阿道夫”,“阿斯蒂芬”,“大”};
String st=string.join("|",names);
Console.Writelin(st);

最终结果: 
阿道夫|阿斯蒂芬|大

String.isnullorempty 的用法:   

返回的是逻辑值,所以需要用一个bool变量来接收

String str=“”;
Bool result =string.isNullOrEmpty(str);
If (result)
{
Console.writeline("有东西");
}
Else
{
Console.writeline("没有东西");
}

 

Guess you like

Origin blog.csdn.net/qq_30631063/article/details/85216035