Strings And Its Operations In Java

Strings :

The string data type defines is used in Java to handle character strings. It is important to note that in Java, strings are Objects. Every time we write an output statement, the characters we enclose within the quotes are automatically turned into a String object,

CODE/PROGRAM/EXAMPLE
System.out.println(“Hello World”);

Strings are constructed just like other objects, e.g.

CODE/PROGRAM/EXAMPLE
String str = new String (“Hello”);
String str2 = new String (str); //str2 = “Hello”
String str = “I love Java.”;

Basic String example :

CODE/PROGRAM/EXAMPLE
// Introduce String.
class StringDemo {
public static void main(String args[]) {
// declare strings in various ways
String str1 = new String("Java strings are objects.");
String str2 = "They are constructed various ways.";
String str3 = new String(str2);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}

// O/P : Java strings are objects.
They are constructed various ways.
They are constructed various ways.

Using String Methods :

CODE/PROGRAM/EXAMPLE
// Some String operations.
class StrOps {
public static void main(String args[]) {
String str1 = "When it comes to Web programming, Java is #1.";
String str2 = new String(str1);
String str3 = "Java strings are powerful.";
int result, idx;
char ch;
System.out.println("Length of str1: " + str1.length());
// display str1, one char at a time.
for(int i=0; i < str1.length(); i++)
System.out.print(str1.charAt(i));
System.out.println();
if(str1.equals(str2))
System.out.println("str1 equals str2");
else
System.out.println("str1 does not equal str2");
if(str1.equals(str3))
System.out.println("str1 equals str3");
else
System.out.println("str1 does not equal str3");
result = str1.compareTo(str3);
if(result == 0)
System.out.println("str1 and str3 are equal");
else if(result < 0)
System.out.println("str1 is less than str3");
else
System.out.println("str1 is greater than str3");
// assign a new string to str2
str2 = "One Two Three One";
idx = str2.indexOf("One");
System.out.println("Index of first occurrence of One: " + idx);
idx = str2.lastIndexOf("One");
System.out.println("Index of last occurrence of One: " + idx);
}
}
//  O/P : Length of str1: 45
When it comes to Web programming, Java is #1.
str1 equals str2
str1 does not equal str3
str1 is greater than str3
Index of first occurrence of One: 0
Index of last occurrence of One: 14

String Arrays :

CODE/PROGRAM/EXAMPLE
// Demonstrate String arrays.
class StringArrays {
public static void main(String args[]) {
String strs[] = { "This", "is", "a", "test." };
System.out.println("Original array: ");
for(String s : strs)
System.out.print(s + " ");
System.out.println("\n");
// change a string
strs[1] = "was";
strs[3] = "test, too!";
System.out.println("Modified array: ");
for(String s : strs)
System.out.print(s + " ");
}
}
//  O/P : Original array:
This is a test.
Modified array:
This was a test, too!

Strings are said to be ‘immutable’, i.e. they cannot be changed once they are created. However we can use the method substring() to capture part of a string. The method is declared as follows:

Syntax
String substring(int startIndex, int endIndex)

Example :

CODE/PROGRAM/EXAMPLE
// Use substring().
class SubStr {
public static void main(String args[]) {
String orig = "Java makes the Web move.";
// construct a substring
String subst = orig.substring(5, 18);
System.out.println("Original String: " + orig);
System.out.println("Sub String: " + subst);
}
}
substring() example in java

O/P :
Original String: Java makes the Web move. Sub String: makes the Web

#Strings_in_java #String_Methods_in_java #String_Arrays_in_java

(New page will open, for Comment)

Not yet commented...