替换为数组 [英] Replace in Array

查看:132
本文介绍了替换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在数组中替换一个字符串,但这不起作用

im trying make one replace in string from a array but this dont work

dna[i].replace('T', 'C');

以这种方式工作?

"ATCTA".replace('T', 'C');

为什么不使用数组,如何使用数组中的替换[]

why dont work with array, how i can use use a replace in array[]

现在我有其他问题,我希望在原始字符串中使用各种替换,我怎么能使这个????

Now i have other problem, i want use various replaces in original string, how i can mahe this????

推荐答案

 String dna[] = {"ATCTA"};
 int i = 0;
 dna[i] = dna[i].replace('T', 'C');
 System.out.println(dna[i]);

这可以按预期工作。如果您遵循类似的模式,请仔细检查您的代码。

This works as expected. Double check your code if you follow a similiar pattern.

您可能已经预料到, dna [i] .replace('T','C'); 直接更改单元格 dna [i] 的内容。情况并非如此,String不会被更改, replace 将返回一个新的String,其中char已被替换。有必要将替换操作的结果分配给变量。

You may have expected, that dna[i].replace('T', 'C'); changes the content of the cell dna[i] directly. This is not the case, the String will not be changed, replace will return a new String where the char has been replaced. It's necessary to assign the result of the replace operation to a variable.

回答您的上一条评论:

字符串是不可变的 - 您无法更改String对象中的单个字符。字符串上的所有操作(子字符串,替换,'+',...)总是创建新的字符串。

Strings are immutable - you can't change a single char inside a String object. All operations on Strings (substring, replace, '+', ...) always create new Strings.

一种方法来进行多次替换是这样的:

A way to make more than one replace is like this:

dna[i] = dna[i].replace('T', 'C').replace('A', 'S');

这篇关于替换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆