boolean startsWith(String string, char c)
: Returns whether the given string starts with the given character.String toString(char[] chars)
: Creates a string from the given chars
.boolean contains(char[] haystack, char needle)
: Returns true
if the character needle
is in the character array haystack
. Returns false
otherwise.String censor(String stringToCensor, char[] forbiddenChars)
: Replaces each of the forbiddenChars
in stringToCensor
with a star (*
).String subString(String string, int from, int to)
: Takes the characters from index from
(inclusive) until index to
(exclusive) from the given string.String trim(String needsTrimming)
: Removes all space characters from the beginning and the end of the given string.main
method to test your methods:
System.out.println("'World' starts with 'W': " + startsWith("World", 'W')); System.out.println("'World' starts with 'o': " + startsWith("World", 'o')); char[] haystack = { 'H', 'e', 'l', 'l', 'o' }; char needle = 'l'; System.out.println(toString(haystack) + " contains '" + needle + "' : " + contains(haystack, needle)); char[] forbiddenChars = { 'a', 'e', 'i', 'o', 'u' }; String stringToCensor = "This is a test string with vowels"; String censoredString = censor(stringToCensor, forbiddenChars); System.out.println("Censored string: " + censoredString); String sub = subString("Hello World", 0, 5); System.out.println("Substring: '" + sub + "'"); String needsTrimming = " Hello World "; System.out.println("Trimmed string: '" + trim(needsTrimming) + "'");
This should produce the following output:
'World' starts with 'W': true 'World' starts with 'o': false H, e, l, l, o contains 'l' : true Censored string: Th*s *s * t*st str*ng w*th v*w*ls Substring: 'Hello' Trimmed string: 'Hello World'
You are not allowed to use any methods of String
from the Java standard library, except these:
charAt
length
char
s, you can check for equality with ==
and inequality with !=
.trim
, it might be helpful to add two additional methods: leftTrim
and rightTrim
. These methods remove the spaces from the left and right side of a string, respectively. You can then call these methods in trim
.