操作String的幾個Method

2021-08-29 20:05:40 字數 4917 閱讀 2281

public static string join(string array, string sep)

if (array.length == 0)

if (sep == null)

// 預處理,得到長度。

int capacity = 0;

for (int i = 0; i < array.length; i++)

char result = new char[capacity - sep.length()];

int begin = 0;

for (int i = 0; i < array.length; i++)

sep.getchars(0, sep.length(), result, begin);

begin = begin + sep.length();

}return new string(result);

}

使用結果:

stringext.join(null, *) = null

stringext.join(, *) = ""

stringext.join([null], *) = "null"

stringext.join(["a", "b", "c"], "--") = "a--b--c"

stringext.join(["a", "b", "c"], null) = "abc"

stringext.join(["a", "b", "c"], "") = "abc"

stringext.join([null, "", "a"], ",") = "null,,a"

第二個兩個join使用結果是一樣了,第乙個可能快一點點,第二個看起來更簡捷。

org.apache.commons.lang.stringutils中有相似的方法。

/***

* @param source

* @param separator

* @return

*/public static string split(string source, string... separator)

if (source.length() == 0) ;

}if (separator == null)

arraylistresult = new arraylist();

char arrsource = source.tochararray();

char arrseps = new char[separator.length];

for (int i = 0; i < arrseps.length; i++)

}int preend = 0;

int begin = 0;

boolean cansep = false;

int end = 0;

for (int i = 0; i < arrsource.length; i++)

int m = i, n = 0;

for (; m < arrsource.length && n < arrseps[j].length; m++, n++)

}cansep = true;

break;

}if (cansep)

}int len = begin - preend + 1;

char prechar = new char[len];

system.arraycopy(arrsource, preend, prechar, 0, len);

result.add(new string(prechar));

return result.toarray(new string[0]);

}

使用結果:

stringext.split(null, *) = null

stringext.split("", *) = [""]

stringext.split("ab de fg", null) = ["ab", "cd", "ef"]

stringext.split("ab de fg", null) = ["ab", "cd", "ef"]

stringext.split("ab de fg", null) = ["ab", "cd", "ef"]

stringext.split("ab de fg", "") = ["ab", "cd", "ef"]

stringext.split("ab de fg", "") = ["ab", "cd", "ef"]

stringext.split("ab de fg", "") = ["ab", "cd", "ef"]

stringext.split("ab de fg", *, "") = ["ab", "cd", "ef"]

stringext.split("ab de fg", *, "") = ["ab", "cd", "ef"]

stringext.split("ab de fg", *, "") = ["ab", "cd", "ef"]

stringext.split("ab de fg", "", *) = ["ab", "cd", "ef"]

stringext.split("ab de fg", "", *) = ["ab", "cd", "ef"]

stringext.split("ab de fg", "", *) = ["ab", "cd", "ef"]

stringext.split("ab:cd:ef", ":") = ["ab", "cd", "ef"]

stringext.split("ab,,,cd,,,ef", ",,,") = ["ab", "cd", "ef"]

stringext.split("ab,,,cd;;;ef", ",,,", ";;;") = ["ab", "cd", "ef"]

stringext.split("ab,,,,,,ef", ",,,") = ["ab", "", "ef"]

split中的分割符可以是多個,為了分割檔案方便。

org.apache.commons.lang.stringutils中有相似的split方法,但是用來分割csv格式的檔案,不是太好用,如:

1;2;3;4;5

a;b;;d;e

9;8;7;6;5

如果要用「;」分割,第二列就對不齊,把每二列中分割為空的扔掉了,不明白apache為啥要這樣實現,難道是為了跟string.split保持一致?

public static string split(string source)

public static string split(string source, string sep)

public static string split(string source, string sep, int maxsplit)

if (source.length() == 0) ;

}if (sep != null && sep.length() != 0)

return splitfields(source, sep, maxsplit);

arraylistlist = new arraylist();

char chars = source.tochararray();

int n = chars.length;

if (maxsplit < 0)

maxsplit = n;

int splits = 0;

int index = 0;

while (index < n && splits < maxsplit)

while (index < n && character.iswhitespace(chars[index]))

index++;

if (index < n)

return list.toarray(new string[0]);

}private static string splitfields(string source, string sep, int maxsplit)

if (lastbreak <= length)

return list.toarray(new string[0]);

}

splitlines按行分割,換行符可以是「\n」或「\r\n」。

org.apache.commons.lang.stringutils中也有相似的方法。

以上方法有些是自己實現的,有些是從原始碼中copy的。

對齊的實現,左對齊,右對齊等

public static string ljust(string src, int width) 

public static string rjust(string src, int width)

public static string ljust(string src, int width, char fillchar)

public static string rjust(string src, int width, char fillchar)

public static string expand(string src, int width, char fillchar, boolean postfix)

if (postfix) else

}return result;

}

替換的實現,嘿嘿,很搞笑吧:

public static string repalce(string source, string oldstr, string newstr)

此行

留給廣告

商吧

操作String的幾個Method

public static string join string array,string sep if array.length 0 if sep null 預處理,得到長度。int capacity 0 for int i 0 i array.length i char result new c...

String的幾個問題

1.已知strcpy的函式原型 char strcpy char strdest,const char strsrc 其中strdest 是目的字串,strsrc 是源字串。不呼叫c c 的字串庫函式,請編寫函式 strcpy。答案 char strcpy char strdest,const ch...

string類幾個函式小結

s.at n 功能是訪問string類的的第n個元素並返回。include include using namespace std intmain return0 abc defgstring類向char型陣列賦值的函式 不可擷取 include include include using name...