JavaRush /Java Blog /Random EN /Manipulating Strings, Functions for Manipulating Strings ...
articles
Level 15

Manipulating Strings, Functions for Manipulating Strings in Java

Published in the Random EN group
In this tutorial, we will continue our study of strings in Java. The basics of working with strings can be found in the lesson "Strings in Java". A class Stringin Java has a set of methods for manipulating the contents of a string. Find characters, substrings, change case and other tasks. Manipulating strings, functions for working with strings in Java - 1

Getting characters and substrings

You can get a character at a specific position in a string by calling the charAt(). The index of the first character in the string is 0, the last is length()-1. The following code returns the 9 character of a string.
String anotherPalindrome = "Niagara. O roar again!";
char aChar = anotherPalindrome.charAt(9);
The numbering of characters starts from 0, so the 9th character in the string is "O". Manipulating strings, functions for working with strings in Java - 2If you need to get not just one character, but part of a string, you can use the substring. The method substringhas two options: Manipulating strings, functions for working with strings in Java - 3The following code will return a substring of the string starting at character 11 but not including character 15, resulting in the word "roar"
String anotherPalindrome = "Niagara. O roar again!";
String roar = anotherPalindrome.substring(11, 15);
Manipulating strings, functions for working with strings in Java - 4

Other Methods for Manipulating Strings

The table shows some methods for working with strings and their description. Manipulating strings, functions for working with strings in Java - 5

Finding characters and substrings

Let's look at a few more methods for searching for characters and substrings. The class Stringcontains methods that return the position of a character or substring in a string: indexOf()and lastIndexOf(). Methods indexOf()search from the beginning of the string, lastIndexOf()- from the end. If these methods do not find a match, they return -1. The class also Stringcontains a method containsthat returns true if the given sequence of characters is contained in the string. Use this method if you need to find out about the existence of a substring in a string, and its position is not important. The following table describes the indexOf()and methods lastIndexOf(). Manipulating strings, functions for working with strings in Java - 6CharSequenceis an interface that the class implements String, so you can pass strings to the contains().

Changing lines. Replacing characters and substrings

The class Stringhas several methods for inserting characters and substrings into a string. The table describes methods for replacing found characters and substrings. Manipulating strings, functions for working with strings in Java - 7

Example

The following class Filenameshows an example of using the lastIndexOf()and methods substring()to use different parts of a string with a file name. Manipulating strings, functions for working with strings in Java - 8Now consider a program that uses the class Filename: Manipulating strings, functions for working with strings in Java - 9The program will output: Manipulating strings, functions for working with strings in Java - 10The method extensionuses the method lastIndexOfto determine the last occurrence of " .". The method substringuses this value to find the file extension. Link to the source: Manipulating strings, functions for working with strings in Java Managing strings, functions for working with strings in Java. Part 2
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION