1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with 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,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package helper;
25 
26 public class StringHelper
27 {
28 
doubleQuote(String _sStr)29     public static String doubleQuote(String _sStr)
30         {
31             return "\"" + _sStr + "\"";
32         }
33 
singleQuote(String _sStr)34     public static String singleQuote(String _sStr)
35         {
36             return "'" + _sStr + "'";
37         }
38 
39     /**
40      * removes quotes if both exists at start and at end
41      */
removeSurroundQuoteIfExists(String _sPath)42     public static String removeSurroundQuoteIfExists(String _sPath)
43         {
44             String sNewPath = _sPath;
45             boolean bRemoveQuotes = false;
46             if (
47                 (_sPath.startsWith("\"") && _sPath.endsWith("\"")) ||
48                 (_sPath.startsWith("'") && _sPath.endsWith("'"))
49                 )
50             {
51                 // remove trailing quotes, if exists
52                 sNewPath = sNewPath.substring(1);
53 
54                 // remove trailing quotes, if exists
55                 sNewPath = sNewPath.substring(0, sNewPath.length() - 1);
56             }
57             return sNewPath;
58         }
59 
removeQuoteIfExists(String _sPath)60     public static String removeQuoteIfExists(String _sPath)
61         {
62             String sNewPath = _sPath;
63 
64             if (_sPath.startsWith("\"") ||
65                 _sPath.startsWith("'"))
66             {
67                 // remove trailing quotes, if exists
68                 sNewPath = sNewPath.substring(1);
69             }
70 
71             if (_sPath.endsWith("\"") ||
72                 _sPath.endsWith("'"))
73             {
74                 // remove trailing quotes, if exists
75                 sNewPath = sNewPath.substring(0, sNewPath.length() - 1);
76             }
77             return sNewPath;
78         }
79 
doubleQuoteIfNeed(String _sStr)80     public static String doubleQuoteIfNeed(String _sStr)
81         {
82             if (_sStr.startsWith("\"") && _sStr.endsWith("\""))
83             {
84                 // don't quote twice
85                 return _sStr;
86             }
87             if (_sStr.indexOf(" ") == -1)
88             {
89                 // don't quote, if there is no space in name
90                 return _sStr;
91             }
92             if (_sStr.indexOf("%") != -1)
93             {
94                 return singleQuote(_sStr);
95             }
96 
97             return doubleQuote(_sStr);
98         }
99 
100     /**
101      * Convert a value to a string with a given length, if the len is greater the len of the value string representation
102      * fill it's front with '0'
103      * So ("5", 4) will result in a string "0005"
104      * @param _nValue
105      * @param _nLen
106      * @return
107      */
createValueString(int _nValue, int _nLen)108     public static String createValueString(int _nValue, int _nLen)
109         {
110             String sValue = String.valueOf(_nValue);
111             StringBuffer a = new StringBuffer();
112             while (_nLen > sValue.length())
113             {
114                 a.append('0');
115                 _nLen --;
116             }
117             a.append(sValue);
118             return a.toString();
119         }
120 
121 }
122