英文:
Removing unwanted char in the String
问题
我有一个字符串
String seq = "AGAGTTAAGTG+A";
我想要移除除了A、C、G和T之外的所有字符,使得字符串变成
String newseq = "AGAGTTAAGTGA";
我尝试了下面的代码,但并没有起作用:
String result = str.replaceAll("[^ACGT]", "");
我听说可以使用StringBuilder
方法,但我不确定该如何使用它。有人可以指导我如何做到这一点吗?
英文:
I have a string
String seq = "AGAGTTAAGTG+A";
I want to remove all the characters except A,C,G and T. So that string can change into
String newseq ="AGAGTTAAGTGA"
I have tried below code but that didn't really work:
String result = str.replaceAll("[^!+]", "");
I heard StringBuilder
method can be used, but I am not sure how I'd use it. Can anyone advise me on how to do this?
答案1
得分: 0
你可以使用 ^
字符来否定一个组:
String result = seq.replaceAll("[^ACGT]", "");
英文:
You can use the ^
character to negate a group:
String result = seq.replaceAll("[^ACGT]", "");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论