string methods¶
String Arrays¶
String[] myArray;
ArrayLists¶
java.util.ArrayList
ArrayList<String> flexibleList = new ArrayList<String>();
char to String¶
String.valueOf(char)
public static final String charToString(char c) {
return Character.toString(c);
}
String to char¶
charAt(int)
public static final char StringToChar(String str) {
return str.charAt(0);
}
String to char array¶
char[] chararray = str.toCharArray();
getChars¶
instance.getChars(int start, int end, array start, int destination)
mystr.getChars(0, 16, chararr, 0);
Streaming - char() method¶
Creates a Stream from a String object
Character[] arraychars = str.chars()
.mapToObj(c -> (char) c)
.toArray(Character[]::new);
String Array to String¶
Arrays.toString();
// convert an array to string
String str = Arrays.toString(strarray);
StringBuilder() and append()¶
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < strarray.length; ++i) {
stringBuilder.append(strarray[i] + " ");
}
String Joiner¶
import java.util.StringJoiner;
StringJoiner stringJoiner = new StringJoiner(";;;", "[", "]");
Stream and collector¶
import java.util.stream.*;
import java.util.Arrays;
return Stream.of(strarray).collect(Collectors.joinint());
String to String Array¶
split()¶
String[] strarray = str.split(",");
pattern.split()¶
import java.util.regex.Pattern;
String splitpattern = "\\s\\s";
Pattern pattern = Pattern.compile(splitpattern);
String Tokenizer Class¶
import java.util.StringTokenizer;
StringTokenizer tokenizer = new StringTokenizer(str);
String[] strarray = new String[tokenizer.countTokens()];
// Add tokens to an array
int i = 0;
while (tokenizer.hasMoreTokens()) {
strarray[i] = tokenizer.nextToken();
++i;
}
Java 8+ methods¶
char charAt(int index);
int compareTo(String anotherString);
String concat(String str);
int hashCode();
int indexOf
int length();
String replace
String replaceAll
String[] split(String regex);
boolean startsWith(String prefix);
String substring(int beginIndex);
String toLowerCase();
String toUpperCase();
String trim();
Java 11+ Methods¶
isBlank();
lines(); // returns a stream containing a collection of all * substrings split by lines(needs java.util.stream.Collectors)
strip(); // Removes all white space from strings, unicode-aware
repeat(int); // repeats a string(int) times
Java 13+ Methods¶
Text Block formatting¶
String block = """
Brain went on to say that
'Text blocks are much more readable
And much easier to debug or follow'
""";