-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into PatternMatcher
- Loading branch information
Showing
3 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.bilgeadam.java.tutorials.strings; | ||
|
||
public class Strings { | ||
private String name = "Java"; | ||
|
||
/** | ||
* The constructor will create 2 different Strings for the String Pool | ||
*/ | ||
public Strings(){ | ||
name = "New Java"; // 'New Java' is stored as a string in the background | ||
|
||
String test; | ||
test = "Java"; // 'Java' string, which is stored in the background for the variable 'name' in the first place, | ||
// is associated with 'test' variable. | ||
} | ||
|
||
/** | ||
* This appends a new string to current string | ||
* @return appended String | ||
*/ | ||
public String appendStrings(String value){ | ||
return name.concat(" " + value); | ||
} | ||
|
||
public String replaceChars(char oldChar, char newChar){ | ||
return name.replace(oldChar, newChar); | ||
} | ||
|
||
public String replaceFirstRegex(String oldChar, String newChar){ | ||
return name.replaceFirst(oldChar, newChar); | ||
} | ||
|
||
public String substring(int start){ | ||
name = "Java Class YJS 4436"; | ||
name = name.substring(start); | ||
|
||
System.out.println("name = " + name); | ||
return name; | ||
} | ||
|
||
/** | ||
* This splints the string for each ' ' | ||
* @return Only one string | ||
*/ | ||
public String splitString(){ | ||
name = "Java Class YJS 4436"; | ||
for (String s: name.split(" ")) { | ||
System.out.println("Split String = " + s); | ||
} | ||
|
||
return name.split(" ", 2)[0]; | ||
} | ||
|
||
/** | ||
* This uses StringBuilder class | ||
*/ | ||
public void buildingStrings(){ | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append(name); | ||
|
||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
test/com/bilgeadam/java/tutorials/strings/StringsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.bilgeadam.java.tutorials.strings; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class StringsTest { | ||
Strings testString; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
testString = new Strings(); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
testString = null; | ||
} | ||
|
||
@Test | ||
void testStrings() { | ||
// 'Java' string is already in JVM, so it will be assigned to 'oldString' parameter. | ||
String oldString = "Java", otherOldString = "Java"; | ||
assertSame(oldString, otherOldString); | ||
|
||
// This will guaranteed to create a new String in the memory | ||
String newString = "Java"; | ||
assertNotSame(oldString, newString); | ||
|
||
// This only compares memory locations of parameters | ||
assertEquals(false, oldString == newString); | ||
|
||
// This compares the values of Strings itself | ||
assertEquals(true, oldString.equals(newString)); | ||
} | ||
|
||
@Test | ||
void testAppendStrings() { | ||
String testValue = "YJS4436"; | ||
assertEquals("New Java " + testValue, testString.appendStrings(testValue)); | ||
} | ||
|
||
@Test | ||
void testReplaceChars() { | ||
char oldVal = 'a', newVal = 'A'; | ||
assertEquals("New JAvA", testString.replaceChars(oldVal, newVal)); | ||
} | ||
|
||
@Test | ||
void testReplaceFirstRegex() { | ||
String oldVal = "a", newVal = "A"; | ||
|
||
assertEquals("New JAva", testString.replaceFirstRegex(oldVal, newVal)); | ||
} | ||
|
||
@Test | ||
void testSubstring() { | ||
int startIndex = 11; | ||
assertEquals("YJS 4436", testString.substring(startIndex)); | ||
} | ||
|
||
@Test | ||
void testBuildingStrings() { | ||
} | ||
|
||
@Test | ||
void testSplitString() { | ||
assertEquals("Java", testString.splitString()); | ||
} | ||
} |