Last Updated:

Searching Java character strings

String operations have a certain important place in a person's work with Java code. You need to sort the strings, search for substrings, and do other formatting. However, with each of these operations it is necessary to get acquainted in depth and with additional material - with the help of a tutor it is difficult to do this without examples. So, it is worth reading this article, where all the main things will be analyzed, which will allow a beginner to work with lines of code in Java.

Find a substring in a string

To begin with, you should search for a substring in the string. This is one of the easy operations that can be done without preparation. There are 2 methods used here. The first is indexOf(), which is responsible for the first occurrence of the substring in the string, and the second is lastIndexOf(), responsible for the last. In the case where the substring cannot be found, both methods will return a value of "-1". And it looks like this:

String str = «Hello world»;
int index1 = str.indexOf(‘l’); 2
int index2 = str.indexOf(«wo»); 6
int index3 = str.lastIndexOf(‘l’); 9

Also, there are 2 methods that determine whether a string begins with a certain substring and the same, but only with the end. For such actions are responsible: startsWith() and endsWith(), respectively. And here's how this can be shown in practice:

String str = «myfile.exe»;
boolean start = str.startsWith(«my»); true
boolean end = str.endsWith(«exe»); true

This is basically all you need to know when looking for a substring. Nothing more complicated at this stage of learning operations with Java strings does not need to be learned.

Sort strings

You can also work with sorting strings in Java. Here, the user must use a method of the Arrays class. The following is an example of sorting in ascending order:

import java.util.Arrays;
public class SortArray {
public static void main(String[] args) {
String[] arrNames = {«Deepak»,»Susan»,»Aarthi»,»Chetan»};

System.out.println(«Arrays in original order: » + Arrays.toString(arrNames));


System.out.println(«Sorted array in ascending order: » + Arrays.toString(arrNames));

And here's what the program will output after that as a result:

Arrays in original order: [Deepak, Susan, Aarthi, Chetan]
Sorted array in ascending order: [Aarthi, Chetan, Deepak, Susan]

Format strings

Also, it is worth analyzing a few points regarding the advanced formatting of strings. In this case, a person will become aware, for example, of extracting characters from a string, as well as splitting and connecting lines of code. Each of these actions, just the same, will be discussed below.

Extract characters from a string

To retrieve the symbols, you must apply the new method. It is called charcharAt(intindex), it starts checking against a given index. After each calculated character, it returns it. If anything, indexing in arrays, as always, starts with 0 and goes only in ascending order. Here's an example of the code:

String str = «Java»;
char c = str.charAt(2);
System.out.println(c); v

When you want to retrieve an entire group of characters, you want to use a complex method. It is denoted as getChars(intsrcBegin, intsrcEnd, char[] dst, intdstBegin). Here's what you can use from the parameters to make this method work:

  • srcBegin – denotes the point from which the data is processed;
  • srcEnd – indicates the place where it is necessary to stop the process by finishing the return of symbols;
  • dst – an array that is used to return characters – all the received information will be displayed there;
  • dstBegin – the index of the array from which to add the characters extracted from the string.

Here's what it should look like in the console to use this method with the parameters labeled for it:

String str = «Hello world!»;
int start = 6;
int end = 11;
char[] dst=new char[end — start];
str.getChars(start, end, dst, 0);
System.out.println(dst); world

String joins

What is important to know in addition to the above is how the string is connected. As a rule, it is enough for a person to use the addition operation to perform this action:

String str1 = «Java»;
String str2 = «Hello»;
String str3 = str1 + » » + str2;

System.out.println(str3); Hello Java

If a non-string object is used, it is converted to a string. For example, as in this case:

String str3 = "Year" + 2015;

Also, there is an additional way to concatenate strings. It's called concat(). And its application is shown in the example below:

String str1 = «Java»;
String str2 = «Hello»;
str2 = str2.concat(str1); HelloJava

The last method is called join(). It allows you to combine using a string separator. That is, a space will be used between the words in the example:

String str1 = «Java»;
String str2 = «Hello»;
String str3 = String.join(» «, str2, str1); Hello Java

Replace in a string

A very simple operation concerns the replacement of information in a string. That is, you need to use only one method - replace (), to change the data in one part of the code to those specified in another. Here's what it might look like the easiest:

String str = «Hello world»;
String replStr1 = str.replace(‘l’, ‘d’); Heddowordd
String replStr2 = str.replace(«Hello», «Bye»); Bye world

Crop a row

You might want to trim a line when you need to make adjustments to the appearance of your code. However, sometimes it is used in the event that spaces or something else prevents you from correctly reading the commands of the program. In the first case, the trim() method is used and an example of its application in 2 lines of code looks like this:

String str = » hello world «;
str = str.trim(); hello world

It is worth noting that in this scenario, only the initial and final spaces are removed. Others, placed between the words, will remain untouched.

Also, there is a method that returns a substring from a certain index to the very end, or to the index specified by the user from the very beginning. This function is called "substring()". An example of using this method is as follows and will not cause difficulties in the work:

String str = «Hello world»;
String substr1 = str.substring(6); world
String substr2 = str.substring(3,5); lo

As you can see, all the features presented are not so complex. They usually consist of only a couple of lines of code. This means that even beginners can work with elements in Java and format strings in different ways without much difficulty.