String manipulation, functions for working with strings in Java. Part 1 We continue the study of strings in Java. The basics of working with strings can be found in the lesson "Strings in Java".
Functions
The main functions
String Comparison Methods
The classString
contains methods for comparing strings and parts of strings. The following table describes these methods: The following program uses a method regionMatches
to find strings in another string: The program will output Eggs
. The program runs through the string character by character, for each character the program calls the method regionMatches
, passing it the position of the character, to search for the string findMe
.
StringBuilder class
Type objectsStringBuilder
differ from type objects String
in that they can be modified. In fact, they are arrays of variable length containing a sequence of characters. The length and content of the sequence can be changed by calling the appropriate methods. StringBuilder
should be used in cases where it will help to simplify the code (example below) or if it is necessary for better performance. If you need to concatenate a large number of strings, StringBuilder
it will be more efficient to use.
length and capacity
The class,StringBuilder
like the class String
, has a method length()
that returns the length of the object's character sequence. Unlike strings, in StringBuilder
addition to the length, there is capacity
the number of characters for which memory is allocated. The capacity returned by the method capacity()
is always greater than or equal to the length of the string (usually greater) and will be automatically increased if necessary. For example, the following code:
StringBuilder sb = new StringBuilder();
sb.append("Greetings");
will create string builder
a length of 9 characters and a capacity of 16: The class StringBuilder
contains several methods related to length and capacity that are not in the class String
: Some operations (for example, append()
, insert()
, or setLength()
) can increase the length of a sequence of characters in stringbuilder
'e , so that the length may be greater than the current capacity. When this happens, the capacity automatically increases.
FunctionsStringBuilder
The main functions StringBuilder
that are not in String
: append()
and insert()
, which are overloaded to accept different types of data. Each converts the argument to a string and appends it to the string stringbuilder
'a. The method append
always adds characters to the end of the existing sequence, while the method insert
adds characters at the specified location. Some class methods StringBuilder
. You can use any class method String
on a class object StringBuilder
by first converting it to a string using the toString()
. Then the string can be made again stringbuilder
using the constructor: StringBuilder(String str)
.
Example
The programStringDemo
we looked at in previous Java lessons is an example of when using StringBuilder
instead String
makes the program easier. StringDemo
reverses the palindrome: The program will output:
doT saw I was toD
In order to reverse the term, the program first converts the string into an array of characters (the first loop), then reverses the array and converts the array back into a string. If you convert the palindrome string to stringbuilder
, you can use reverse()
the StringBuilder
. The code will become cleaner and easier to read: Conclusion:
doT saw I was toD
Note that println()
we are passing to the function StringBuilder
:
System.out.println(sb);
The method sb.toString()
will be implicitly invoked like any other object passed to println()
. Link to the original source: Manipulating strings, functions for working with strings in Java. Part 2
GO TO FULL VERSION