如何删除字符串的第一个和最后一个字符?

时间:2022-01-11 00:03:51

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 prints [wdsd34svdf], but I want only wdsd34svdf so, how to remove square bracket. please any one help me.

我使用SOAP消息从Webservice获取LoginToken,并将LoginToken存储在String中并使用System.out.println(LoginToken);打印。打印[wdsd34svdf],但我只想要wdsd34svdf,如何删除方括号。请任何人帮助我。

Thanks

谢谢

Example:

例:

String LoginToken=getName().toString();
System.out.println("LoginToken" + LoginToken);

Output: [wdsd34svdf] I want wdsd34svdf

输出:[wdsd34svdf]我想要wdsd34svdf

8 个解决方案

#1


121  

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));

#2


11  

You can always use substring:

您始终可以使用子字符串:

String loginToken = getName().toString();
loginToken = loginToken.substring(1, loginToken.length() - 1);

#3


10  

This is generic solution:

这是通用解决方案:

str.replaceAll("^.|.$", "")

#4


7  

Another solution for this issue is use commons-lang (since version 2.0) StringUtils.substringBetween(String str, String open, String close) method. Main advantage is that it's null safe operation.

此问题的另一个解决方案是使用commons-lang(自2.0版本起)StringUtils.substringBetween(String str,String open,String close)方法。它的主要优点是无效安全操作。

StringUtils.substringBetween("[wdsd34svdf]", "[", "]"); // returns wdsd34svdf

StringUtils.substringBetween(“[wdsd34svdf]”,“[”,“]”); //返回wdsd34svdf

#5


4  

I had a similar scenario, and I thought that something like

我有类似的情况,我觉得有点像

str.replaceAll("\[|\]", "");

looked cleaner. Of course, if your token might have brackets in it, that wouldn't work.

看起来更干净当然,如果您的令牌中可能包含括号,则无效。

#6


0  

This way you can remove 1 leading "[" and 1 trailing "]" character. If your string happen to not start with "[" or end with "]" it won't remove anything:

这样你就可以删除1个前导“[”和1个尾随“]”字符。如果您的字符串碰巧不以“[”开头或以“]结尾”,则不会删除任何内容:

str.replaceAll("^\\[|\\]$", "")

#7


0  

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

StringUtils的removeStart和removeEnd方法有助于从字符串的开头和结尾删除字符串。

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

在这种情况下,我们也可以使用这两种方法的组合

String string = "[wdsd34svdf]";
System.out.println(StringUtils.removeStart(StringUtils.removeEnd(string, "]"), "["));

#8


-1  

This will gives you basic idea

这将为您提供基本的想法

    String str="";
    String str1="";
    Scanner S=new Scanner(System.in);
    System.out.println("Enter the string");
    str=S.nextLine();
    int length=str.length();
    for(int i=0;i<length;i++)
    {
        str1=str.substring(1, length-1);
    }
    System.out.println(str1);

#1


121  

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));

#2


11  

You can always use substring:

您始终可以使用子字符串:

String loginToken = getName().toString();
loginToken = loginToken.substring(1, loginToken.length() - 1);

#3


10  

This is generic solution:

这是通用解决方案:

str.replaceAll("^.|.$", "")

#4


7  

Another solution for this issue is use commons-lang (since version 2.0) StringUtils.substringBetween(String str, String open, String close) method. Main advantage is that it's null safe operation.

此问题的另一个解决方案是使用commons-lang(自2.0版本起)StringUtils.substringBetween(String str,String open,String close)方法。它的主要优点是无效安全操作。

StringUtils.substringBetween("[wdsd34svdf]", "[", "]"); // returns wdsd34svdf

StringUtils.substringBetween(“[wdsd34svdf]”,“[”,“]”); //返回wdsd34svdf

#5


4  

I had a similar scenario, and I thought that something like

我有类似的情况,我觉得有点像

str.replaceAll("\[|\]", "");

looked cleaner. Of course, if your token might have brackets in it, that wouldn't work.

看起来更干净当然,如果您的令牌中可能包含括号,则无效。

#6


0  

This way you can remove 1 leading "[" and 1 trailing "]" character. If your string happen to not start with "[" or end with "]" it won't remove anything:

这样你就可以删除1个前导“[”和1个尾随“]”字符。如果您的字符串碰巧不以“[”开头或以“]结尾”,则不会删除任何内容:

str.replaceAll("^\\[|\\]$", "")

#7


0  

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

StringUtils的removeStart和removeEnd方法有助于从字符串的开头和结尾删除字符串。

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

在这种情况下,我们也可以使用这两种方法的组合

String string = "[wdsd34svdf]";
System.out.println(StringUtils.removeStart(StringUtils.removeEnd(string, "]"), "["));

#8


-1  

This will gives you basic idea

这将为您提供基本的想法

    String str="";
    String str1="";
    Scanner S=new Scanner(System.in);
    System.out.println("Enter the string");
    str=S.nextLine();
    int length=str.length();
    for(int i=0;i<length;i++)
    {
        str1=str.substring(1, length-1);
    }
    System.out.println(str1);