C 中一些格式的轉換

2021-04-27 02:49:15 字數 4852 閱讀 6076

1、datetime 數字型

system.datetime currenttime=new system.datetime();

1.1 取當前年月日時分秒

currenttime=system.datetime.now;

1.2 取當前年

int 年=currenttime.year;

1.3 取當前月

int 月=currenttime.month;

1.4 取當前日

int 日=currenttime.day;

1.5 取當前時

int 時=currenttime.hour;

1.6 取當前分

int 分=currenttime.minute;

1.7 取當前秒

int 秒=currenttime.second;

1.8 取當前毫秒

int 毫秒=currenttime.millisecond;

(變數可用中文)

1.9 取中文日期顯示——年月日時分

string stry=currenttime.tostring("f"); //不顯示秒

1.10 取中文日期顯示_年月

string strym=currenttime.tostring("y");

1.11 取中文日期顯示_月日

string strmd=currenttime.tostring("m");

1.12 取當前年月日,格式為:2003-9-23

string strymd=currenttime.tostring("d");

1.13 取當前時分,格式為:14:24

string strt=currenttime.tostring("t");

2、int32.parse(變數) int32.parse("常量")

字元型轉換 轉為32位數字型

變數.tostring()

字元型轉換 轉為字串

12345.tostring("n"); //生成 12,345.00

12345.tostring("c"); //生成 ¥12,345.00

12345.tostring("e"); //生成 1.234500e+004

12345.tostring("f4"); //生成 12345.0000

12345.tostring("x"); //生成 3039 (16進製制)

12345.tostring("p"); //生成 1,234,500.00%

4、變數.length 數字型

取字串長度:

如: string str="中國";

int len = str.length ; //len是自定義變數, str是求測的字串的變數名

7、變數.substring(引數1,引數2);

擷取字串的一部分,引數1為左起始位數,引數2為擷取幾位。

如:string s1 = str.substring(0,2);

8、string user_ip=request.servervariables["remote_addr"].tostring();

取遠端使用者ip位址

10、 session["變數"];

訪問session值;

如,賦值: session["username"]="小布希";

取值: object objname=session["username"];

string strname=objname.tostring();

清空: session.removeall();

在edit.aspx頁中取值:string str=request.querystring["fdid"];

12、doc物件.createelement("新建節點名");

建立xml文件新節點

14、 父節點.removechild(節點);

刪除節點

15、response.write("字串");

response.write(變數);

向頁面輸出。

response.redirect("url位址");

跳轉到url指定的頁面

16、char.iswhitespce(字串變數,位數)——邏輯型

查指定位置是否空字元;

如: string str="中國 人民";

response.write(char.iswhitespace(str,2)); //結果為:true, 第乙個字元是0位,2是第三個字元。

17、char.ispunctuation("字元") --邏輯型

查字元是否是標點符號

如:response.write(char.ispunctuation("a")); //返回:false

18、(int)"字元"

把字元轉為數字,查**點,注意是單引號。

如: response.write((int)"中"); //結果為中字的**:20013

19、(char)**

把數字轉為字元,查**代表的字元。

如: response.write((char)22269); //返回「國」字。 20、 trim()

清除字串前後空格

21 、字串變數.replace("子字串","替換為")

字串替換

如: string str="中國";

str=str.replace("國","央"); //將國字換為央字

response.write(str); //輸出結果為「**」

再如:(這個非常實用)

string str="這是指令碼";

str=str.replace("<","<"); //將左尖括號替換為與 < 與 (或換為<,但估計經xml存諸后,再提出仍會還原)

response.write(str); //顯示為:「這是指令碼」

如果不替換,將不顯示,如果是一段指令碼,將執行;而替換後,指令碼將不執行。

這段**的價值在於:你可以讓乙個文字中的所有html標籤失效,全部顯示出來,保護你的具有互動性的站點。

具體實現:將你的表單提交按鈕指令碼加上下面**:

string strsubmit=label1.text; //label1是你讓使用者提交資料的控制項id。

strsubmit=strsubmit.replace("<","<");

然後儲存或輸出strsubmit。 用此方法還可以簡單實現ubb**。

22、math.max(i,j)

取i與j中的最大值

如 int x=math.max(5,10); // x將取值 10

23、字串對比一般都用: if(str1==str2) , 但還有別的方法:

(1)、

string str1; str2

//語法: str1.endswith(str2); __檢測字串str1是否以字串str2結尾,返回布林值.如:

if(str1.endswith(str2))

(2)、

//語法:str1.equals(str2); __檢測字串str1是否與字串str2相等,返回布林值,用法同上.

(3)、

//語法 equals(str1,str2); __檢測字串str1是否與字串str2相等,返回布林值,用法同上.

24、indexof() 、lastindexof()

查詢字串中指定字元或字串首次(最後一次)出現的位置,返回索引值,如:

str1.indexof("字"); //查詢「字」在str1中的索引值(位置)

str1.indexof("字串");//查詢「字串」的第乙個字元在str1中的索引值(位置)

str1.indexof("字串",3,2);//從str1第4個字元起,查詢2個字元,查詢「字串」的第乙個字元在str1中的索引值(位置)

25、insert()

在字串中指定索引位插入指定字元。如:

str1.insert(1,"字");在str1的第二個字元處插入「字」,如果str1="中國",插入後為「中字國」;

26、padleft()、padright()

在字串左(或右)加空格或指定char字元,使字串達到指定長度,如:

<%

string str1="中國人";

str1=str1.padleft(10,"1"); //無第二引數為加空格

response.write(str1); //結果為「1111111中國人」 , 字串長為10

%>

27、remove()

從指定位置開始刪除指定數的字元

字串對比一般都用: if(str1==str2) , 但還有別的方法:

1、 string str1; str2

//語法: str1.endswith(str2); __檢測字串str1是否以字串str2結尾,返回布林值.如:

if(str1.endswith(str2))

2、 //語法:str1.equals(str2); __檢測字串str1是否與字串str2相等,返回布林值,用法同上.

3、 //語法 equals(str1,str2); __檢測字串str1是否與字串str2相等,返回布林值,用法同上.

indexof()

查詢字串中指定字元或字串首次出現的位置,返首索引值,如:

str1.indexof("字"); //查詢「字」在str1中的索引值(位置)

str1.indexof("字串");//查詢「字串」的第乙個字元在str1中的索引值(位置)

str1.indexof("字串",3,2);//從str1第4個字元起,查詢2個字元,查詢「字串」的第乙個字元在str1中的索引值(位置)

c 中一些資料型別的轉換

一些型別轉換 string s char 函式c str s char c 10 double 函式atof c 一些輸入格式 cin.get 去掉行末的換行符 if cin.peek n 判斷是否是空行 getchar 表示從輸入欄上獲取乙個字元。包括單個的空格 字元 回車。並返回它。一些知識 1...

MFC中一些型別的Format格式

很多時候一些格式需要以日誌的形式列印出來,就需要轉成cstring.那麼就需要用到format 1.dword dword a2 276321234 cstring str2 str2.format t u a2 2.char 如果工程時unicode編碼,有三種方法 char pchar liux...

C 中一些常見的方法

1.對規則的字串進行處理的bool splitstring string strorigin,string strsplit,vector vct string str strorigin.substr 0,iindex vct string.push back str ilen int stror...