Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Convert String to Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert String to Array" Watch "Convert String to Array" New topic

Convert String to Array

Sj Yard

Joined: Oct 09, 2012
Posts: 3
Given a non-empty string and an int n, return a new string where the char at index n has been removed. The value of n will be a valid index of a char in the original string


[Added code tags - see UseCodeTags for more details]

I need help converting String str into an Array so I can eliminate a letter from the array at the desired index.
Sj Yard

Joined: Oct 09, 2012
Posts: 3
got it!

Sorry for troubles.
Jelle Klap

Joined: Mar 10, 2008
Posts: 1756

You can't access a String object as if it were an array.
The String class does offer a method to return a String object's value as a char array, though.
Still, because Strings are immutable you can't just manipulate that array and expect the String object's value to refelct that change accordingly.
So you'd need a way to create a new String object based on the manipulated char array.
Why don't you have a look at the String API and see if you can figure out how to solve this.
Note that there are other ways to go about this.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Jelle Klap

Joined: Mar 10, 2008
Posts: 1756

That's one way to go, but it's by no means the most clear or efficient one
Matthew Brown

Joined: Apr 06, 2010
Posts: 4344

And welcome to the Ranch!
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 7552

Sj Yard wrote:got it!

Read Jelle's posts, because he's absolutely right:
1. You can do it that way.
2. You should read the API for java.lang.String (←click) because you may work out a much better way. (Big hint: substring() is your friend).


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
jQuery in Action, 2nd edition
subject: Convert String to Array
Similar Threads
convert byte array to string index by index
Passing Vector elements to an array
array confusion
plz delete
can any one explain this