Java 09: String

String is an data type that stores a series of characters. Inputs such as names or messages are stored as a string. A string is very simlar to array of characters.

String msg = "Welcome, to string in Java";
for example,
class StringExample{
    public static void main(String[] args){
        String msg = "Welcome, to string in Java";
        System.out.println(msg);
    }
}

Output

Welcome, to string in Java

The syntax for creating a string is by assigning a string litereal to the string variable

String strObject = stringLiteral;

the String is the data type and strObject is reference to the string object.

Another way to create an object is by using the new keyword.

String strObject = new String(object);

In Java, string is a class and has methods that we can use to work with the string. An object of a class is created using the new keyword.

class StringExample{
    public static void main(String[] args){
        String msg = new String("Welcome, to string in Java");
        System.out.println(msg);
    }
}

Concatenation

Two or more strings can be joined to form a new string. str1.concat(str2);
class StringExample{
    public static void main(String[] args){
        String str1 = "James ";
        String str2 = "Bond";
        String fullname = str1.concat(str2);
        System.out.println(fullname);
    }
}

Output

James Bond

The plus (+) operator also can be used to concatenate string with other objects. Remember that java will perform addition operation if both values are numbers. We just have to make sure one of the value is a string, java automatically change the number to string before do concatenation.

strObject + strObject2

or,

strObject + number

Example

class StringExample{
    public static void main(String[] args){
        String str1 = "James ";
        String str2 = "Bond";
        String fullname = str1 + str2;
       
        int id = 7;
        String agentName = fullname + id;
        System.out.println(agentName);
    }
}

Output

James Bond7

Length

To get information about the length of the string. It returns number of characters in the string.

class StringExample{
    public static void main(String[] args) {
        String txt = "Chicken burger";

        int len = txt.length();

        System.out.println("Text Length : " + len);
    }
}

Output

Text Length : 14

Change case

To change the case of the letter in a string. To change the case to lowercase, use the toLowerCase() method. And, to change the case of the string to uppercase, use the toUpperCase() method. Following is the syntax.

strObject.toLowerCase()

and, or

strObject.toUpperCase()

Example

class StringExample{
    public static void main(String[] args) {
        String txt = "I joIn tHe suPper clUb";
 
        System.out.println("In Lowercase : " + txt.toLowerCase());
        System.out.println("Original     : " + txt);
        System.out.println("In Uppercase : " + txt.toUpperCase());
    }
}

Output

In Lowercase : i join the supper club
Original     : I joIn tHe suPper clUb
In Uppercase : I JOIN THE SUPPER CLUB

Finding a character

To get the location of the key in the string. The indexOf() method returns the index value of the location if the key is found. If not found it will return -1. Syntax:

strObject.indexof(key)
class StringExample{
    public static void main(String[] args) {
        String txt = "I need to think";

        int index = txt.indexOf('n');

        System.out.println(index);

        System.out.println(txt.indexOf("need"));
        System.out.println(txt.indexOf("to"));
        System.out.println(txt.indexOf("g"));
}

Output

2
2
7
-1

A post by Cuber

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net