4

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

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

and with this way work?

"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????

2
  • 1
    Is dna an array of Strings or an array of chars? Commented Jul 14, 2010 at 20:52
  • 1
    What exactly was the error or your expectation - as you can see from the answers, we have two competing theories already ;-) Commented Jul 14, 2010 at 21:01

2 Answers 2

8
 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.


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.


To answer your last comment:

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');
Sign up to request clarification or add additional context in comments.

6 Comments

This would make a one-element array of Strings, and dna[i] references the first (only) element. I think the OP was confusing a String object and an array of characters, not an array of Strings.
@Andreas, I think he has an array of characters, not an array of strings.
I thought he had an array of Strings, not of characters.
@Dean, my guess is that the OP is confusing the two.
I'd imagine that the OP is actually getting tripped up by the immutability of String. I would assume that he is trying to do just dna[i].replace(...); instead of dna[i]=dna[i].replace(...);, which of course leads to the contents of the array not being modified.
|
3

An array is just a data structure that holds data. It doesn't support any operations on that data. You need to write the algorithms to work on the data yourself.

A String is basically a char array with some methods that you can call on that. The replace() method is one of them.

The method you want would look something like this:

static void replace(char[] arr, char find, char replace) {
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == find) {
            arr[i] = replace;
            return;
        }
    }
}

You would then call it like so:

replace(dna, 'T', 'C');

That would replace the first instance of T in the array with a C.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.