-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathStringUtilities.java
More file actions
71 lines (64 loc) · 2 KB
/
StringUtilities.java
File metadata and controls
71 lines (64 loc) · 2 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
public class StringUtilities {
/**
* @param input value to be returned
* @return `input`
*/
public String returnInput(String input) {
return input;
}
/**
* @param baseValue value to be added to
* @param valueToBeAdded value to add
* @return the concatenation of `baseValue` and `valueToBeAdded`
*/
public String concatenate(String baseValue, String valueToBeAdded) {
return baseValue + valueToBeAdded;
}
/**
* @param valueToBeReversed value to be reversed
* @return identical string with characters in opposite order
*/
public String reverse(String valueToBeReversed) {
String reverse = "";
for (int j =valueToBeReversed.length() -1; j >= 0 ; j--){
reverse = reverse + valueToBeReversed.charAt(j);
}
return reverse;
}
/**
* @param word word to get middle character of
* @return middle character of `word`
*/
public Character getMiddleCharacter(String word) {
if(word.length() % 2 == 0){
char c = word.charAt(word.length()/2 -1);
return c;
} else {
char c = word.charAt(word.length()/2);
return c;
}
}
/**
* @param value value to have character removed from
* @param charToRemove character to be removed from `value`
* @return `value` with char of value `charToRemove` removed
*/
public String removeCharacter(String value, Character charToRemove) {
String str = "";
for(int i = 0; i <= value.length()-1; i++){
if(value.charAt(i) != charToRemove){
str += value.charAt(i);
}
}
return str;
}
/**
* @param sentence String delimited by spaces representative of a sentence
* @return last `word` in sentence
*/
public String getLastWord(String sentence) {
String[] str = sentence.split(" ");
String lastWord = str[str.length-1];
return lastWord;
}
}