View Javadoc

1   package org.apache.maven.util;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import org.codehaus.plexus.util.StringUtils;
25  
26  /**
27   * @author <a href="mailto:bwalding@jakarta.org">Ben Walding</a>
28   * @version $Id: StringTool.java 517014 2007-03-11 21:15:50Z ltheussl $
29   * @todo move to org.apache.maven.util or make a jelly tag
30   */
31  public class StringTool
32  {
33  
34      /**
35       * Splits a string at the last delimiter. If no delimiter is found,
36       * first element is the string, second element is empty string.
37       * @param s the string to be split
38       * @param delim the delimiter
39       * @return String[] a two element array, element 0 = string up to last delim (exclusive), element 1 = string past
40       * last delim (exclusive)
41       */
42      public List splitStringAtLastDelim( String s, String delim )
43      {
44          if ( s == null )
45          {
46              String[] result = { null, null };
47              return Arrays.asList( result );
48          }
49  
50          int index = s.lastIndexOf( delim );
51  
52          if ( index == -1 )
53          {
54              String[] result = { s, "" };
55              return Arrays.asList( result );
56          }
57          else
58          {
59              String[] result = { s.substring( 0, index ), s.substring( index + 1 ) };
60              return Arrays.asList( result );
61          }
62      }
63  
64      /**
65       * <p>Removes all whitespace characters from the start and end of a String.</p>
66       *
67       * <p>This is similar to {@link #trim(String)} but removes whitespace.
68       * Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
69       *
70       * @param s the String to remove characters from, may be null
71       * @return the trimmed String, <code>null</code> if null String input
72       */
73      public static String trim( String s )
74      {
75          return StringUtils.strip( s );
76      }
77  }