Noodlejay BlaBla

welcome and happy to share


發表留言

C# Verbatim String start with @ 字串使用

C# String類別,相比於char[]大幅度提升了處理字串的方便性,當然也不是char[]就毫無用處,但不是本篇的重點,就不多說明。

Verbatim string literals

在字串的前面加入@符號,後面雙引號內的內容,就會直接是輸出的內容,不會考慮跳脫字元

string str=@"c:\Docs\Source\a.txt";  

string str2="c:\\Docs\\Source\\a.txt";  // Rather than c:\\Docs\\Source\\a.txt

Console.WriteLine(str);
Console.WriteLine(str2);

輸出結果

c:\Docs\Source\a.txt
c:\Docs\Source\a.txt

由於反斜線 \ ,屬於跳脫字元需要用 “\\",才能正確輸出

參考文章:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/reference-types

https://ithelp.ithome.com.tw/articles/10213867


發表留言

C# string Split 字串分割方法

            Console.WriteLine("Hello World!");

            string str1 = "123.123,456.456,789.789";
            string [] num_str=str1.Split(',');
            string x, y, z;

            x = num_str[0];
            y = num_str[1];
            z = num_str[2];

            Console.WriteLine("x,y,z..");
            Console.WriteLine(x);
            Console.WriteLine(y);
            Console.WriteLine(z+"\n");


            Console.WriteLine("foreach method...");
            foreach (string s in num_str) {
                Console.WriteLine(s);
            }

            Console.WriteLine("\nfor method ...");
            for (int i = 0; i < num_str.Length; i++) {
                Console.WriteLine(num_str[i].ToString());
            }

            Console.WriteLine("\nMuti delimeter ...");
            string str2 = "123.123 456.456-789.789,111.111";
            string[] num_str2 = str2.Split(new char[] { ' ', ',', '-' });

            foreach (string s in num_str2)
            {
                Console.WriteLine(s);
            }


            Console.ReadLine(); //end 

結果:

參考文章:

https://www.c-sharpcorner.com/UploadFile/mahesh/split-string-in-C-Sharp/