浏览代码

string util

Jamez-s 6 年前
父节点
当前提交
1af05322f6
共有 1 个文件被更改,包括 39 次插入16 次删除
  1. 39
    16
      StringUtilities.java

+ 39
- 16
StringUtilities.java 查看文件

@@ -7,19 +7,17 @@ public class StringUtilities {
7 7
     /**
8 8
      * @return `Hello World` as a string
9 9
      */
10
-    public static void Main(String[] args){
11
-        System.out.println("Hello World");
10
+    public String getHelloWorld(){
11
+        return "Hello World";
12 12
         }
13
-    }
14
-   
15
-
13
+    
16 14
     /**
17 15
      * @param firstSegment a string to be added to
18 16
      * @param secondSegment a string to add
19 17
      * @return the concatenation of two strings, `firstSegment`, and `secondSegment`
20 18
      */
21 19
     public String concatenation(String firstSegment, String secondSegment){
22
-        return null;
20
+        return firstSegment + secondSegment;
23 21
     }
24 22
 
25 23
     /**
@@ -28,7 +26,7 @@ public class StringUtilities {
28 26
      * @return the concatenation of an integer, `firstSegment`, and a String, `secondSegment`
29 27
      */
30 28
     public String concatenation(int firstSegment, String secondSegment){
31
-        return null;
29
+        return firstSegment + secondSegment;
32 30
     }
33 31
 
34 32
     /**
@@ -36,7 +34,8 @@ public class StringUtilities {
36 34
      * @return the first 3 characters of `input`
37 35
      */
38 36
     public String getPrefix(String input){
39
-        return null;
37
+       
38
+        return input.substring(0, 3);
40 39
     }
41 40
 
42 41
     /**
@@ -44,7 +43,7 @@ public class StringUtilities {
44 43
      * @return the last 3 characters of `input`
45 44
      */
46 45
     public String getSuffix(String input){
47
-        return null;
46
+        return input.substring(input.length() - 3);
48 47
     }
49 48
 
50 49
     /**
@@ -53,7 +52,9 @@ public class StringUtilities {
53 52
      * @return the equivalence of two strings, `inputValue` and `comparableValue`
54 53
      */
55 54
     public Boolean compareTwoStrings(String inputValue, String comparableValue){
56
-        return null;
55
+        
56
+        
57
+        return inputValue.equals(comparableValue);
57 58
     }
58 59
 
59 60
     /**
@@ -61,7 +62,8 @@ public class StringUtilities {
61 62
      * @return the middle character of `inputValue`
62 63
      */
63 64
     public Character getMiddleCharacter(String inputValue){
64
-        return null;
65
+        int mid = inputValue.length()/2;
66
+        return inputValue.charAt(mid);
65 67
     }
66 68
 
67 69
     /**
@@ -69,7 +71,10 @@ public class StringUtilities {
69 71
      * @return the first sequence of characters
70 72
      */
71 73
     public String getFirstWord(String spaceDelimitedString){
72
-        return null;
74
+        int x = spaceDelimitedString.indexOf(" ");
75
+        String firstWord = spaceDelimitedString.substring(0, x);
76
+        
77
+        return firstWord;
73 78
     }
74 79
 
75 80
     /**
@@ -77,7 +82,15 @@ public class StringUtilities {
77 82
      * @return the second word of a string delimited by spaces.
78 83
      */
79 84
     public String getSecondWord(String spaceDelimitedString){
80
-        return null;
85
+        int start = spaceDelimitedString.indexOf(" ") + 1;
86
+        int end = spaceDelimitedString.indexOf(" ", start);
87
+    
88
+    if (end == 1){
89
+        end = spaceDelimitedString.length( );
90
+    }
91
+    String secondWord = spaceDelimitedString.substring(start, end);
92
+    return secondWord;
93
+       
81 94
     }
82 95
 
83 96
     /**
@@ -85,7 +98,15 @@ public class StringUtilities {
85 98
      * @return an identical string with characters in reverse order.
86 99
      */
87 100
     public String reverse(String stringToReverse){
88
-        return null;
101
+        if (stringToReverse == null || stringToReverse.isEmpty()){
102
+            return stringToReverse;
103
+        }
104
+        String reverse = " ";
105
+        
106
+        for (int i = stringToReverse.length() - 1; i >=0; i--) {
107
+            reverse = reverse + stringToReverse.charAt(i);
108
+        }
109
+        return reverse;
89 110
     }
90 111
 
91 112
     /**
@@ -93,7 +114,8 @@ public class StringUtilities {
93 114
      * @return an identical string with spaces removed.
94 115
      */
95 116
     public String removeWhitespace(String input){
96
-        return null;
117
+        String newTrim = input.replaceAll("\\s","");
118
+        return newTrim;
97 119
     }
98 120
 
99 121
     /**
@@ -101,6 +123,7 @@ public class StringUtilities {
101 123
      * @return an identical string with spaces in the front and end removed.
102 124
      */
103 125
     public String trim(String input){
104
-        return null;
126
+        String trimmedString = input.trim();
127
+        return trimmedString;
105 128
     }
106 129
 }