Replace
Zur Navigation springen
Zur Suche springen
Beschreibung
Die Funktion replace ersetzt alle entsprechenden Zeichen einer Zeichenkette durch ein angegebenes Zeichen. Die Funktion benötigt eine Zeichenkette, ein zu ersetzendes Zeichen und ein Zeichen, durch das alle zu ersetzende Zeichen in der Zeichenkette ausgetauscht werden.
Syntax
replace(String zeichenkette, Char zeichen, Char austauschZeichen)
Parameter
- zeichenkette - die Zeichenkette, in der Zeichen ausgetausch werden sollen
- zeichen - das Zeichen, das ausgetauscht werden soll
- austauschZeichen - das Zeichen, durch das die anderen ausgetauscht werden.
Return Value
- Gibt einen String zurück
Beispiele
Standardfälle
replace('Hello World! ',' ','$') --> returns 'Hello$$World!$' replace('Have a!nice day!','!',' ') --> returns 'Have a nice day '
Benutzung mit Variablen
repalce('[pos=1]','a','t') replace('Hallo','[var=a]','a')
JUnit Tests
[Anzeigen]
@BeforeClass
public static void beforeTest() {
fillInVariableMap.put(7, OMConverter.toObject("<OMOBJ><OMSTR>HaveQaQniceQday</OMSTR></OMOBJ>"));
fillInVariableMap.put(8, OMConverter.toObject("<OMOBJ><OMSTR>a</OMSTR></OMOBJ>"));
fillInVariableMap.put(9, OMConverter.toObject("<OMOBJ><OMSTR>b</OMSTR></OMOBJ>"));
exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMSTR>HaveQaQniceQday</OMSTR></OMOBJ>"));
exerciseVariableMap.put("h", OMConverter.toObject("<OMOBJ><OMSTR>a</OMSTR></OMOBJ>"));
exerciseVariableMap.put("i", OMConverter.toObject("<OMOBJ><OMSTR>b</OMSTR></OMOBJ>"));
}
@Test
public void testReplace1() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("Hello$$World!$")),
Evaluator.evaluate("replace('Hello World! ', ' ', '$')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testReplace2() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("Have a nice day ")),
Evaluator.evaluate("replace('Have a nice day!', '!', ' ')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testReplace3() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("1010101")),
Evaluator.evaluate("replace('a0a0a0a', 'a', '1')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testReplace4() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("`0`0`0`")),
Evaluator.evaluate("replace('a0a0a0a', 'a', '`')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testReplaceWithInput1() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("Have a nice day")),
Evaluator.evaluate("replace('[pos=7]', 'Q', ' ')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testReplaceWithInput2() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("H veQ QniceQd y")),
Evaluator.evaluate("replace('[pos=7]', '[pos=8]', ' ')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testReplaceWithInput3() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("HbveQbQniceQdby")),
Evaluator.evaluate("replace('[pos=7]', '[pos=8]', '[pos=9]')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testReplaceWithVariables1() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("Have a nice day")),
Evaluator.evaluate("replace('[var=g]', 'Q', ' ')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testReplaceWithVariables2() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("H veQ QniceQd y")),
Evaluator.evaluate("replace('[var=g]', '[var=h]', ' ')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testReplaceWithVariables3() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("HbveQbQniceQdby")),
Evaluator.evaluate("replace('[var=g]', '[var=h]', '[var=i]')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testReplaceWithCharacterMaxMinValue1() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("" + cMax + "0")),
Evaluator.evaluate("replace('a0', 'a', '" + cMax + "')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testReplaceWithCharacterMaxMinValue2() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("" + cMin + "0")),
Evaluator.evaluate("replace('a0', 'a', '" + cMin + "')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testReplaceWithWrongInputSecondArgumentEmpty() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("aaaaa")),
Evaluator.evaluate("replace('aa', '', 'a')", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testReplaceWithThirdArgumentEmpty() {
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("a")),
Evaluator.evaluate("replace('a ', ' ', '')", exerciseVariableMap, fillInVariableMap));
}
@Test(expected = ParserException.class)
public void testReplaceWithWrongInputCharacter() {
Evaluator.evaluate("replace(ab, ab, ab)", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testReplaceWithOneArgument() {
Evaluator.evaluate("replace('a')", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testReplaceWithTwoArguments() {
Evaluator.evaluate("replace('a', 'b')", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = UndefinedExerciseVariableException.class)
public void testReplaceWithMissingExerciseVariable() {
Evaluator.evaluate("replace('[var=j]', '[var=j]', '[var=j]')", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected = UndefinedFillInVariableException.class)
public void testReplaceWithMissingInput() {
Evaluator.evaluate("replace('[pos=42]', '[pos=42]', '[pos=42]')", exerciseVariableMap, fillInVariableMap);
fail();
}