去除字串中的char 比如空格

2021-04-12 14:42:55 字數 687 閱讀 3668

因為要去除乙個字串中的所有空格,沒有找到現成可用的方法,所以寫了一下.

比較下來,removechar和removechar2的執行效率時間為 5:8.  可能是因為removechar2中,建立了更多的物件的緣故?

package all;

import org.apache.commons.logging.log;

import org.apache.commons.logging.logfactory;

public class datetest

logger.info(system.currenttimemillis()-begin);

begin=system.currenttimemillis();

for (int i = 0; i < 10000000; i++)

logger.info(system.currenttimemillis()-begin);

}public static string removechar(string str,char c)

lastindex++;}}

return new string(array,0,lastindex);

}public static string removechar2(string str,char c)

}return builder.tostring();}}

去除字串中的空格

使用js去除字串內所帶有空格,有以下三種方法 1 replace正則匹配方法 去除字串內所有的空格 str str.replace s g,去除字串內兩頭的空格 str str.replace s s g,去除字串內左側的空格 str str.replace s 去除字串內右側的空格 str str...

去除字串中的空格

利用迭代的思想將字串中的空字元去除 去除字串中的所有空格 deftrim s index 0while index len s if s index if index 0 如果是首字母是空格,直接向後推一位進行迭代 return trim s index 1 len s if index len s...

去除string字串中的空格

很多其他語言的libary都會有去除string類的首尾空格的庫函式,但是標準c 的庫卻不提供這個功能。但是c string也提供很強大的功能,實現trim這種功能也不難。下面是幾種方法 1.使用string的find first not of,和find last not of方法 filenam...