KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > build > StringUtils


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

18 package net.sf.uitags.build;
19
20 public final class StringUtils {
21   /**
22    * Escapes all regular expression special characters in a string to
23    * allow it to be safely used as a regex pattern.
24    *
25    * @param str the string
26    * @return the regex pattern
27    */

28   static String JavaDoc escapePattern(String JavaDoc str) {
29     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
30     char curr;
31     for (int i = 0; i < str.length(); i++) {
32       curr = str.charAt(i);
33       switch (curr) {
34         case '\\':
35           buffer.append("\\\\");
36           break;
37         default:
38           buffer.append(curr);
39       }
40     }
41     return String.valueOf(buffer);
42   }
43 }
44
Popular Tags