• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Manipulation

 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Michael Lloyd Lee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scheepers de Bruin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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";
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that was my fault, I forgot to put the (10) in the example. Fixed it.
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21338
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic