• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Changing filename

 
Chris Sanders
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a file name 1234_abc_abcd_45678_01-05-2010.rtf which I need to remove everything before the 3rd underscore. This is probably easy but I am trying to teach myself Java and don't come from a programming background. Thanks for the help.


var filename = "1234_abc_abcd_45678_01-05-2010.rtf";

for (int i = 0; i < fileName.length(); i++)
{
if (fileName.charAt(i) == '_')
{
counter++;
}
}

if (counter==3)
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's close, but you don't keep a reference to exactly where the third underscore is. Also, you can break out of the loop after the third one.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12101
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as a general tip, before you write any code, write down in english all the steps. yours would be something like

then you look at it, and break down each step further. HOW do you find the third underscore?


ok...so you look at this...is this right? sort of...it finds the third underscore, bu then blows right on past it... let's revise it again...

once you get to a point where it's clear how to code each part, you start writing code. Then, write as little code as possible before you compile, debug and test.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch!

I would tell you exactly what fred said above. Follow that advice!
 
Chris Sanders
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.


I think I found an easier way to do what I am trying to accomplish with the following.

var fileName = "1234_abc_abcd_45678_01-05-2010.rtf";

var filePieces = fileName.split("_");

var newFileName = filePieces[3] + "_" + filePieces[4];

document.write(newFileName);
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great!

But do you also need to handle situations in which the input is not as expected? For example, what happens if the filename contains only a single underscore?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12101
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can your original file names have more than four underscores? if so, this won't work. just something to think about.
 
Chris Sanders
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For my situation the file name convention will always be the same. I will next work on the other scenerio as it may come up sometime.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic