KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > utils > Replace


1 /*
2  ************************************************************************************
3  * Copyright (C) 2001-2006 Openbravo S.L.
4  * Licensed under the Apache Software License version 2.0
5  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software distributed
7  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  * specific language governing permissions and limitations under the License.
10  ************************************************************************************
11 */

12 package org.openbravo.utils;
13
14 public class Replace{
15   public static void main(String JavaDoc[] args){
16     String JavaDoc strInicial = args[0];
17     //System.out.println("Initial array: " + strInicial);
18
String JavaDoc strFinal = replace(strInicial, "'","\'");
19     //System.out.println("Final array: -" + strFinal + "-");
20
String JavaDoc strFinal2 = replace(strInicial, "a","xx");
21     //System.out.println("Final array: -" + strFinal2 + "-");
22
}// End of main() function
23

24   public static String JavaDoc replace(String JavaDoc strInicial, String JavaDoc strReplaceWhat, String JavaDoc strReplaceWith) {
25     int index = 0;
26     int pos;
27     if (strInicial==null || strInicial.equals("")) return strInicial;
28     else if (strReplaceWhat==null || strReplaceWhat.equals("")) return strInicial;
29     else if (strReplaceWith==null) strReplaceWith = "";
30     StringBuffer JavaDoc strFinal = new StringBuffer JavaDoc("");
31     do {
32       pos = strInicial.indexOf(strReplaceWhat, index);
33       if (pos != - 1) {
34         strFinal.append(strInicial.substring(index, pos) + strReplaceWith);
35         index = pos + strReplaceWhat.length();
36       } else {
37         strFinal.append(strInicial.substring(index));
38       }
39     } while (index < strInicial.length() && pos != -1);
40     return strFinal.toString();
41   }
42 }// End of class
43
Popular Tags