forked from amyaim99/CR-Ch1-Day1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtilities.java
More file actions
116 lines (98 loc) · 3.73 KB
/
Copy pathStringUtilities.java
File metadata and controls
116 lines (98 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.zipcodewilmington.danny_do_better_exercises;
/**
* Created by dan on 6/14/17.
*/
public class StringUtilities {
/**
* @return `Hello World` as a string
*/
public static String getHelloWorld() {
return "Hello World";
}
/**
* @param firstSegment a string to be added to
* @param secondSegment a string to add
* @return the concatenation of two strings, `firstSegment`, and `secondSegment`
*/
public static String concatenation(String firstSegment, String secondSegment){
StringBuilder concat = new StringBuilder();
concat.append(firstSegment).append(secondSegment);
return concat.toString();
}
/**
* @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 static String concatenation(int firstSegment, String secondSegment){
StringBuilder concat = new StringBuilder();
concat.append(firstSegment).append(secondSegment);
return concat.toString();
}
/**
* @param input a string to be manipulated
* @return the first 3 characters of `input`
*/
public static String getPrefix(String input){
return input.substring(0,3);
}
/**
* @param input a string to be manipulated
* @return the last 3 characters of `input`
*/
public static String getSuffix(String input){
return input.substring(input.length() - 3, input.length());
}
/**
* @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 static Boolean compareTwoStrings(String inputValue, String comparableValue){
return inputValue.equals(comparableValue);
}
/**
* @param inputValue the value input from user
* @return the middle character of `inputValue`
*/
public static Character getMiddleCharacter(String inputValue){
if (inputValue.length() % 2 == 0) {
return inputValue.charAt((inputValue.length() / 2) - 1);
}
return inputValue.charAt(inputValue.length() / 2);
}
/**
* @param spaceDelimitedString a string, representative of a sentence, containing spaces
* @return the first sequence of characters
*/
public static String getFirstWord(String spaceDelimitedString){
for (int i = 0; i<spaceDelimitedString.length(); i++){
if (spaceDelimitedString.charAt(i) == ' '){
int chopper = i;
return spaceDelimitedString.substring(0, chopper);
}
}
return null;
}
/**
* @param spaceDelimitedString a string delimited by spaces
* @return the second word of a string delimited by spaces.
*/
public static String getSecondWord(String spaceDelimitedString){
int start = spaceDelimitedString.indexOf(' ') +1;
//I spent way too long on this because I was trying way to hard to do it with loops (see my code for getFirstWord)
//And I'm pretty sure this only works because the String in question only has 2 words. But I'll take what I can get
return spaceDelimitedString.substring(start, spaceDelimitedString.length());
}
/**
* @param stringToReverse
* @return an identical string with characters in reverse order.
*/
public static String reverseTheTwo(String stringToReverse){
StringBuilder answer = new StringBuilder();
for (int i = stringToReverse.length() -1; i>=0; i--){
answer.append(stringToReverse.charAt(i));
}
return answer.toString();
}
}