-1

I have a string array in JAVA like:

String[] fruits = {"banana", "apple", "orange"};

How can I access to a character/substring of a string member? For example I want to show the second character of first member "banana" which is 'a' and change it to 'b'. Should I make a new string equal to my array member, do the manipulation and assign the new string to my array list like this:

string manipulate = fruits[0];
//do manipulation on 'manipulate' then:
fruits[0] = manipulate;

or there is a builtin or better way?

Thanks

Saeed Vrz
  • 147
  • 1
  • 3
  • 10
  • `fruits[index].charAt(index)`? – dehasi Aug 08 '18 at 08:17
  • One thing is for sure, to update/replace string reference, you will have to create a new `String` object – Ashishkumar Singh Aug 08 '18 at 08:18
  • 1
    What is your question about, arrays or string manipulation or object reference or....? – Joakim Danielson Aug 08 '18 at 08:20
  • @AshishSingh Not necessarily, because you could define a function which manipulates strings as desired, then assign fruits[0] to its manipulated version. – Adi219 Aug 08 '18 at 08:21
  • @ Adi219 In that string function you will return a new `String` object, because as known, `String` object are immutable and cannot be changed/manipulated – Ashishkumar Singh Aug 08 '18 at 08:22
  • @AshishSingh Ohhh I see what you meant now, yes you're right, but the `String` object will be created inside the function, not outside like here in the code above. – Adi219 Aug 08 '18 at 08:23

4 Answers4

2

Java's Strings are immutable, meaning you can't change them. Instead, as @AshishSingh notes in the comments, you'll need to create a new String.

Just do this:

fruits[0] = manipulate(fruits[0]);

Here, manipulate() is your function which takes an input string, manipulates it however you want, and then returns the manipulated string.

public String manipulate(String oldStr) {
    StringBuilder newStr = new StringBuilder(oldStr);
    newStr.setChar(1, 'b')
    return newStr.toString();
}

I'm using StringBuilder which is a mutable object, so can have elements reassigned. I set the second character to 'b' and then return the new String.

Adi219
  • 4,712
  • 2
  • 20
  • 43
  • `StringBuilder` does not mutate existing `String` value. However `StringBuilder` is by itself is mutable object. – Awan Biru Aug 08 '18 at 08:40
  • @AwanBiru I never said it mutates existing `String`s; I said it allows mutable strings (I'm using the English sense of the word strings here, not the Java sense). However, I'll edit it just to make it clearer. – Adi219 Aug 08 '18 at 08:43
  • `StringBuilder.toString() ` will produce new `String`. – Awan Biru Aug 08 '18 at 08:50
1

The Java String object is immutable, so you can't modify its internal value.

char charArray[] = fruits[index].toCharArray();
charArray[2] = 'b';

After modifying the elements in the character array, put it back into fruits array.

fruits[index] = String.valueOf(charArray);

The existing fruits[index] will be replaced by the new String.

Adi219
  • 4,712
  • 2
  • 20
  • 43
Awan Biru
  • 373
  • 2
  • 10
0

If you want to access a particular character of a String, use, String.charAt(index).

That said, you cannot change a character in a String because Strings are immutable in Java.

If you want to change a character in a given String, you will actually have to create a new String.

Example :

String[] fruits = {"banana", "apple", "orange"};

String banana = fruits[0];
char[] chars = banana.toCharArray();
chars[0] = 'c';
chars[4] = 'd';
String newStr = String.valueOf(chars);
System.out.println(newStr);

Output :

canada
Adi219
  • 4,712
  • 2
  • 20
  • 43
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
0

This is how you can do it.

int indexOfArray=1;
int indexOfString=1;
char charToChange='x';          
String fruits[] = {"banana", "apple", "orange"};            

StringBuilder manipulate = new StringBuilder(fruits[indexOfArray]);
manipulate.setCharAt(indexOfString, charToChange);
fruits[indexOfArray]=manipulate.toString();
Tarun
  • 986
  • 6
  • 19