-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBasicStringUtils.java
More file actions
71 lines (61 loc) · 2.38 KB
/
BasicStringUtils.java
File metadata and controls
71 lines (61 loc) · 2.38 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
package com.dtcc.exams.fundamentals;
import java.util.regex.Pattern;
public class BasicStringUtils {
/**
* @param string1 - Base string to be added to
* @param string2 - String to add to `string1`
* @return concatenation of `string1` and `string2`
*/
public static String concatentate(String string1, String string2) {
return string1 + string2;
}
/**
* @param string1 - String to be reversed
* @return an identical string with characters in reverse order
*/
public static String reverse(String string1) {
StringBuilder emad = new StringBuilder();
emad.append(string1);
emad = emad.reverse();
string1 = emad.toString();
return string1;
}
/**
* @param string1 - first string to be reversed
* @param string2 - second string to be reversed
* @return concatenation of the reverse of `string1` and reverse of `string2`
*/
public static String reverseThenConcatenate(String string1, String string2) {
StringBuilder emad = new StringBuilder();
emad.append(string1);
emad = emad.reverse();
string1 = emad.toString();
StringBuilder emad1 = new StringBuilder();
emad1.append(string2);
emad1 = emad1.reverse();
string2 = emad1.toString();
return string1 + string2;
}
/**
* @param string - the string to be manipulated
* @param charactersToRemove - Characters that should be removed from `string`
* @return `string` with `charactersToRemove` removed
*/
public static String removeCharacters(String string, String charactersToRemove) {
return string.replaceAll(charactersToRemove, "");
}
/**
* @param string - the string to be manipulated
* @param charactersToRemove - characters to be removed from the string
* @return reverse of `string` with `charactersToRemove` removed
*/
public static String removeCharactersThenReverse(String string, String charactersToRemove) {
string.replaceAll(charactersToRemove, "");
byte[] strAsByteArray = string.getBytes();
byte[] result = new byte[strAsByteArray.length];
for (int i = 0; i < strAsByteArray.length; i++)
result[i] = strAsByteArray[strAsByteArray.length - i - 1];
String string1 = new String(result);
return string1;
}
}