001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.lang3; 018 019 /** 020 * Null-safe CharSequence utility methods. 021 * 022 * @author Gary Gregory 023 * @version $Id: CharSequenceUtils.java 967237 2010-07-23 20:08:57Z mbenson $ 024 */ 025 public class CharSequenceUtils { 026 027 /** 028 * Gets a CharSequence length or <code>0</code> if the CharSequence is 029 * <code>null</code>. 030 * 031 * @param cs 032 * a CharSequence or <code>null</code> 033 * @return CharSequence length or <code>0</code> if the CharSequence is 034 * <code>null</code>. 035 * @since 3.0 036 */ 037 public static int length(CharSequence cs) { 038 return cs == null ? 0 : cs.length(); 039 } 040 041 /** 042 * Returns a new <code>CharSequence</code> that is a subsequence of this 043 * sequence starting with the <code>char</code> value at the specified 044 * index. The length (in <code>char</code>s) of the returned sequence is 045 * <code>length() - start</code>, so if <code>start == end</code> then an 046 * empty sequence is returned. </p> 047 * 048 * @param cs 049 * the specified subsequence, may be null 050 * @param start 051 * the start index, inclusive 052 * @return a new subsequence or null 053 * 054 * @throws IndexOutOfBoundsException 055 * if <code>start</code> is negative or if <code>start</code> is 056 * greater than <code>length()</code> 057 * @since 3.0 058 */ 059 public static CharSequence subSequence(CharSequence cs, int start) { 060 return cs == null ? null : cs.subSequence(start, cs.length()); 061 } 062 }