简体   繁体   English

需要帮助格式化Java中特定类型的String

[英]need help in formatting a particular type of String in java

I am new in java String formatting concept so need help. 我是java字符串格式化概念的新手,因此需要帮助。 I have a string something like [MALE] and i need remove those braces and get MALE. 我有一个类似[MALE]的字符串,我需要去掉那些花括号并得到MALE。 How to get it done? 如何完成?

You cannot modify a String in java so you will need to use a StringBuilder . 您无法在Java中修改String ,因此需要使用StringBuilder If you want to use the atomic bomb, use a regular expression. 如果要使用原子弹,请使用正则表达式。 Otherwise just checkout the StringBuilder methods in the doc and do what you wish. 否则,只需签出文档中StringBuilder方法并执行您想要的操作即可。

You can turn it back to a String afterwards. 之后,您可以将其转换为字符串。

这会..

yourString=yourString.substring(1, yourString.length-1)
public class Remove {
public static void main(String [] args){
    String string = "[MALE]";
    string = string.replaceAll("\\[", "");
    string = string.replaceAll("\\]", "");
    System.out.println(string);
}

} }

You can use StringBuilder 您可以使用StringBuilder

 StringBuilder sb=new StringBuilder();
 sb.deleteCharAt(sb.indexOf("["));
 sb.deleteCharAt(sb.indexOf("]"));

For String(This looks like a workaround): 对于字符串(这看起来是一种解决方法):

 String s="[SBD]";
  s=  s.replace("[","");
   s= s.replace("]","");

Please note that if more than one "[" or "]" it will replace all occurences. 请注意,如果有多个“ [”或“]”,它将替换所有出现的事件。

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

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