Skip to content

Commit

Permalink
Merge branch 'master' into PatternMatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Umit-Soylu authored Sep 15, 2020
2 parents 5d00708 + da54593 commit e64897b
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ Following issues are introduced step by step at each commit. Please use the link
## Common Java Classes
* [Enumeration](https://github.com/Umit-Soylu/Java-Training/tree/Enumeration) class is introduced with example cases. (New keywords: `enum`, and `ordinal`)
* [String](https://github.com/Umit-Soylu/Java-Training/tree/Strings) class is introduced with example cases. (New keywords: `String`, `StringBuilder` and `StringBuffer`)
* [Pattern and Matcher](https://github.com/Umit-Soylu/Java-Training/tree/PatternMatcher) class is introduced with example cases. (New keywords: `Pattern`, `Matcher`, `group`, `compile` and `regex`)
* [Pattern and Matcher](https://github.com/Umit-Soylu/Java-Training/tree/PatternMatcher) class is introduced with example cases. (New keywords: `Pattern`, `Matcher`, `group`, `compile` and `regex`)
62 changes: 62 additions & 0 deletions src/com/bilgeadam/java/tutorials/strings/Strings.java
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 test/com/bilgeadam/java/tutorials/strings/StringsTest.java
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());
}
}

0 comments on commit e64897b

Please sign in to comment.