从字符串中移除不需要的字符

huangapple go评论71阅读模式
英文:

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]", "");

huangapple
  • 本文由 发表于 2020年9月28日 00:10:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64090582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定