KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > util > StringUtils


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.util;
17
18 /**
19  * A collection of <code>String</code> handling utility methods.
20  *
21  * @author <a HREF="mailto:ricardo@apache.org">Ricardo Rocha</a>
22  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
23  * @version CVS $Id: StringUtils.java 30932 2004-07-29 17:35:38Z vgritsenko $
24  */

25 public class StringUtils {
26
27     /**
28      * Split a string as an array using whitespace as separator
29      *
30      * @param line The string to be split
31      * @return An array of whitespace-separated tokens
32      */

33     public static String JavaDoc[] split(String JavaDoc line) {
34         return split(line, " \t\n\r");
35     }
36
37     /**
38      * Split a string as an array using a given set of separators
39      *
40      * @param line The string to be split
41      * @param delimiter A string containing token separators
42      * @return An array of token
43      */

44     public static String JavaDoc[] split(String JavaDoc line, String JavaDoc delimiter) {
45         return Tokenizer.tokenize(line, delimiter, false);
46     }
47
48     /**
49      * Tests whether a given character is alphabetic, numeric or
50      * underscore
51      *
52      * @param c The character to be tested
53      * @return whether the given character is alphameric or not
54      */

55     public static boolean isAlphaNumeric(char c) {
56         return c == '_' ||
57             (c >= 'a' && c <= 'z') ||
58             (c >= 'A' && c <= 'Z') ||
59             (c >= '0' && c <= '9');
60     }
61
62     /**
63      * Counts the occurrence of the given char in the string.
64      *
65      * @param str The string to be tested
66      * @param c the char to be counted
67      * @return the occurrence of the character in the string.
68      * @deprecated Use {@link org.apache.commons.lang.StringUtils#countMatches(String, String)}
69      */

70     public static int count(String JavaDoc str, char c) {
71         int index = 0;
72         char[] chars = str.toCharArray();
73         for (int i = 0; i < chars.length; i++) {
74             if (chars[i] == c) index++;
75         }
76         return index;
77     }
78
79     /**
80      * Matches two strings.
81      *
82      * @param a The first string
83      * @param b The second string
84      * @return the index where the two strings stop matching starting from 0
85      * @deprecated Use {@link org.apache.commons.lang.StringUtils#indexOfDifference(String, String)}
86      */

87     public static int matchStrings(String JavaDoc a, String JavaDoc b) {
88         int i;
89         char[] ca = a.toCharArray();
90         char[] cb = b.toCharArray();
91         int len = ( ca.length < cb.length ) ? ca.length : cb.length;
92
93         for (i = 0; i < len; i++) {
94             if (ca[i] != cb[i]) break;
95         }
96
97         return i;
98     }
99
100     /**
101      * Replaces tokens in input with Value present in System.getProperty
102      */

103     public static String JavaDoc replaceToken(String JavaDoc s) {
104         int startToken = s.indexOf("${");
105         int endToken = s.indexOf("}",startToken);
106         String JavaDoc token = s.substring(startToken+2,endToken);
107         StringBuffer JavaDoc value = new StringBuffer JavaDoc();
108         value.append(s.substring(0,startToken));
109         value.append(System.getProperty(token));
110         value.append(s.substring(endToken+1));
111         return value.toString();
112     }
113 }
114
Popular Tags