5 Ways to Manipulate Strings in Java

Table of contents:

5 Ways to Manipulate Strings in Java
5 Ways to Manipulate Strings in Java
Anonim

Strings are sequences of characters. For example, "Hello!" it is a string, because it is made up of the characters "C", "i", "a", "o" and "!". In Java, strings are objects, which means that there is a String class, which will then have its own attributes and methods. We can use the various methods of the String class to manipulate strings.

Steps

Method 1 of 5: Create a String

3016567 1
3016567 1

Step 1. Create a string using the constructor of the String class

3016567 2
3016567 2

Step 2. Create a string by directly assigning it a value

3016567 3
3016567 3

Step 3. Here is an example program that creates a string in two different ways

Method 2 of 5: Find the Length of a String

3016567 4
3016567 4

Step 1. Let's try to understand what it means to find the length of a string

The length of a string is the number of characters it contains. For example, the length of the string "Hello!" is 6, since it contains 6 characters.

3016567 5
3016567 5

Step 2. Invoke the method

length ()

on an object of type String and saves the result in an integer variable.

3016567 6
3016567 6

Step 3. Here is an example program that measures the length of a newly created string

Method 3 of 5: Invert a String

Step 1. Let's try to understand what it means to invert a string

Inverting a string means reversing the order of the characters it contains. For example, the reverse string of: "Hello!" is: "! olleH". There are several ways to reverse a string in Java.

3016567 8
3016567 8

Step 2. Using the reverse () method of the StringBuffer class

Creates a StringBuffer object that takes the string to be inverted as an input parameter. Use StringBuffer's reverse () method and then get the new string via the toString () method.

3016567 9
3016567 9

Step 3. Iterating from the last to the first character of the string and copying them in append to a StringBuffer at each iteration

Create a new StringBuffer object by passing it as a parameter to initialize it the length of the string you want to reverse. At this point, use a for loop to iterate over the string, starting with the last character. At each iteration, add the character that is in the position described by the index as an append to the StringBuffer. Now, to get the inverted string, just use the toString () method.

3016567 10
3016567 10

Step 4. Writing a recursive function to reverse the string

In the recursive function, the base case is when the string is null, or if its length is less than or equal to one. In all other cases, the reverse () method returns a call to itself taking as a parameter the starting string minus the leading character, and the first character in append. So, if the string passed to the first call is "Hello!", The reverse () call in the return on the first recursion will take the string "ello!" As a parameter.

3016567 11
3016567 11

Step 5. Converting the string to a vector of characters and then swapping the first with the last, the second with the penultimate and so on

First, convert the string to a character vector by calling the toCharArray () method on the string. At that point, it obtains the index of the position of the last character contained in the vector, which will be equal to the length of the string minus one. Now iterates over the vector, swapping, at each iteration, the i-th character with the one in the position of the last character, minus i. Finally, convert the character vector back into a string.

3016567 12
3016567 12

Step 6. Here is the output you will get from any of the string inversion methods we just looked at

Method 4 of 5: Trim the Whitespace of a String

Step 1. Let's try to understand what it means to trim the whitespace of a string

Trimming a string in java means removing the whitespace at the beginning and end of the string. For example, if you have the string:"

Hello, world!

"and you want it to be:" Hello, world! "without whitespace at the beginning and end, you can trim the string. The String class exposes the trim () method, which returns a copy of the original string to less than leading and trailing whitespace, or the string itself, in case there are no superfluous spaces.

3016567 14
3016567 14

Step 2. Use the trim () method of the String class on an object of type String to trim the whitespace

Note that the trim () method will throw an exception in case the string it was invoked on is null. The trim () method will not change the original contents of the string it was invoked on, as strings in Java are immutable, which means that the state of a string cannot be changed after it is created. For this reason, the trim () method will return a new string which will be a copy of the original string, except for leading and trailing whitespace.

3016567 15
3016567 15

Step 3. Here is an example program that trims the whitespace of a string:

Method 5 of 5: Splitting a String

Step 1. Let's try to understand what it means to split a string

Splitting a string in Java means splitting the string into a vector of sub-strings, using a certain character as a delimiter. For example, if I split the string: "red, blue, green, yellow, pink" using the comma as delimiter, I would get the vector {"red", "blue", "green", "yellow", "pink"}. Here are three different ways to split a string.

3016567 17
3016567 17

Step 2. Using one

StringTokenizer

to tokenize the string.

Import the class

java.util. StringTokenizer

. At this point, create a new instance of

StringTokenizer

passing as parameters to the constructor the string to be divided into token and the character to be used as delimiter. If you don't pass a delimiter to the constructor, the tokenizer will use whitespace as the default delimiter. Once created the

StringTokenizer

you can use the method

nextToken ()

to have each token returned to you.

  • Before Java 1.4, the class

    StringTokenizer

    it was used to split strings in Java. Now, instead, using

    StringTokenizer

    it is not recommended, and it is recommended to use the method

    split ()

    of the class

    String

    or to use the package

    java.util.regex

3016567 18
3016567 18

Step 3. Using the method

split ()

of the class

String

.

The method

split ()

will take the delimiter as a parameter, and return a vector of sub-strings, which are nothing more than the tokens returned in the previous method by the

StringTokenizer

3016567 19
3016567 19

Step 4. Using a regular expression

Import the package

java.util.regex. Pattern

. Use the method

compile ()

of the class

Pattern

to set the delimiter, and then go to the method

split ()

the string you want to split. The

Pattern

will return you a vector of sub-strings.

Recommended: