简体   繁体   English

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

[英]How to remove first and last character of a string?

I have worked in SOAP message to get LoginToken from Webservice, and store the LoginToken in String and used System.out.println(LoginToken);我曾在 SOAP 消息中工作以从 Webservice 获取 LoginToken,并将 LoginToken 存储在 String 中并使用System.out.println(LoginToken); to print.打印。 This prints [wdsd34svdf], but I want only wdsd34svdf so, how to remove square bracket.这会打印 [wdsd34svdf],但我只想要 wdsd34svdf,所以,如何删除方括号。 please any one help me.请任何人帮助我。

Thanks谢谢

Example:例子:

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

Output: [wdsd34svdf] I want wdsd34svdf输出:[wdsd34svdf] 我想要 wdsd34svdf

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

这是通用解决方案:

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

You can always use substring :您始终可以使用substring

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

Another solution for this issue is use commons-lang (since version 2.0) StringUtils.substringBetween(String str, String open, String close) method.此问题的另一个解决方案是使用 commons-lang(自 2.0 版起) StringUtils.substringBetween(String str, String open, String close)方法。 Main advantage is that it's null safe operation.主要优点是它的空安全操作。

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

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.当然,如果您的令牌中可能有括号,那将不起作用。

This way you can remove 1 leading "[" and 1 trailing "]" character.通过这种方式,您可以删除 1 个前导“[”和 1 个尾随“]”字符。 If your string happen to not start with "[" or end with "]" it won't remove anything:如果您的字符串碰巧不以“[”开头或以“]”结尾,则不会删除任何内容:

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

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

In Kotlin在科特林

private fun removeLastChar(str: String?): String? {
    return if (str == null || str.isEmpty()) str else str.substring(0, str.length - 1)
}

Try this to remove the first and last bracket of string ex.[1,2,3]试试这个删除字符串 ex.[1,2,3] 的第一个和最后一个括号

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

Exptected result = 1,2,3预期结果 = 1,2,3

StringUtils 's removeStart and removeEnd method help to remove string from start and end of a string. StringUtilsremoveStartremoveEnd方法有助于从开始和字符串的结尾去掉字符串。

In this case we could also use combination of this two method在这种情况下,我们也可以使用这两种方法的组合

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

SOLUTION 1解决方案 1

def spaceMeOut(str1): def spaceMeOut(str1):

 print(str1[1:len(str1)-1])

str1='Hello' str1='你好'

print(spaceMeOut(str1))打印(spaceMeOut(str1))

SOLUTION 2解决方案 2

def spaceMeOut(str1): def spaceMeOut(str1):

 res=str1[1:len(str1)-1] print('{}'.format(res))

str1='Hello' str1='你好'

print(spaceMeOut(str1))打印(spaceMeOut(str1))

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM