Home Java Java-tips Data Strings 96string_examples Java: Example - Change Extension

Ask Questions?

View Latest Questions

Advertisement


 
 

Java: Example - Change Extension
Posted on: February 18, 2008 at 12:00 AM
Problem: Change the extension of a file name

Java: Example - Change Extension

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
Copyleft 2003 Fred Swartz MIT License, Last update: 2003-10-29
Advertisement


DMCA.com