Posted By


trusktr on 10/13/10

Tagged


Statistics


Viewed 971 times
Favorited by 1 user(s)

cisp401 MyString.java


/ Published in: Java
Save to your folder(s)

Great for formatting.


Copy this code and paste it in your HTML
  1. import java.util.ArrayList;
  2.  
  3. public class MyString{
  4.  
  5. // ########## Data Objects ########## (
  6. private char[] theString, result;
  7. // )
  8. // ########## Constructors ########## (
  9. public MyString(){ // default
  10. theString = new char[] {'x'};
  11. result = new char[] {'x'};
  12. }
  13. public MyString(String s){ // parameterized
  14. theString = s.toCharArray();
  15. result = new char[] {'x'};
  16. }
  17. public MyString(char[] s){ // parameterized
  18. int length = s.length;
  19. theString = new char[length];
  20. for(int i=0; i<length; i++){
  21. theString[i] = s[i];
  22. }
  23. result = new char[] {'x'};
  24. }
  25. public MyString(MyString s){ // copy
  26. theString = s.contents();
  27. result = new char[] {'x'};
  28. }
  29. // )
  30. // ########## Accessors ########## (
  31. private char[] contents(){
  32. char[] s = theString;
  33. return s;
  34. }
  35. public int length(){
  36. return theString.length;
  37. }
  38. public String result(){
  39. return new String(result);
  40. }
  41. public String toString(){
  42. return new String(theString);
  43. }
  44. public char get(int n){
  45. return theString[n];
  46. }
  47. // )
  48. // ########## Mutators ########## (
  49. public MyString left(int n){
  50. int stringLength = theString.length;
  51. result = new char[n];
  52. if(n > stringLength){ // If the space is bigger than the string...
  53. for(int i=0; i<stringLength; i++){
  54. result[i] = theString[i];
  55. }
  56. for(int j=stringLength; j<n; j++){
  57. result[j] = ' ';
  58. }
  59. }
  60. if(n <= stringLength){ // If the space is the same size as the string or smaller...
  61. for(int i=0; i<n; i++){
  62. result[i] = theString[i];
  63. }
  64. }
  65. return new MyString(result);
  66. }
  67. public MyString right(int n){
  68. int stringLength = theString.length;
  69. int emptySpace = n - stringLength;
  70. result = new char[n];
  71. if(n > stringLength){ // If the space is bigger than the string...
  72. for(int j=0; j<emptySpace; j++){
  73. result[j] = ' ';
  74. }
  75. int count = 0;
  76. for(int i=emptySpace; i<n; i++){
  77. result[i] = theString[count];
  78. count++;
  79. }
  80. }
  81. if(n <= stringLength){ // If the space is the same size as the string or smaller...
  82. for(int j=0; j<n; j++){
  83. result[j] = theString[j];
  84. }
  85. }
  86. return new MyString(result);
  87. }
  88. public MyString mid(int n){
  89. int stringLength = theString.length;
  90. int emptySpace = n - stringLength;
  91. int leftPadding=0, rightPadding=0;
  92. result = new char[n];
  93. if(n > stringLength){ // If the space is bigger than the string...
  94. if ((emptySpace % 2) == 0){ // If the empty space size is even...
  95. leftPadding = (emptySpace / 2); // The left padding is equal to the right padding.
  96. rightPadding = leftPadding;
  97. }
  98. if ((emptySpace % 2) == 1){ // If the empty space size is odd...
  99. rightPadding = (emptySpace / 2) + 1; // The left padding is smaller than the right by 1 space.
  100. leftPadding = emptySpace / 2;
  101. }
  102. for(int i=0; i<leftPadding; i++){
  103. result[i] = ' ';
  104. }
  105. int count = 0;
  106. for(int j=leftPadding; j<(leftPadding + stringLength); j++){
  107. result[j] = theString[count];
  108. count++;
  109. }
  110. for(int k=(leftPadding + stringLength); k<n; k++){
  111. result[k] = ' ';
  112. }
  113. }
  114. if(n <= stringLength){ // If the space is the same size as the string or smaller...
  115. for(int j=0; j<n; j++){
  116. result[j] = theString[j];
  117. }
  118. }
  119. return new MyString(result);
  120. }
  121. public boolean has(String s){ // Determines whether or not a search term exists in our string (true or false).
  122. char[] searchString = s.toCharArray();
  123. int searchLength = searchString.length;
  124. int stringLength = theString.length;
  125. int foundAt = 0; // Location where a match is found...
  126. int charMatches = 0; // Counter for matching characters...
  127. boolean found = false; // To stop the test when a match is found...
  128.  
  129. for(int i=0; i<stringLength; i++){ // For each letter in our string...
  130. if (searchLength > stringLength) { // If our search term is bigger than our main string itself...
  131. break; // Then obviously it will not be found, so prevent the CPU from wasting it's resources on the test...
  132. }
  133. if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
  134. int k=i; // To count starting at our current position without messing up i.
  135. charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
  136. for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
  137. if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
  138. break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
  139. }
  140. if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
  141. charMatches++; // Then count a character match.
  142. }
  143. k++; // Move to the next letter in our base string...
  144. }
  145. if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
  146. found = true; // Then we've found a matching sequence of characters!
  147. foundAt = i; // The match is at position i!
  148. }
  149. }
  150. if(found){break;} // We just want to find the first ocurrence for now, so break...
  151. }
  152. return found;
  153. }
  154. public int firstOcurrenceOf(String s, int n){ // Returns the position at which the search term is first encountered. The search begins at the desired starting point n.
  155. char[] searchString = s.toCharArray();
  156. int searchLength = searchString.length;
  157. int stringLength = theString.length;
  158. int start = n; // The location in in our string where we begin the search.
  159. int foundAt = 0; // Location where a match is found...
  160. int charMatches = 0; // Counter for matching characters...
  161. boolean found = false; // To stop the test when a match is found...
  162.  
  163. for(int i=start; i<stringLength; i++){ // For each letter in our string...
  164. if (searchLength > stringLength || start >= stringLength) { // If our search term is bigger than our main string itself, or the starting point is outside the string's boundaries...
  165. break; // Then obviously our search term will not be found, so prevent the CPU from wasting it's resources on the test or encountering errors...
  166. }
  167. if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
  168. int k=i; // To count starting at our current position without messing up i.
  169. charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
  170. for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
  171. if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
  172. break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
  173. }
  174. if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
  175. charMatches++; // Then count a character match.
  176. }
  177. k++; // Move to the next letter in our base string...
  178. }
  179. if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
  180. found = true; // Then we've found a matching sequence of characters!
  181. foundAt = i; // The match is at position i!
  182. }
  183. }
  184. if(found){break;} // We just want to find the first ocurrence for now, so exit the loop...
  185. }
  186. return foundAt;
  187. }
  188. public int firstOcurrenceOf(String s){ // This is simply an overloaded version of the previous method so that if you leave off the "n" parameter, it defaults to 0.
  189. return firstOcurrenceOf(s, 0);
  190. }
  191. public int[] findAll(String s){ // Returns an array containing all the locations in our main string where a search term is found to begin.
  192. char[] searchString = s.toCharArray();
  193. int searchLength = searchString.length;
  194. int stringLength = theString.length;
  195. ArrayList<Integer> locations = new ArrayList<Integer>(); // Locations where a match is found...
  196. int foundAt[]; // The final array to output containing the locations of the ArrayList above...
  197. int charMatches = 0; // Counter for matching characters...
  198.  
  199. boolean found = false; // To stop the test when a match is found...
  200.  
  201. for(int i=0; i<stringLength; i++){ // For each letter in our string...
  202. if (searchLength > stringLength) { // If our search term is bigger than our main string itself...
  203. break; // Then obviously it will not be found, so prevent the CPU from wasting it's resources on the test...
  204. }
  205. if (theString[i] == searchString[0]) { // If a letter in our string matches the first letter of the search term...
  206. int k=i; // To count starting at our current position without messing up i.
  207. charMatches = 0; // For reset match count for each test. If not, some terms will not be found...
  208. for(int j=0; j<searchLength; j++){ // Then for each letter in the search term...
  209. if (k == stringLength) { // If k is equal to string length (if we have surpassed the boudary of our main string)...
  210. break; // Then end the for loop or else we get an error during runtime complaining about Array boundaries.
  211. }
  212. if (theString[k] == searchString[j]) { // If the letter we're at in our search term matches the corresponding letter in our main string...
  213. charMatches++; // Then count a character match.
  214. }
  215. k++; // Move to the next letter in our base string...
  216. }
  217. found = false; // Set to false for each test unless we find a matching term...
  218. if (charMatches == searchLength) { // Finally, if the amount of matching letters equals the size of the search term...
  219. found = true; // Then we've found a matching sequence of characters!
  220. locations.add(i); // The match is at position i. Add it to our ArrayList.
  221. }
  222. }
  223. }
  224. int termCount = locations.size();
  225. foundAt = new int[termCount];
  226. for (int h=0; h<termCount; h++) {
  227. foundAt[h] = locations.get(h);
  228. }
  229. return foundAt;
  230. }
  231. public MyString uppercase(){ // Turns the string to all uppercase.
  232. int stringLength = theString.length;
  233. result = new char[stringLength];
  234. for (int i=0; i<stringLength; i++) {
  235. if (theString[i] >= 97 && theString[i] <= 122) {
  236. result[i] = (char)(theString[i] - ('a' - 'A'));
  237. }
  238. else {
  239. result[i] = theString[i];
  240. }
  241. }
  242. return new MyString(result);
  243. }
  244. public MyString lowercase(){ // Turns the string to all lowercase.
  245. int stringLength = theString.length;
  246. result = new char[stringLength];
  247. for (int i=0; i<stringLength; i++) {
  248. if (theString[i] >= 65 && theString[i] <= 90) {
  249. result[i] = (char)(theString[i] + ('a' - 'A'));
  250. }
  251. else {
  252. result[i] = theString[i];
  253. }
  254. }
  255. return new MyString(result);
  256. }
  257. public MyString invert(){ // Inverts the string so it is spelled backwards.
  258. int stringLength = theString.length;
  259. result = new char[stringLength];
  260. for (int i=stringLength-1; i>=0; i--) {
  261. result[ (stringLength-1)-i ] = theString[i];
  262. }
  263. return new MyString(result);
  264. }
  265. // )
  266. }

URL: atbskate.com/trusktr

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.