下表根據運算子所執行的運算類型,將運算子分為幾類。每個分類都是依照優先順序列出。
主要 x.y、f(x)、a[x]、x++、x--、new、typeof、checked、unchecked
一元 +、-、!、~、++x、--x、(T)x
算術 (乘法類) *, /, %
算術 (加法類) +, -
移位 <<, >>
關係和型別測試 <, >, <=, >=, is, as
相等 ==, !=
邏輯 (依照優先順序排列) &, ^, |
條件 (依照優先順序排列) &&, ||, ?:
設定 =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
2008年5月26日 星期一
C# 使用字串
逸出字元
像是 "\n" (新增一行) 和 "\t" (定位鍵) 的逸出字元可包含在字串中。
程式碼行:
string hello = "Hello\nWorld!";
產出
Hello
World!
============================
如果要包含反向斜線,在反向斜線之前必須有另一個反向斜線。
下列字串:
string filePath = "\\\\My Documents\\";
產出
\\My Documents\
====================================
@ 符號
@ 符號會告訴字串建構函式忽略逸出字元和分行符號。
下列兩個字串是完全相同的:
string p1 = "\\\\My Documents\\My Files\\";
string p2 = @"\\My Documents\My Files\";
================================================
要在字串中搜尋字串,請使用 IndexOf()。如果找不到搜尋字串,IndexOf() 會傳回 -1,否則,會傳回第一個發生位置之以零起始的索引。
string s9 = "Battle of Hastings, 1066";
System.Console.WriteLine(s9.IndexOf("Hastings")); // outputs 10
System.Console.WriteLine(s9.IndexOf("1967")); // outputs -1
==================================================
將字串分隔為子字串
將字串分隔為子字串 (例如,將句子分隔為個別文字) 是常見的程式設計工作。Split() 方法使用分隔符號 (例如,空白字元) 的 char 陣列,並且傳回子字串陣列。您可以用 foreach 存取這個陣列,如下所示:
char[] delimit = new char[] { ' ' };
string s10 = "The cat sat on the mat.";
foreach (string substr in s10.Split(delimit))
{
System.Console.WriteLine(substr);
}
產出
The
cat
sat
on
the
mat.
=============================
http://msdn.microsoft.com/zh-tw/library/ms228362(VS.80).aspx
像是 "\n" (新增一行) 和 "\t" (定位鍵) 的逸出字元可包含在字串中。
程式碼行:
string hello = "Hello\nWorld!";
產出
Hello
World!
============================
如果要包含反向斜線,在反向斜線之前必須有另一個反向斜線。
下列字串:
string filePath = "\\\\My Documents\\";
產出
\\My Documents\
====================================
@ 符號
@ 符號會告訴字串建構函式忽略逸出字元和分行符號。
下列兩個字串是完全相同的:
string p1 = "\\\\My Documents\\My Files\\";
string p2 = @"\\My Documents\My Files\";
================================================
要在字串中搜尋字串,請使用 IndexOf()。如果找不到搜尋字串,IndexOf() 會傳回 -1,否則,會傳回第一個發生位置之以零起始的索引。
string s9 = "Battle of Hastings, 1066";
System.Console.WriteLine(s9.IndexOf("Hastings")); // outputs 10
System.Console.WriteLine(s9.IndexOf("1967")); // outputs -1
==================================================
將字串分隔為子字串
將字串分隔為子字串 (例如,將句子分隔為個別文字) 是常見的程式設計工作。Split() 方法使用分隔符號 (例如,空白字元) 的 char 陣列,並且傳回子字串陣列。您可以用 foreach 存取這個陣列,如下所示:
char[] delimit = new char[] { ' ' };
string s10 = "The cat sat on the mat.";
foreach (string substr in s10.Split(delimit))
{
System.Console.WriteLine(substr);
}
產出
The
cat
sat
on
the
mat.
=============================
http://msdn.microsoft.com/zh-tw/library/ms228362(VS.80).aspx
C# → Main() 傳回值
Main 方法可以屬於型別 void:
static void Main()
{
//...
}
它也可以傳回 int:
static int Main()
{
//...
return 0;
}
========MainReturnValue.cs===================================================
class MainReturnValue
{
static int Main()
{
//...
return 0;
}
}
===============text.bat=============================================
rem test.bat
@echo off
MainReturnValue
@if "%ERRORLEVEL%" == "0" goto good
:fail
echo Execution Failed
echo return value = %ERRORLEVEL%
goto end
:good
echo Execution Succeded
echo return value = %ERRORLEVEL%
goto end
:end
=====================================================
http://msdn.microsoft.com/zh-tw/library/0fwzzxz2(VS.80).aspx
static void Main()
{
//...
}
它也可以傳回 int:
static int Main()
{
//...
return 0;
}
========MainReturnValue.cs===================================================
class MainReturnValue
{
static int Main()
{
//...
return 0;
}
}
===============text.bat=============================================
rem test.bat
@echo off
MainReturnValue
@if "%ERRORLEVEL%" == "0" goto good
:fail
echo Execution Failed
echo return value = %ERRORLEVEL%
goto end
:good
echo Execution Succeded
echo return value = %ERRORLEVEL%
goto end
:end
=====================================================
http://msdn.microsoft.com/zh-tw/library/0fwzzxz2(VS.80).aspx
訂閱:
文章 (Atom)