Common String Operations and their application in Java

Diliru Munasingha
2 min readMay 21, 2021

The string operations include concatenation, scanning, substring, translation, and verification. String operations can only be used on character, graphic, or UCS-2 fields. The following are detailed and related practical applications.

Concatenation

Concatenation, in the context of programming, is the operation of joining two strings together. Simply this describes combining a string, text, or other data in a series without any gaps. In programming languages, an operator is used to denote concatenation. For example, In the Java programming language, the operator “+” denotes concatenation, as it does in other programming languages.

Ex:

System.out.println(“Hello,” + “ how are you?”);

This code will output a single String as “Hello, how are you?” with the help of concatenation.

Substring

A substring is a contiguous sequence of characters within a string. Or simply, A part of string is called substring. In other words, substring is a subset of another string. In case of substring startIndex is inclusive and endIndex is exclusive.

In java You can get substring from the given string object by one of the two methods:

  • public String substring (int startIndex)

This method returns new String object containing the substring of the given string from specified startIndex (inclusive).

  • public String substring (int startIndex, int endIndex)

This method returns new String object containing the substring of the given string from specified startIndex to endIndex.

In case of string:

  • startIndex: inclusive
  • endIndex: exclusive

Ex:

String s=”hello”;

System.out.println(s.substring(0,2));

In the above substring, 0 points to h but 2 points to e (because end index is exclusive). Therefore, output of this above code will be “he”.

Ascii conversion

ASCII (American Standard Code for Information Interchange) is the common mode for text files in the computer and on the Internet. In ASCII file, each alphabetical, numeric or special character is represented with a 7-bit number (0s or 1s). Defined as 128 characters. Usually, ASCII is used for text files of UNIX and DOS-based OSs. Windows uses Unicode using modern codes. The IBM S / 390 systems use an 8-bit batch code named EBCDIC. Translation programs will change the file to different files from one code to another file.

Ex:

public class AsciiValue {

public static void main(String[] args) {

char ch = ‘a’;

int ascii = ch;

// You can also cast char to int

int castAscii = (int) ch;

System.out.println(“The ASCII value of “ + ch + “ is: “ + ascii);

System.out.println(“The ASCII value of “ + ch + “ is: “ + castAscii);

}

}

The output of the above program will be,

  • The ASCII value of a is: 97
  • The ASCII value of a is: 97

--

--