JavaRush /Java Blog /Random EN /String manipulation, functions for working with strings i...
articles
Level 15

String manipulation, functions for working with strings in Java. Part 2

Published in the Random EN group
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". String manipulation, functions for working with strings in Java.  Part 2 - 1

String Comparison Methods

The class Stringcontains methods for comparing strings and parts of strings. The following table describes these methods: String manipulation, functions for working with strings in Java.  Part 2 - 2The following program uses a method regionMatchesto find strings in another string: String manipulation, functions for working with strings in Java.  Part 2 - 3The 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 objects StringBuilderdiffer from type objects Stringin 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. StringBuildershould 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, StringBuilderit will be more efficient to use.

length and capacity

The class, StringBuilderlike the class String, has a method length()that returns the length of the object's character sequence. Unlike strings, in StringBuilderaddition to the length, there is capacitythe 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. String manipulation, functions for working with strings in Java.  Part 2 - 4For example, the following code:
StringBuilder sb = new StringBuilder();
sb.append("Greetings");
will create string buildera length of 9 characters and a capacity of 16: String manipulation, functions for working with strings in Java.  Part 2 - 5The class StringBuildercontains several methods related to length and capacity that are not in the class String: String manipulation, functions for working with strings in Java.  Part 2 - 6Some 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 StringBuilderthat 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 appendalways adds characters to the end of the existing sequence, while the method insertadds characters at the specified location. Some class methods StringBuilder. String manipulation, functions for working with strings in Java.  Part 2 - 7You can use any class method Stringon a class object StringBuilderby first converting it to a string using the toString(). Then the string can be made again stringbuilderusing the constructor: StringBuilder(String str).

Example

The program StringDemowe looked at in previous Java lessons is an example of when using StringBuilderinstead Stringmakes the program easier. StringDemoreverses the palindrome: String manipulation, functions for working with strings in Java.  Part 2 - 8The 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: String manipulation, functions for working with strings in Java.  Part 2 - 9Conclusion:
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
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION