Wednesday, October 5, 2016

Muhammad Akbar

Java String (Part 1)-Tutorial

String in Java

String in Java is an Object, not a data type. It belongs to java.lang.String package. But, necessarily, we do not have to write import statement for that. Because, any java class default imports java.lang.* package.
Declaring a String and assigning a value to it
Like in most of the other languages, in Java, String is defines by enclosing a series of characters in a literal ("");
public class BasicjavaExample 
{
 public static void main(String[] args) {
  String a = "This is a basic Java Program.";
  System.out.println(a);
 }

}
Try It
Click "Try It" button above to see below output:
This is a basic Java Program.
There is another way to create a String: which is using new(). As String Is a class, we can create object for this class. In way both with create the same String.
String a = new String(“Welcome To Java”);

Some Important Escape characters in String

Escape Sequences
Description
Example
\n
New Line
System.out.println("\nWelcome");
\’
Single Quote
System.out.println("\'Welcome");
\”
Double Quote
System.out.println("\"Welcome”);
\\
Backslash
System.out.println("\\Welcome");
\t
Tab
System.out.println("\tWelcome");
\b
Backspace
System.out.println("\bWelcome");
\r
Carriage return
System.out.println("\rWelcome");

Some important and useful methods to remember

Concatenation (+ operator and concat() method)

Both + operator and concat() method can be used to concat two strings in an order. For String, + operator can be used only as concatenation operator, and not as addition operator (mentioned in Arithmetic operators).
Example:
public class Concat 
{
 public static void main(String[] args) {
  String a = "Welcome";
  String b = "To Java";
  String c = a+b;
  System.out.println(c);
  String d = a.concat(b);
  System.out.println(d);
 }
}
Try It
Click "Try IT" button so see the output. The output should be –
Welcome To Java
Welcome To Java
They produce exact same result!

length() method

length() method is used to find the length of the string used as an input. It includes spaces, special characters, everything that is present in the string and returns an integer which is the length of the string.
Example:
public class Length 
{
 public static void main(String[] args) 
 {
  String a = "Welcome to Java";
  int b = a.length();
  System.out.println("The length of the string is : "+b);
 }
}
Try It
Click "Try It" button to see the output. The output will be –
The length of the String is: 15

toString() method

toString() method one of the most used method in Java. This method is used to return the String value of a variable. If, it is applied on a String, it will return the String itself, but, if it is applied on any other variable, if will convert that variable in String and return them.
Point to note: toString() cannot be used on primitive data types. E.g. – int, float etc.
Example:
public class ToString 
{
 public static void main(String[] args) 
 {
  String a = "Welcome to Java";
  String output = a.toString();
  System.out.println(output);
  
  Integer b = 12;
  output = b.toString();
  System.out.println(output);
  
  Double c = 12.56;
  output = c.toString();
  System.out.println(output);
 }
}
Try It
Click "Try It" button to see the output. The Output will be:
Welcome to Java
12
12.56

subString() method

As the name suggests, substring() method is used to get a substring(a part of string) dynamically or programmatically from a String. There are 2 methods for getting a substring:

substring(arg0)

In this method we only input the starting point of the desired substring. The method returns the substring starting from that index of the main string till the end of the actual string.
Example:
public class SubString 
{
 public static void main(String[] args) 
 {
  String a = "Welcome to Java";
  System.out.println(a.substring(5));
 }
}
Try It
Click "try It" button to see the output. Output will be:
me To Java
Explanation
The substring returns the section of the actual string which is highlighted in green color.

substring(arg0, arg1) 

In this method we provide the starting index and the end index of the desired substring. The method returns the substring starting from that index of the main string till the (end index -1)th character provided by the user.
public class SubString 
{
 public static void main(String[] args) 
 {
  String a = "Welcome to Java";     
  System.out.println(a.substring(5,10));
 }
}
Try It
Click "try It" button to check the output. The output is:
me To
Explanation
The substring returns the section of the actual string which is highlighted in green color.

        

  Java String (Part 2)

Muhammad Akbar

About Muhammad Akbar -

Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

Subscribe to this Blog via Email :

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you