File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes sorting contents of strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sorting contents of strings" Watch "sorting contents of strings" New topic

sorting contents of strings

Tim Tock

Joined: Jan 15, 2004
Posts: 15
hey all, having a bit of trouble, got a 3 character string, just wanna be able to remove the middle one and return a string something like
public static void main(String args[]) {
String phrase = new String ("CAT");
if (phrase.length()==3)
char temp = phrase.charAt(3);
char temp1= phrase.charAt(1);

String temp2 = new String ("temp");
Any suggestions? i've cant concat, i cant put a char straight into a string, im all out
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

This sounds like a homework problem, so I don't want to give the code right to you. Let me give you a couple of hints. There are a couple of different ways to approach the "problem". Keep in mind that at their heart, Stings are really char arrays (char[]). Take a look at the String API and see if with that thought in mind you might find a different way of approaching the problem. You can also take a look at the StringBuffer API for some alternatives.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Note that the class documentation that Mark mentioned is available at
Take a look at the documentation of the methods available to Strings. Perhaps you'll notice a few things that you'll be able to make use of.
Also, note that the index of the first character in a String is 0, not 1.
Don't hesitate to ask for further nudging in the right direction.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
public static void main(String args[]) {
String phrase = new String ("CAT");
if (phrase.length()==3)
char temp = phrase.charAt(3); //is your indexes correct?
char temp1= phrase.charAt(1);
String temp2 = new String ("temp"); //is this going to be output?
temp1.concat(temp); //is this correct
System.out.println(temp1); //what will be output here if it did work?
in your code, look at your indexes, are they correct??
is you concat() method correct?
also check the API to see if you can find a better solution. to your charAt() methods.
I am sure you can see what mistakes you have done, I was going to give the answer until i saw others thought it was a homework question
good luck, but post back with any amendments you have
[ February 28, 2004: Message edited by: Davy Kelly ]

How simple does it have to be???
Tim Tock

Joined: Jan 15, 2004
Posts: 15
edited again, oops, fantastic, just got to grips with stringbuffers, heres wat i got:
public class JdbcExample2 {
public static void main(String args[]) {
String phrase = new String ("ABC");
String x;
if (phrase.length()==3)
char temp = phrase.charAt(0);
char temp1= phrase.charAt(2);
x = new StringBuffer().append(temp).append(temp1).toString();
Thanks for the help, just have to add, I cannot thank javaranch enough for all the help and advice i've recieved since being on here (I just wish I could put something back into the board)
big THANKYOU to everyone
ps. it wasnt a homework assignmet, lol, it was just something i wanted to do for a little project of mine, but thanks all the same!!!
[ February 29, 2004: Message edited by: Tim Tock ]
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
Here's what I'd write:

Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

Stings [sic] are really char arrays (char[]).

This methodology of thinking is prone to problems.
Most VMs implement a String using a char[].
However, a char[] is a mutable type, where a String is not (intended to be anyway - this is another academic argument).
char[] a = new char[]{'a', 'b', 'c'};
a[0] = 'd';
Now let's see the String equivalent:
String a = "abc";
// What now ?
It can't be done (well it can, but like I said, that's another story).

Tony Morris
Java Q&A (FAQ, Trivia)
I agree. Here's the link:
subject: sorting contents of strings
Similar Threads
Garbage for objects and strings ...
nested comments
String Concat
javac and classpath doubt
.append() method