Java tutorial
//package com.java2s; /* * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * to you 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 { /** * <p> * Uncapitalise a String. * </p> * <p/> * <p> * That is, convert the first character into lower-case. <code>null</code> is returned as <code>null</code>. * </p> * * @param str the String to uncapitalise * @return uncapitalised String */ public static String uncapitalise(String str) { if (str == null) { return null; } else if (str.length() == 0) { return ""; } else { return new StringBuilder(str.length()).append(Character.toLowerCase(str.charAt(0))) .append(str.substring(1)).toString(); } } /** * <p> * Gets a substring from the specified string avoiding exceptions. * </p> * <p/> * <p> * A negative start position can be used to start <code>n</code> characters from the end of the String. * </p> * * @param str the String to get the substring from * @param start the position to start from, negative means count back from the end of the String by this many * characters * @return substring from start position */ public static String substring(String str, int start) { if (str == null) { return null; } // handle negatives, which means last n characters if (start < 0) { start = str.length() + start; // remember start is negative } if (start < 0) { start = 0; } if (start > str.length()) { return ""; } return str.substring(start); } /** * <p> * Gets a substring from the specified String avoiding exceptions. * </p> * <p/> * <p> * A negative start position can be used to start/end <code>n</code> characters from the end of the String. * </p> * * @param str the String to get the substring from * @param start the position to start from, negative means count back from the end of the string by this many * characters * @param end the position to end at (exclusive), negative means count back from the end of the String by this many * characters * @return substring from start position to end positon */ public static String substring(String str, int start, int end) { if (str == null) { return null; } // handle negatives if (end < 0) { end = str.length() + end; // remember end is negative } if (start < 0) { start = str.length() + start; // remember start is negative } // check length next if (end > str.length()) { // check this works. end = str.length(); } // if start is greater than end, return "" if (start > end) { return ""; } if (start < 0) { start = 0; } if (end < 0) { end = 0; } return str.substring(start, end); } }