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

String management, functions for working with strings in Java

Published in the Random EN group
In this tutorial we will continue learning about 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. String management, functions for working with strings in Java - 1

Getting characters and substrings

You can get the character that is at a specific position in a string by calling the charAt(). The index of the first character in the line is 0, the last one is length()-1. The following code returns the 9th character of the 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 line is “O”. String management, 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: String management, functions for working with strings in Java - 3The following code will return a substring of the string, starting from the 11th character, but not including the 15th, resulting in the word “roar”
String anotherPalindrome = "Niagara. O roar again!";
String roar = anotherPalindrome.substring(11, 15);
String management, functions for working with strings in Java - 4

Other methods for manipulating strings

The table shows some methods for working with strings and their descriptions. String management, functions for working with strings in Java - 5

Search for 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(). The 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 specified sequence of characters is contained in the string. Use this method if you need to know whether a substring exists in a string, but its position is not important. The following table describes the indexOf()and methods lastIndexOf(). String management, 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. String management, 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. String management, functions for working with strings in Java - 8Now consider a program that uses the class Filename: String management, functions for working with strings in Java - 9The program will output: String management, functions for working with strings in Java - 10The method extensionuses a method lastIndexOfto determine the last occurrence of " .". The method substringuses this value to find the file extension. Link to the original source: String management, functions for working with strings in Java String management, functions for working with strings in Java. Part 2
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION