Java Path Normalize normalizePath(String path, char sep)

Here you can find the source of normalizePath(String path, char sep)

Description

Normalize a path, ensuring that it start with a '/' and does not end with a '/'.

License

Open Source License

Declaration

public static String normalizePath(String path, char sep) 

Method Source Code

//package com.java2s;
/*//w w  w  .j  a  va2  s  .co m
 * ? Copyright IBM Corp. 2012-2013
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

public class Main {
    /**
     * Normalize a path, ensuring that it start with a '/' and does not end with a '/'.
     * @ibm-api
     */
    public static String normalizePath(String path, char sep) {
        boolean needLeadingSlash = path.charAt(0) != sep;
        boolean needRemoveTrailingSlash = path.charAt(path.length() - 1) == sep;
        if (needLeadingSlash || needRemoveTrailingSlash) {
            if (needLeadingSlash && needRemoveTrailingSlash) {
                StringBuilder b = new StringBuilder();
                b.append(sep);
                b.append(path, 0, path.length() - 1);
                return b.toString();
            } else if (needLeadingSlash) {
                StringBuilder b = new StringBuilder();
                b.append(sep);
                b.append(path);
                return b.toString();
            } else {
                return path.substring(0, path.length() - 1);
            }
        }
        return path;
    }
}

Related

  1. normalizePath(String path)
  2. normalizePath(String path)
  3. normalizePath(String path)
  4. normalizePath(String path)
  5. normalizePath(String path)
  6. normalizePath(String path, String oldFileSeperator, String newFileSeparator)
  7. normalizePath(String path, String separator)
  8. normalizePath(String pathFragment)
  9. normalizePath(String pathname)