wood burning stoves*
The moose likes Beginning Java and the fly likes String Manipulation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Manipulation" Watch "String Manipulation" New topic
Author

String Manipulation

Greg Roberts
Ranch Hand

Joined: Feb 05, 2005
Posts: 72
I have a string that is in the format:

Greg Roberts - 99444005 - Display(10).java

I've used the split method and passed over the resulting array to get:
{Greg, Roberts, Display(10).java}

How do I get rid of the (10) in the last string in the array? I've tried passing over it with loops using charAt but it seemingly won't get rid of the (10). I also need this to work with variations, like Display(950).java or DisplayFrame(555).java.
[ November 17, 2005: Message edited by: Greg Roberts ]

Greg Roberts<br />CIS Student<br />University of West Florida
Michael Lloyd Lee
Greenhorn

Joined: Sep 07, 2005
Posts: 22
I think the problem you are having is down to you trying to change the string, you can not. It is immutablem you have to create a new string, and assign it.
i.e.

Note: My regex is untested, it might work.


Please please please use code tags!<br /> <br /><a href="http://java.sun.com/j2se/1.5.0/docs/api/" target="_blank" rel="nofollow">Java API</a> - <a href="http://java.sun.com/docs/books/tutorial/index.html" target="_blank" rel="nofollow">Java Tutorials</a>
Scheepers de Bruin
Ranch Hand

Joined: Jul 19, 2005
Posts: 99
I'm not entirely sure how that (10) ended up there in the first place, as it is not in your original String:

"Greg Roberts - 99444005 - Display.java";


We're doomed!!<br />Yay!!!<br />No that's bad Girr!!<br />Yay!!!
Greg Roberts
Ranch Hand

Joined: Feb 05, 2005
Posts: 72
Yeah, that was my fault, I forgot to put the (10) in the example. Fixed it.
Greg Roberts
Ranch Hand

Joined: Feb 05, 2005
Posts: 72
I'm trying to make use of the mkdir() and renameTo() methods of the File class, but the API is a little vague on their usage. Can anybody post some examples of the syntax, and what the output would produce?

I've got an ArrayList of files, and I need to create a directory based on the names of the files and drop the files in the directories that were made from their filenames.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20271
    ∞

The regular expression stuff is probably the quickest way to do it. But you kinda hafta learn how to do that sort of thing.

Without it .... hmmmm.... how about

String s = "Display(10).java"; // you'll get this string some other way
String left = s.substring(0, s.indexOf('(')); // left is now "Display"
String right = s.substring(s.indexOf(')') + 1, s.length()); // right is now ".java"

How's that?


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Greg Roberts
Ranch Hand

Joined: Feb 05, 2005
Posts: 72
Here's how I ended up doing it. I wonder which way would be more efficient. This code is inside another for loop that is looping through an ArrayList of files.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Manipulation
 
Similar Threads
Assertion problem?
Logic Errors
Video Course Craig Larman
loss of precision
startsWith and prefix finders