Java Program to Demo Built-in String Functions

In java programming there are many built-in string manipulation functions.These functions belong to java.lang packages. We will demonstrate the use of each commonly used built-in string function here.More details about string class will be discussed in Java Programming Tutorial.

The program is written using NetBean 8.2 and you may also need to install the suitable Java SDK version for NetBean IDE. The operating system platform is Windows 7 64-bit system. The program is a very simple one and intended for beginners of Java programming.

Problem Definition

The program reads 3 string variables and then perform following operations on them
  1. Find the length of the string using length () function.
  2. Convert the string into uppercase and lowercase using toUpperCase () and toLowerCase () functions respectively.
  3.  Concatenate two strings using concat () function.

Flowchart

Flowchart: Java Program to Demo Built-in String Functions
Flowchart: Java Program to Demo Built-in String Functions

Program Code

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package JavaExamples;

/**
*
* @author Admin
*/
public class StringDemo {

public static void main(String[] args) {

int x, y;

String S = "Elephant";

String S1 = "Tiger";

String S3 = "Hello";

x = S.length();
y = S1.length();

System.out.println("Length of S=" + x);
System.out.println("Length of S1=" + y);

System.out.println(S.toUpperCase());
System.out.println(S.toLowerCase());

String S2 = S + S1;

System.out.println("S2=" + S2);

String S4 = S3.concat(S1);

System.out.println(S4);
}

}

Output

The output of the program is given below. It show the various forms of string variable – uppercase, lowercase, concatenated strings and length of strings.

 

Length of S=8

Length of S1=5

ELEPHANT

elephant

S2=ElephantTiger

HelloTiger

 

Advertisements