Thursday, October 28, 2010

Change Extension (string)

Problem: Change the extension of a file name

//============================================== changeExtension
// changes extension to new extension
// example: x = changeExtension("data.txt", ".java")
// will assign "data.java" to x.
static String changeExtension(String originalName, String newExtension) {
int lastDot = originalName.lastIndexOf(".");
if (lastDot != -1) {
return originalName.substring(0, lastDot) + newExtension;
} else {
return originalName + newExtension;
}
}//end changeExtension

No comments:

Post a Comment

Chitika