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> * Strip any of a supplied String from the start of a String. * </p> * <p/> * <p> * If the strip String is <code>null</code>, whitespace is stripped. * </p> * * @param str the String to remove characters from * @param strip the String to remove * @return the stripped String */ public static String stripStart(String str, String strip) { if (str == null) { return null; } int start = 0; int sz = str.length(); if (strip == null) { while ((start != sz) && Character.isWhitespace(str.charAt(start))) { start++; } } else { while ((start != sz) && (strip.indexOf(str.charAt(start)) != -1)) { start++; } } return str.substring(start); } /** * <p> * Checks if the String contains only whitespace. * </p> * <p/> * <p> * <code>null</code> will return <code>false</code>. An empty String will return <code>true</code>. * </p> * * @param str the String to check * @return <code>true</code> if only contains whitespace, and is non-null */ public static boolean isWhitespace(String str) { if (str == null) { return false; } int sz = str.length(); for (int i = 0; i < sz; i++) { if ((Character.isWhitespace(str.charAt(i)) == false)) { return false; } } return true; } /** * <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); } }