123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
-
-
- /**
- * Created by dan on 6/14/17.
- */
- public class StringUtilities {
- /**
- * @return `Hello World` as a string
- */
- public String getHelloWorld() {
- String getHelloWorld = "Hello World";
- return getHelloWorld;
- }
-
- /**
- * @param firstSegment a string to be added to
- * @param secondSegment a string to add
- * @return the concatenation of two strings, `firstSegment`, and `secondSegment`
- */
- public String concatenation(String firstSegment, String secondSegment){
- String firstAndSecond = firstSegment + secondSegment;
- return firstAndSecond;
- }
-
- /**
- * @param firstSegment a string to be added to
- * @param secondSegment a string to add
- * @return the concatenation of an integer, `firstSegment`, and a String, `secondSegment`
- */
- public String concatenation(int firstSegment, String secondSegment){
- String newString = firstSegment + secondSegment;
- return newString;
- }
-
- /**
- * @param input a string to be manipulated
- * @return the first 3 characters of `input`
- */
- public String getPrefix(String input){
- String firstThree = input.substring(0, 3);
- return firstThree;
- }
-
- /**
- * @param input a string to be manipulated
- * @return the last 3 characters of `input`
- */
- public String getSuffix(String input){
- String lastThree = input.substring(input.length() - 3, input.length());
- return lastThree;
- }
-
- /**
- * @param inputValue the value to be compared
- * @param comparableValue the value to be compared against
- * @return the equivalence of two strings, `inputValue` and `comparableValue`
- */
- public Boolean compareTwoStrings(String inputValue, String comparableValue){
- String inputValue1 = inputValue;
- String comparableValue1 = comparableValue;
- return inputValue1.equals(comparableValue);
- }
-
- /**
- * @param inputValue the value input from user
- * @return the middle character of `inputValue`
- */
- public Character getMiddleCharacter(String inputValue)
- {
- int mid = inputValue.length() / 2;
- char middleChar = inputValue.charAt(mid);
-
- if (inputValue.length() % 2 == 0){
- middleChar = inputValue.charAt(mid - 1);
- }
- return middleChar;
-
- }
-
- /**
- * @param spaceDelimitedString a string, representative of a sentence, containing spaces
- * @return the first sequence of characters
- */
- public String getFirstWord(String spaceDelimitedString){
- int a = spaceDelimitedString.indexOf(" ");
- String firstWord = spaceDelimitedString.substring(0, a);
- return firstWord;
- }
-
- /**
- * @param spaceDelimitedString a string delimited by spaces
- * @return the second word of a string delimited by spaces.
- */
- public String getSecondWord(String spaceDelimitedString){
- int start = spaceDelimitedString.indexOf(" ") + 1;
- int end = spaceDelimitedString.indexOf(" ", start);
-
- if (end == -1) {
- end = spaceDelimitedString.length();
- }
- String secondWord = spaceDelimitedString.substring(start, end);
- return secondWord;
- }
-
- /**
- * @param stringToReverse
- * @return an identical string with characters in reverse order.
- */
- public String reverse(String stringToReverse){
- if (stringToReverse == null || stringToReverse.isEmpty()) {
- return stringToReverse;
- }
- String reversed = "";
- for(int i = stringToReverse.length() - 1; i >= 0; i--) {
- reversed = reversed + stringToReverse.charAt(i);
- }
- return reversed;
- }
-
- /**
- * @param input
- * @return an identical string with spaces removed.
- */
- public String removeWhitespace(String input){
- String newTrim = input.replaceAll("\\s","");
- return newTrim;
- }
-
- /**
- * @param input
- * @return an identical string with spaces in the front and end removed.
- */
- public String trim(String input){
- String trimmedWord = input.trim();
- return trimmedWord;
- }
- }
|