Debian Codegraph @

src/org/stathissideris/ascii2image/text/StringUtils.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /**
2 * ditaa - Diagrams Through Ascii Art
3 *
•4 * Copyright (C) 2004-2011 Efstathios Sideris
5 *
•6 * ditaa is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
10 *
•11 * ditaa is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
•14 * GNU Lesser General Public License for more details.
15 *
•16 * You should have received a copy of the GNU Lesser General Public
17 * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 package org.stathissideris.ascii2image.text;
21
22 /**
23 * @author sideris
24 *
25 * To change the template for this generated type comment go to
26 * Window>Preferences>Java>Code Generation>Code and Comments
27 */
28 public class StringUtils {
29
30 /**
31 * The indexOf idiom
32 *
33 * @param big
34 * @param fragment
35 * @return
36 */
37 public static boolean contains(String big, String fragment){
38 return (big.indexOf(fragment) != -1);
39 }
40
41 public static String repeatString(String string, int repeats){
42 if(repeats == 0) return "";
43 String buffer = "";
44 for(int i=0; i < repeats; i++){
45 buffer += string;
46 }
47 return buffer;
48 }
49
50 /*public static String repeatString(String string, int repeats){
51 if(repeats == 0) return "";
52 StringBuffer buffer = new StringBuffer("");
53 for(int i=0; i < repeats; i++){
54 buffer.append(string);
55 }
56 return buffer.toString();
57 }*/
58
59 public static boolean isBlank(String s){
60 return (s.length() == 0 || s.matches("^\\s*$"));
61 }
62
63 /**
64 *
65 * Converts the first character of <code>string</code> into a capital letter
66 *
67 * @param string
68 * @return
69 */
70 public static String firstToUpper(String string){
71 return string.substring(0,1).toUpperCase()+string.substring(1);
72 }
73
74 public static String insertSpaceAtCaps(String string){
75
76 int uppers = 0;
77
78 //first we count
79 for(int i=0; i < string.length(); i++){
80 if(Character.isUpperCase(string.charAt(i))) uppers++;
81 }
82
83 int[] indexes = null;
84
85 if(Character.isUpperCase(string.charAt(0))){
86 indexes = new int[uppers];
87 } else {
88 indexes = new int[++uppers];
89 }
90 indexes[0] = 0;
91 int k = 1;
92
•93 //then we find the indexes (we have checked the first char already)
94 for(int j =1; j < string.length(); j++){
95 if(Character.isUpperCase(string.charAt(j))) indexes[k++] = j;
96 }
97
98 StringBuffer buffer = new StringBuffer("");
99 //and finally we breakup the String
100 for(int i =0; i < indexes.length; i++){
101 if(i+1 < indexes.length){
102 buffer.append(string.substring(indexes[i], indexes[i+1]));
103 buffer.append(" ");
104 } else {
105 buffer.append(string.substring(indexes[i]));
106 }
107 }
108 return buffer.toString();
109 }
110
111
112 public static boolean isOneOf(char c, char[] group){
113 for(int i = 0; i < group.length; i++)
114 if(c == group[i]) return true;
115 return false;
116 }
117
118 public static boolean isOneOf(String str, String[] group){
119 for(int i = 0; i < group.length; i++)
120 if(str.equals(group[i])) return true;
121 return false;
122 }
123
124 public static String getPath(String fullPath){
125 if(fullPath.lastIndexOf("\\") != -1)
126 return fullPath.substring(0, fullPath.lastIndexOf("\\"));
127 else return "";
128 }
129
130 public static String getBaseFilename(String fullPath){
131 if(fullPath.lastIndexOf(".") != -1 && fullPath.lastIndexOf("\\") != -1)
132 return fullPath.substring(fullPath.lastIndexOf("\\") + 1, fullPath.lastIndexOf("."));
133 else return fullPath;
134 }
135
136 public static String getExtension(String fullPath){
137 if(fullPath.lastIndexOf(".") != -1)
138 return fullPath.substring(fullPath.lastIndexOf(".") + 1);
139 else return "";
140 }
141
142
143 public static void main(String[] args){
144 System.out.println("1 "+StringUtils.firstToUpper("testing"));
145 System.out.println("2 "+StringUtils.firstToUpper(" testing"));
146 System.out.println("3 "+StringUtils.firstToUpper("_testing"));
147 System.out.println("4 "+StringUtils.firstToUpper("Testing"));
148 System.out.println("5 "+StringUtils.firstToUpper("ttesting"));
149 String path = "C:\\Files\\test.txt";
150 System.out.println(path);
151 System.out.println(StringUtils.getPath(path));
152 System.out.println(StringUtils.getBaseFilename(path));
153 System.out.println(StringUtils.getExtension(path));
154
155 path = "test.txt";
156 System.out.println(path);
157 System.out.println(StringUtils.getPath(path));
158 System.out.println(StringUtils.getBaseFilename(path));
159 System.out.println(StringUtils.getExtension(path));
160
161 path = "test";
162 System.out.println(path);
163 System.out.println("path: "+StringUtils.getPath(path));
164 System.out.println("base: "+StringUtils.getBaseFilename(path));
165 System.out.println(" ext: "+StringUtils.getExtension(path));
166
167
168 }
169 }