C 常用工具類

2021-10-09 04:16:33 字數 2713 閱讀 9204

string類用法 ( 實為 system.string ) :

string class

差:傳遞的引數,返回值?具體例項?

// 是否以x結尾。

a.endswith

(x)// 判斷a,b是否相同

a.equals

(b)// 格式化

a = string.

format

(fmt, args)

// 判斷子串是否存在

index = a.

indexof

(sub)

// 插入乙個子串

a.insert

(index , sub)

// 判斷是否null或為空串

string.

isnullorempty

(a)// 從後面查詢乙個子串

index = a.

lastindexof

(sub)

// 刪除一部分子串

a.remove

(index, count)

// 查詢並替換子串

a.replace

(oldstr, newstr)

// 分割

a.split

( ccc )

// 判斷是否以x開頭

a.startswith

( x )

// 獲取子串

a.substring

(start, length)

// 轉成小寫/大寫

a.tolower()

/ a.

toupper()

// 去除空白

a.trim

()

datetime 乙個時間點 ,如 2020-1-29 17:21:45

// 1 建立乙個datetime

datetime dt1 =

newdatetime

(2020,1

,29);

datetime dt1 =

newdatetime()

;// 2 取系統的當前時間

datetime dt2 = datetime.now;

int year = dt2.year;

int hour = dt2.hour;

// 3 轉換 datetime -> string

string str = dt2.

tostring

("yyyy-mm-dd hh:mm:ss");

// 4 解析 string -> datetime

datetime dt3 = datetime.

parseexact

("2020/1/29"

,"yyyy/m/d"

, cultureinfo.invariantculture)

;// 5 曆法計算

datetime dt4 = dt3.

adddays(-

100)

; console.

writeline

("100天前: "

+ dt4.

tostring

("yyyy-mm-dd"))

;// 6 時間差 interval = t1.substract(t2)。例如,計算從1970-1-1開始到現在的時間流逝

timespan interval = datetime.now.

subtract

(new

datetime

(1970,1

,1))

;double milli = interval.totalmilliseconds;

timespan 乙個時間長度,如 3天零4小時50分鐘

檔案與目錄操作:system.io

// 檔案的建立、複製、刪除、移動

file、fileinfo

// 目錄的建立、刪除、移動和列舉

directory, directoryinfo

// 檔案目錄的路徑處理

path

目錄操作有2個類:

//directory: 提供一系列 靜態方法

directory.

createdirectory

("c:\\test\\x\\y\\z");

//directoryinfo: 提供一系列 例項方法

string dirpath =

"c:\\test\\x\\y\\z"

;directoryinfo dir =

newdirectoryinfo

(dirpath)

; dir.

create()

;

檔案操作有2個類:

//file: 提供一系列 靜態方法

file.

move

( path1, path2)

;//fileinfo: 提供一系列 例項方法

fileinfo info =

newfileinfo

(filepath)

;long size = info.length;

datetime time = info.lastwritetime;

注:這裡的檔案操作,是檔案本身的移動、複製、刪除

常用工具類

本講內容 常用工具類 陸續更新 author administrator public static context context public void oncreate public static context getcontext 2 提供所有activity呼叫 基類 author ad...

常用工具類

reflectionutils objectutils public static void main string args 判斷兩個陣列是否相等 system.out.println arrays.equals arr,arr true 列印陣列 string s1 arrays.tostrin...

常用工具類 作業

1.生成10個 10,23 之間的隨機整數 random 類的nextdouble 或nextint 實現public class randomdemo for int i 1 i 10 i 1.以樹狀結構輸出計算機某個指定資料夾下的所有的檔案和子資料夾名稱。public class filedem...