How to remove first and last character of a string?

后端 未结 12 2070
遥遥无期
遥遥无期 2020-12-08 08:56

I have worked in SOAP message to get LoginToken from Webservice, and store the LoginToken in String and used System.out.println(LoginToken); to print. This prin

相关标签:
12条回答
  • 2020-12-08 09:40

    this is perfectly working fine

    String str = "[wdsd34svdf]";
    //String str1 = str.replace("[","").replace("]", "");
    String str1 = str.replaceAll("[^a-zA-Z0-9]", "");
    System.out.println(str1);
    
    
    String strr = "[wdsd(340) svdf]";
    String strr1 = str.replaceAll("[^a-zA-Z0-9]", "");
    System.out.println(strr1);
    
    0 讨论(0)
  • 2020-12-08 09:42

    In Kotlin

    private fun removeLastChar(str: String?): String? {
        return if (str == null || str.isEmpty()) str else str.substring(0, str.length - 1)
    }
    
    0 讨论(0)
  • 2020-12-08 09:45

    StringUtils's removeStart and removeEnd method help to remove string from start and end of a string.

    In this case we could also use combination of this two method

    String string = "[wdsd34svdf]";
    System.out.println(StringUtils.removeStart(StringUtils.removeEnd(string, "]"), "["));
    
    0 讨论(0)
  • 2020-12-08 09:45

    Try this to remove the first and last bracket of string ex.[1,2,3]

    String s =str.replaceAll("[", "").replaceAll("]", "");
    

    Exptected result = 1,2,3

    0 讨论(0)
  • 2020-12-08 09:47

    This is generic solution:

    str.replaceAll("^.|.$", "")
    
    0 讨论(0)
  • 2020-12-08 09:48

    It's easy, You need to find index of [ and ] then substring. (Here [ is always at start and ] is at end) ,

    String loginToken = "[wdsd34svdf]";
    System.out.println( loginToken.substring( 1, loginToken.length() - 1 ) );
    
    0 讨论(0)
提交回复
热议问题