Source code: org/pptp_gui/util/StringUtil.java
1 /*
2 * PPTP_GUI - Graphical Interface to PPTP Client for Linux, FreeBSD, & NetBSD.
3 * Copyright (C) 2003 Gregory Kaczmarczyk
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 *
20 * StringUtil.java
21 *
22 * Created on January 14, 2003, 4:03 AM
23 */
24
25 package org.pptp_gui.util;
26
27 import java.util.regex.*;
28
29 /**
30 * The class will modify, extract, or determine information from an existing
31 * string.
32 *
33 * @author Gregory Kaczmarczyk (gkaczmar@yahoo.com)
34 *
35 * @version 0.04
36 */
37 public abstract class StringUtil {
38
39 /** Creates a new instance of StringUtil */
40 public StringUtil() {
41 }
42
43 /**
44 * Replace every instance of a string pattern within a string with another string.
45 *
46 * @param str The original string to be modified
47 * @param replaceable The pattern within the string to be replaced
48 * @param replacement The string to replace the replaceable pattern
49 * @return String The newly modified string
50 */
51 public static String modifyString(String str, String replaceable, String replacement) {
52 String newStr = "";
53 Pattern p = Pattern.compile(replaceable);
54 Matcher m = p.matcher(str);
55 StringBuffer sb = new StringBuffer();
56 while(m.find()) {
57 m.appendReplacement(sb, replacement);
58 }
59 m.appendTail(sb);
60 newStr = sb.toString();
61
62 return newStr;
63 }
64
65 /**
66 * Perform a simple grep type of operation on a string
67 *
68 * @param str The string to be tested
69 * @param pattern The pattern for which to search
70 * @return boolean True if the pattern is found, false if not
71 */
72 public static boolean stringGrep(String str, String pattern) {
73 boolean found = false;
74 Pattern p = Pattern.compile(pattern);
75 Matcher m = p.matcher(str);
76 if (m.find())
77 found = true;
78
79 return found;
80 }
81
82 /**
83 * Retrieve a portion of a string based on a regular expression pattern
84 *
85 * @param str The string with the desired text
86 * @param pattern Regular Expression to be used on the string
87 * @return String String extracted via the regular expression
88 */
89 public static String getPattern(String str, String re) {
90 String extracted = "";
91 Pattern p = Pattern.compile(re);
92 Matcher m = p.matcher(str);
93 if (m.find()) {
94 int start = m.start();
95 int end = m.end();
96 extracted = str.substring(start, end);
97 }
98
99 return extracted;
100 }
101 }