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