Advertisement
AngelGiurov

2

Jul 19th, 2021
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. package com.telerikacademy.core.strings;
  2.  
  3. @SuppressWarnings("StringConcatenationInLoop")
  4. public class StringHelpers {
  5.  
  6. public static String abbreviate(String source, int maxLength) {
  7. //Koko
  8. return null;
  9. }
  10.  
  11. /** Method return string with capitalized first element.
  12. *
  13. * @param source Get string.
  14. * @return string String.
  15. *
  16. * @author Pavel Milanov
  17. */
  18. public static String capitalize(String source) {
  19. String result = "" ;
  20. if (source.length() != 0){
  21. result = source.substring(0,1).toUpperCase() + source.substring(1);
  22. }
  23. return result;
  24. }
  25. /**
  26. * Concatenates string1 to the end of string2.
  27. *
  28. * @param string1 String - The left part of the new string
  29. * @param string2 String - The right part of the new string
  30. * @return String - A string that represents the concatenation of string1 followed by string2's characters
  31. *
  32. * @author Teodora Georgieva
  33. */
  34. public static String concat(String string1, String string2) {
  35. StringBuilder sb = new StringBuilder();
  36. sb.append(string1);
  37. sb.append(string2);
  38. return sb.toString();
  39. }
  40.  
  41. public static boolean contains(String source, char symbol) {
  42. String temp = symbol + "";
  43. if (source.contains(temp)){
  44. return true;
  45. }else {
  46. //Acho
  47. return false;
  48. }
  49. }
  50.  
  51. public static boolean endsWith(String source, char target) {
  52. //Galio
  53. return false;
  54. }
  55.  
  56. public static int firstIndexOf(String source, char target) {
  57. //Koko
  58. return 0;
  59. }
  60.  
  61. /** Method that find the last index of searched char in string.
  62. *
  63. * @param source Get a String.
  64. * @param symbol Get character.
  65. * @return integer
  66. *
  67. * @author Pavel Milanov
  68. */
  69. public static int lastIndexOf(String source, char symbol) {
  70. int lastIndex = -1;
  71. for (int i = 0; i < source.length(); i++) {
  72. if (source.charAt(i) == symbol) lastIndex = i;
  73. }
  74. return lastIndex;
  75. }
  76. /**
  77. * Pads string on the left and right sides if it's shorter than length.
  78. *
  79. * @param source String - The string to pad
  80. * @param length int - The length of the string to achieve
  81. * @param paddingSymbol char - The character used as padding
  82. * @return String The padded string
  83. *
  84. * @author Teodora Georgieva
  85. */
  86. public static String pad(String source, int length, char paddingSymbol) {
  87. StringBuilder sb = new StringBuilder();
  88. int counter = 0;
  89. int copy = 0;
  90. while(length > source.length() && length - source.length() != 1) {
  91. counter++;
  92. copy++;
  93. length -= 2;
  94. } while (counter > 0) {
  95. sb.append(paddingSymbol);
  96. counter--;
  97. }
  98. sb.append(source);
  99. while (copy > 0) {
  100. sb.append(paddingSymbol);
  101. copy--;
  102. }
  103. return sb.toString();
  104.  
  105.  
  106. }
  107.  
  108. public static String padEnd(String source, int length, char paddingSymbol) {
  109. int repeatingLenght = length - source.length();
  110. String padEnd = paddingSymbol + "";
  111. for (int i = 0; i < repeatingLenght; i++) {
  112. source += padEnd;
  113. }
  114.  
  115. //Acho
  116. return source;
  117. }
  118.  
  119. public static String padStart(String source, int length, char paddingSymbol) {
  120. //Galio
  121. return null;
  122.  
  123. }
  124.  
  125. public static String repeat(String source, int times) {
  126. //Koko
  127. return null;
  128. }
  129.  
  130. /** This method return reversed string.
  131. *
  132. * @param source Get String.
  133. * @return string
  134. *
  135. * @author Pavel Milanov
  136. */
  137. public static String reverse(String source) {
  138. StringBuilder sb = new StringBuilder(source);
  139. return String.valueOf(sb.reverse());
  140. }
  141.  
  142. public static String section(String source, int start, int end) {
  143. StringBuilder sb = new StringBuilder();
  144. for (int i = start; i <= end; i++) {
  145. sb.append(source.charAt(i));
  146. }
  147. return sb.toString();
  148. }
  149.  
  150. public static boolean startsWith(String source, char target) {
  151. //Ira
  152. return false;
  153. }
  154.  
  155. }
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement