Strings in java - Passionate Geekz

Breaking

Where you can unleash your inner geek.

Saturday, 25 January 2020

Strings in java

Java Strings

Strings are used for storing text.

String variable contains a collection of characters surrounded by double quotes:

Example

Create a variable of type String and assign it a value:

public class MyClass

{
public static void main(String[] args)

{
String abc = “Hello”;
System.out.println(abc);
}
}

1)String Length

A String in Java is actually an object, which contain methods that can perform certain operations on strings. For example, the length of a string can be found with the length() method:

Example

public class MyClass

{
public static void main(String[] args)

{
String txt = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
System.out.println(“The length of the txt string is: ” + txt.length());
}
}

2)More String Methods

There are many string methods available, for example toUpperCase() and toLowerCase():

public class MyClass

{
public static void main(String[] args)

{
String txt = “Hello World”;
System.out.println(txt.toUpperCase());
System.out.println(txt.toLowerCase());
}
}

3)Finding a String in a String

The indexOf() method returns the index (the position) of the first occurrence of a specified text in a string (including whitespace):

public class MyClass

{
public static void main(String[] args)

{
String txt = “Please locate where ‘locate’ occurs!”;
System.out.println(txt.indexOf(“locate”));
}
}

4)String Concatenation

The + operator can be used between strings to add them together to make a new string. This is called concatenation:

public class MyClass

{
public static void main(String[] args)

{
String firstName = “Ruchika”;
String lastName = “Katoch”;
System.out.println(firstName + ” ” + lastName);
}
}

You can also use the concat() method to concatenate two strings:

public class MyClass

{
public static void main(String[] args)

{
String firstName = “Ruchika”;
String lastName = “Katoch”;
System.out.println(firstName.concat(lastName));
}
}

5)Special Characters

Because strings must be written within quotes, Java will misunderstand this string, and generate an error:

String txt = “We are the so-called “Vikings” from the north.”;

The solution to avoid this problem, is to use the backslash escape character.

The backslash (\) escape character turns special characters into string characters:

Escape characterResultDescription
\’Single quote
\”Double quote
\\\Backslash

The sequence \"  inserts a double quote in a string:

public class MyClass {
public static void main(String[] args)

{
String txt = “We are the so-called \”Vikings\” from the north.”;
System.out.println(txt);
}
}

The sequence \'  inserts a single quote in a string:

public class MyClass

{
public static void main(String[] args)

{
String txt = “It\’s alright.”;
System.out.println(txt);
}
}

The sequence \\  inserts a backslash in a string:

public class MyClass

{
public static void main(String[] args)

{
String txt = “The character \\ is called backslash.”;
System.out.println(txt);
}
}

Six other escape sequences are valid in Java:

CodeResult
\nNew Line
\rCarriage Return
\tTab
\bBackspace
\fForm Feed

6)Adding Numbers and Strings

Java uses the + operator for both addition and concatenation.

Numbers are added. Strings are concatenated.

If you add two strings, the result will be a string concatenation:

Example

public class MyClass

{
public static void main(String[] args)

{
String x = “10”;
String y = “20”;
String z = x + y;
System.out.println(z);
}
}

No comments:

Post a Comment