We Offer 100% Job Guarantee Courses (Any Degree / Diploma Candidates / Year GAP / Non-IT / Any Passed Outs). Placement Records
Hire Talent (HR):+91-9707 240 250

General

Java String

Java String

Java String

String could be a category that belongs to java.lang package. String is largely associate in nursing object that represents sequence of character values.

char[] ch={‘S’,’E’,’L’,’E’,’N’,’I’,’U’,’M’};
String s=“SELENIUM";

Java String class provides a lot of methods to perform operations on string such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), substring() etc. 

How to create String object

By string literal

1. String s1=”India “;

String objects square measure keep in an exceedingly special memory space called the “string constant pool” whereas making exploitation literal as shown within the below diagram.

2. By new keyword

String s2=new String(“Delhi”);

In this case, JVM can produce a brand new string object in traditional (non-pool) heap memory as shown within the below diagram.

  • s1 == s2:0
  • s1 > s2  :positive value(D->68,K->75 ,68-75=-7 so s1.compareTo(s2) will result in -7)   s1 < s2 :negative value

Java String

Click Here-> Get Prepared for Java Interviews

Java String Example

The string literal could be a relation to a String object. String literal could be a reference, to will be manipulated like all different String reference. its accustomed invoke ways of String category.

For Example,

int myLenght = "Hello Java".length();

Java provides special support for the string concatenation operators (+),String concatenation is enforced through the StringBuffer category and technique.

For example,

String finalString = "Hello" + "Java";

String Constructors

Java string category provides numerous kinds of the constructors to form String objects. a number of them ar,

String()
Creates a replacement String object empty content"".
String(String s)

Java String Compare Example

Compares 2 strings the character case of the given String.

Java String object or Java Object

Example

public class JavaStringCompareExample{
public static void main(String args[]){
  String str = "Hello World";
  String anotherString = "hello world";
  Object objStr = str;
System.out.println( str.compareTo(anotherString) );
 System.out.println( str.compareToIgnoreCase(anotherString) );
 System.out.println( str.compareTo(objStr) );
 }
}

Output

-32
0
0

Java String Length Example

How to get the length of a given String objects.

Example:

public class StringLengthExample {
 public static void main(String[] args) {
 String str = "Hello World";
   int length = str.length();
    System.out.println("The String length is : " + length);
  }
}

Output:

Length of a String is : 11

Java String Replace Example

Java String Replace example describes however do replace technique of java String category can be accustomed replace character or sub string will be replaced by new one.

public class JavaStringReplaceExample{
public static void main(String args[]){     
    String str="Replace Region";
    System.out.println( str.replace( 'R','A' ) );
    System.out.println( str.replaceFirst("Re", "Ra") );
    new String object.
        System.out.println( str.replaceAll("Re", "Ra") );
    }
 }

Output:

Aeplace Aegion
Raplace Region

Click Here-> Get Java Training with Real-time Projects

Besant Technologies WhatsApp