Java String Manipulation
When working with a string of characters, Java has a number of built in methods that allow for them to be easily manipulated.
When working with the console, a string can be displayed using the ‘println’ method.
This will display the message, ‘Hello World!’, in the console. If it is necessary to incorporate the value of a variable into this message it can be done using the ‘+’ concatenation operator.
String fname = "Bob"; System.out.println("Hello " + fname);
String fname = "Bob"; int age = 30; System.out.println(fname + " is " + age + " years old.");
Here, the message, ‘Bob is 30 years old’, is displayed in the console. Another way of doing this is with the use of formatted strings. These utilise either the the ‘printf’ or ‘format’ methods.
String fname = "Bob"; int age = 30; System.out.printf("%s is %d years old.", fname, age);
The ‘printf’ method uses what are called format specifiers as placeholders for the variables to be incorporated, in this case, ‘fname’ and ‘age’. The ‘%s’ format specifier denotes that a string variable is to be incorporated in its place, whilst ‘%d’ is the format specifier for integer values.
The ‘format’ method allows for a formatted string to be assigned to a variable so that it can be reused. It uses format specifiers in the same way as the ‘printf’ method.
String fname = "Bob"; int age = 30; String formattedString; formattedString = String.format("%s is %d years old.", fname, age); System.out.println(formattedString);
The table below shows some of the format specifiers that are available to use in formatted strings.
|%a||Argument is formatted as a floating-point hexadecimal number.|
|%A||Argument is formatted as a floating-point hexadecimal number and converted to upper case.|
|%b||Argument is formatted as a Boolean.|
|%B||Argument is formatted as a Boolean and converted to upper case.|
|%c||Argument is formatted as a character.|
|%C||Argument is formatted as a character and converted to upper case.|
|%d||Argument is formatted as a decimal integer.|
|%e||Argument is formatted in scientific notation.|
|%f||Argument is formatted as a floating-point number.|
|%n||Inserts a newline character.|
|%o||Argument is formatted as an octal integer.|
|%s||Argument is formatted as a string.|
|%S||Argument is formatted as a string and converted to upper case.|
|%t||Argument is formatted as a time.|
|%T||Argument is formatted as a date.|
|%x||Argument is formatted as an integer hexadecimal number.|
|%X||Argument is formatted as an integer hexadecimal number and converted to upper case.|
Every character in a string has an index value, which can be used to reference it. The first character has an index value of zero, the second has an index of one and so on.
String example = "This is a string."; System.out.println(example.charAt(3));
Here, the ‘charAt’ method is used to display the fourth character of a string, which has an index position of three.
It is also possible to find the index position of a character in a string using the ‘indexOf’ method.
Note that, the ‘indexOf’ method will return the index position of the first occurrence of a character, if more than one exists in the string. There is also a ‘lastIndexOf’ method that can be used to return the index position of the last occurrence of a particular character.
If the character does not exist in the string, then a ‘-1’ is returned.
Using the ‘substring’ method, it is possible to return a portion of a string by specifying the index position to start from, together with the index position to go up to but not include.
String example = "This is a string."; System.out.println(example.substring(5, 9));
The above example starts at index position five and goes up to, but does not include index position nine.
It should be noted that if only one argument is specified for the ‘substring’ method, then this is taken as the index position to start from and it is assumed that the remainder of the string is to be included.
Upper and Lower Case
In order to change a string to upper or lower case, there are two methods that can be used, ‘toUpperCase’ and ‘toLowerCase’.
String example = "This is a string."; System.out.println(example.toUpperCase()); System.out.println(example.toLowerCase());
The first will change the whole string to upper case and the latter will convert it to lower case.
THIS IS A STRING. this is a string.
If a string has extra spacing at the start, end, or both, it’s possible to remove this using the ‘trim’ method as demonstrated below.
String example = " This is a string. "; System.out.println(example.trim());
Sometimes it is necessary to find the size or length of a string. This can be done using the ‘length’ method.
String example = "This is a string."; System.out.println(example.length());
The ‘replace’ method can be used to replace a part of a string with another piece of text.
String example = "This is a short string."; System.out.println(example.replace("short", "small"));
Here, the word ‘short’ is replaced with the word ‘small’ to produce the string, ‘This is a small string.’. This method will replace all occurrences of the word, not just the first, if there is more than one.
These are just a small number of the string methods that are available.