diff --git a/ReplaceString.java b/ReplaceString.java new file mode 100644 index 0000000..257c2fb --- /dev/null +++ b/ReplaceString.java @@ -0,0 +1,24 @@ +package com.apple.ReplaceString; +public class ReplaceString{ + + public static String removeChar(String s, char c) + { + if (s == null) + return null; + String newstring = ""; + for (int i = 0; i < s.length(); i++){ + if (s.charAt(i) != c) { + newstring += s.charAt(i); + } + } + return newstring; + } + + public static String removeCharMethod(String st, char ch) { + if (st == null) + return null; + return st.replaceAll(String.valueOf(ch),""); + } + +} +} \ No newline at end of file diff --git a/TestReplaceString.java b/TestReplaceString.java new file mode 100644 index 0000000..bd25646 --- /dev/null +++ b/TestReplaceString.java @@ -0,0 +1,33 @@ +package com.apple.ReplaceString; +import org.testng.annotations.Test; +import org.testng.Assert; +import org.testng.annotations.DataProvider; + +public class TestReplaceString { + +@Test(dataProvider="getData") +public void testremoveChar(String s, char c, String actual){ +Assert.assertEquals(ReplaceString.removeChar(s, c), actual); +} + +@Test(dataProvider="getData") +public void testremoveCharMethod(String st, char c, String actual){ +Assert.assertEquals(ReplaceString.removeCharMethod(st, ch), actual); +} + +@DataProvider() +public Object[][] getData(){ +Object data[][] = new Object[][]; +data[][]={ + { "jdadva", 'd', "java" }, + { "djdava", 'd', "java" }, + {null, 'd', null},//null String as input + { "", 'd', "" },//empty string as input + { "#6$a#", '#', "6$a" },//String with special characters and numericals + { "123456", '5', "12346" },//String with numericals + { "ja v a", ' ', "java" }//String with spaces as input + }; +return data; +} +} + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..035d061 --- /dev/null +++ b/pom.xml @@ -0,0 +1,57 @@ + + 4.0.0 + + com.apple + +ReplaceString + 0.0.1-SNAPSHOT + jar + + +com.apple + http://maven.apache.org + + + + UTF-8 + + + + + org.testng + testng + 6.9.9 + + + + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + ${suiteXmlFile} + + + + + + diff --git a/testng-StringProblem.xml b/testng-StringProblem.xml new file mode 100644 index 0000000..630aeb8 --- /dev/null +++ b/testng-StringProblem.xml @@ -0,0 +1,9 @@ + + + + + + + + +