mikebai.com

  • Home
  • dev
  • DotNET
  • M365
  • 搞笑
  • 杂七杂八
  • FocusDict
個人BLOG
it developer
  1. Main page
  2. dev
  3. Main content

android java 的trim很考抹鲁啊

2011-06-16 129hotness 0likes 0comments

发现竟然没有重载函数.只能trim空格etc(ascii码一裤裆).


很考抹鲁啊..


还是.net整的明白啊.....


http://stackoverflow.com/questions/2088037/trim-characters-in-java


这个问题在网上一搜,还真是一堆实现方法..还有人特地计算了一下耗时,,,,,


PS:eclipse里面没有Ctrl+C直接复制焦点行(非选择状态)+回车符号的功能啊


考抹鲁啊......................................


有没有有没有啊.............................


 


public String trim( String stringToTrim, String stringToRemove )
{
    String answer = stringToTrim;


    while( answer.startsWith( stringToRemove ) )
    {
        answer = answer.substring( stringToRemove.length() );
    }


    while( answer.endsWith( stringToRemove ) )
    {
        answer = answer.substring( 0, answer.length() - stringToRemove.length() );
    }


    return answer;
}



This answer assumes that the characters to be trimmed are a string. For example, passing in "abc" will trim out "abc" but not "bbc" or "cba", etc.


Some performance times for running each of the following 10 million times.


" mile ".trim(); runs in 248 ms included as a reference implementation for performance comparisons.


trim( "smiles", "s" ); runs in 547 ms - approximately 2 times as long as java's String.trim() method.


"smiles".replaceAll("s$|^s",""); runs in 12,306 ms - approximately 48 times as long as java's String.trim() method.


And using a compiled regex pattern Pattern pattern = Pattern.compile("s$|^s"); pattern.matcher("smiles").replaceAll(""); runs in 7,804 ms - approximately 31 times as long as java's String.trim() method.

Tag: Nothing
Last updated:2011-06-16

mikebai

This person is a lazy dog and has left nothing

Like
< Last article
Next article >

COPYRIGHT © 2025 mikebai.com. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang