| •1 |
|
| 2 |
|
| 3 |
|
| •4 |
|
| 5 |
|
| •6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| •11 |
|
| 12 |
|
| 13 |
|
| •14 |
|
| 15 |
|
| •16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package org.stathissideris.ascii2image.text; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class StringUtils { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public static boolean isBlank(String s){ |
| 60 |
return (s.length() == 0 || s.matches("^\\s*$")); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |