0% found this document useful (0 votes)
7 views3 pages

Java Code To Illustrate Different Constructors and Methods in String Class

The document is a Java code example demonstrating various constructors and methods of the String class. It covers functionalities such as finding string length, character at a specific index, substring extraction, string concatenation, index searching, string equality checks, case conversion, trimming, and character replacement. Each method is illustrated with print statements to show the output of the operations performed on string variables.

Uploaded by

csmss789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Java Code To Illustrate Different Constructors and Methods in String Class

The document is a Java code example demonstrating various constructors and methods of the String class. It covers functionalities such as finding string length, character at a specific index, substring extraction, string concatenation, index searching, string equality checks, case conversion, trimming, and character replacement. Each method is illustrated with print statements to show the output of the operations performed on string variables.

Uploaded by

csmss789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// Java code to illustrate different constructors and methods in String class

import [Link].*;

import [Link].*;

class Test

public static void main (String[] args)

String s= "CSMSSS";

// or String s= new String ("Artificial");

// Returns the number of characters in the String.

[Link]("String length = " + [Link]());

// Returns the character at ith index.

[Link]("Character at 3rd position = "

+ [Link](3));

// Return the substring from the ith index character

// to end of string

[Link]("Substring " + [Link](3));

// Returns the substring from i to j-1 index.

[Link]("Substring = " + [Link](2,5));


// Concatenates string2 to the end of string1.

String s1 = "Csmss";

String s2 = "polytechnic";

[Link]("Concatenated string = " +

[Link](s2));

// Returns the index within the string

// of the first occurrence of the specified string.

String s4 = "Learn Share Learn";

[Link]("Index of Share " +

[Link]("Share"));

// Returns the index within the string of the

// first occurrence of the specified string,

// starting at the specified index.

[Link]("Index of a = " +

[Link]('a',3));

// Checking equality of Strings

Boolean out = "Csmss".equals("csmss");

[Link]("Checking Equality " + out);

out = "Csmss".equals("csmss");

[Link]("Checking Equality " + out);

out = "Csmss".equalsIgnoreCase(" csmss");


[Link]("Checking Equality " + out);

//If ASCII difference is zero then the two strings are similar

int out1 = [Link](s2);

[Link]("the difference between ASCII value is="+out1);

// Converting cases

String w1 = "CSMSS";

[Link]("Changing to lower Case " +

[Link]());

// Converting cases

String w2 = "csmss";

[Link]("Changing to UPPER Case " +

[Link]());

// Trimming the word

String w4 = " Learn Share Learn ";

[Link]("Trim the word " + [Link]());

// Replacing characters

String str1 = "csmsscop";

[Link]("Original String " + str1);

String str2 = "csmsscop".replace('m' ,'j') ;

[Link]("Replaced m with j -> " + str2);

You might also like