java.lang.string constructors and methods with examples ... · java.lang.stirng constructors and...

30
java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides many constructors and methods to do string operations in Java. In this Java Tutorial, we shall see the usage of all the constructors and methods of java.lang.String with example programs. java.lang.String Constructors String() String(byte[] bytes) String(byte[] bytes, Charset charset) String(byte[] bytes, int offset, int length) String(byte[] bytes, int offset, int length, Charset charset) String(byte[] bytes, int offset, int length, String charsetName) String(byte[] bytes, String charsetName) String(char[] value) String(char[] value, int offset, int count) String(int[] codePoints, int offset, int count) String(String original) String(StringBuffer buffer) String(StringBuilder builder) java.lang.String Methods char charAt(int index) int codePointAt(int index) int codePointBefore(int index) int codePointCount(int beginIndex, int endIndex) int compareTo(String anotherString) int compareToIgnoreCase(String str) String concat(String str) boolean contains(CharSequence s) java.lang.String Constructors and Methods with Examples

Upload: others

Post on 04-Jul-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

java.lang.Stirng Constructors and Methods with Examples

String is a sequence of characters. java.lang.String class provides many constructors and methods to do stringoperations in Java. In this Java Tutorial, we shall see the usage of all the constructors and methods ofjava.lang.String with example programs.

java.lang.String Constructors

String()

String(byte[] bytes)

String(byte[] bytes, Charset charset)

String(byte[] bytes, int offset, int length)

String(byte[] bytes, int offset, int length, Charset charset)

String(byte[] bytes, int offset, int length, String charsetName)

String(byte[] bytes, String charsetName)

String(char[] value)

String(char[] value, int offset, int count)

String(int[] codePoints, int offset, int count)

String(String original)

String(StringBuffer buffer)

String(StringBuilder builder)

java.lang.String Methods

char charAt(int index)

int codePointAt(int index)

int codePointBefore(int index)

int codePointCount(int beginIndex, int endIndex)

int compareTo(String anotherString)

int compareToIgnoreCase(String str)

String concat(String str)

boolean contains(CharSequence s)

java.lang.String Constructors and Methods with Examples

Page 2: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

boolean contentEquals(CharSequence cs)

boolean contentEquals(StringBuffer sb)

static String copyValueOf(char[] data)

static String copyValueOf(char[] data, int offset, int count)

boolean endsW ith(String suffix)

boolean equals(Object anObject)

boolean equalsIgnoreCase(String anotherString)

static String format(Locale l, String format, Object… args)

static String format(String format, Object… args)

byte[] getBytes()

byte[] getBytes(Charset charset)

byte[] getBytes(String charsetName)

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

int hashCode()

int indexOf(int ch)

int indexOf(int ch, int fromIndex)

int indexOf(String str)

int indexOf(String str, int fromIndex)

String intern()

boolean isEmpty()

static String join(CharSequence delimiter, CharSequence… elements)

static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)

int lastIndexOf(int ch)

int lastIndexOf(int ch, int fromIndex)

int lastIndexOf(String str)

int lastIndexOf(String str, int fromIndex)

int length()

boolean matches(String regex)

int offsetByCodePoints(int index, int codePointOffset)

boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)

boolean regionMatches(int toffset, String other, int ooffset, int len)

Page 3: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

String replace(char oldChar, char newChar)

String replace(CharSequence target, CharSequence replacement)

String replaceAll(String regex, String replacement)

String replaceFirst(String regex, String replacement)

String[] split(String regex)

String[] split(String regex, int limit)

boolean startsW ith(String prefix)

boolean startsW ith(String prefix, int toffset)

CharSequence subSequence(int beginIndex, int endIndex)

String substring(int beginIndex)

String substring(int beginIndex, int endIndex)

char[] toCharArray()

String toLowerCase()

String toLowerCase(Locale locale)

String toString()

String toUpperCase()

String toUpperCase(Locale locale)

String trim()

static String valueOf(boolean b)

static String valueOf(char c)

static String valueOf(char[] data)

static String valueOf(char[] data, int offset, int count)

static String valueOf(double d)

static String valueOf(float f)

static String valueOf(int i)

static String valueOf(long l)

static String valueOf(Object obj)

java.util.String provides following functionalities through its methods :

Work with individual characters of a String

Compare two strings

Pattern Maching

Page 4: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

Search a character or sub-string in a String

Uppercase and Lowercase conversions

Sub-String replacements

Representation of primitive data types as Strings

java.lang.String Constructors :

String()

String() constructor creates a new String object which is initialized to an empty character sequence.

Example : An example program to demonstrate String() constructor.

StringClassExample.java to demonstrate String() constructor

public class StringClassExample {

public static void main(String[] args) { String str = new String(); System.out.print("Value of str is \""+str+"\""); }}

public class StringClassExample {

public static void main(String[] args) {

String str = new String();

System.out.print("Value of str is \""+str+"\"");

}

}

Output to console is :

Value of str is ""

Value of str is ""

String(byte[] bytes)

Java has a computing platform to run and develop java applications. There are many character sets available tochoose from based on the application you are interested in. The constructor String(byte[] bytes) creates a newstring and initializes it to a sequence of characters based on the interpretation of bytes with the defaultcharacter set of Java.

Example : An example program to demonstrate String(byte[] bytes) constructor.

StringClassExample.java

import java.nio.charset.Charset;

public class

Page 5: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

import java.nio.charset.Charset;

public class StringClassExample {

public static void main(String[] args) {

byte[] bytes = {97,98,99,101,102,105,108,110};

String str = new String(bytes);

System.out.println("Value of str is \""+str+"\"");

System.out.println("Default char set is "+Charset.defaultCharset());

}

}

Output to console is :

Value of str is "abcefiln"Default char set is UTF-8

Value of str is "abcefiln"

Default char set is UTF-8

String(byte[] bytes, Charset charset)

The constructor String(byte[] bytes, Charset charset) creates a new string and initializes the string with thebytes decoded using the charset speicified.

Example : An example program to demonstrate String(byte[] bytes, Charset charset) constructor.

StringClassExample.java

import java.nio.charset.Charset;

public class StringClassExample {

public static void main(String[] args) { byte[] bytes = {97,98,99,101,102,105,108,110}; String str = new String(bytes, Charset.forName("ISO-8859-1")); System.out.println("Value of str is \""+str+"\" using ISO-8859-1 charset"); String str1 = new String(bytes, Charset.forName("UTF-8")); System.out.println("Val

import java.nio.charset.Charset;

public class StringClassExample {

public static void main(String[] args) {

byte[] bytes = {97,98,99,101,102,105,108,110};

String str = new String(bytes, Charset.forName("ISO-8859-1"));

System.out.println("Value of str is \""+str+"\" using ISO-8859-1 charset");

String str1 = new String(bytes, Charset.forName("UTF-8"));

System.out.println("Value of str1 is \""+str1+"\" using UTF-8 charset");

}

}

Page 6: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

Output to console is :

Value of str is "abcefiln" using ISO-8859-1 charsetValue of str1 is "abcefiln" using UTF-8 charset

Value of str is "abcefiln" using ISO-8859-1 charset

Value of str1 is "abcefiln" using UTF-8 charset

String(byte[] bytes, int offset, int length)

The constructor String(byte[] bytes, int offset, int length) creates a new string and initialize the string withsequence of characters starting from a position in bytes array specified by “offset”, and considering the“length” number of bytes from the offset.

Example : An example program to demonstrate String(byte[] bytes, int offset, int length) constructor.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { byte[] bytes = {97,98,99,101,102,105,108,110,111,112,113,114}; //"abcefilnopqr" String str = new String(bytes, 3, 5); System.out.println("Value of str is \""+str+"\""); }}

public class StringClassExample {

public static void main(String[] args) {

byte[] bytes = {97,98,99,101,102,105,108,110,111,112,113,114}; //"abcefilnopqr"

String str = new String(bytes, 3, 5);

System.out.println("Value of str is \""+str+"\"");

}

}

Output to console is :

Value of str is "efiln"

Value of str is "efiln"

The bytes when decoded using UTF-8 Charset which is default to the Java Virtual Machine,

bytes = abcefilnopqr

and the offset is given by the integers in bold : 0-a-1-b-2-c-3-e-4-f-5-i-6-l-7-n-8-o-9-p-10-q-11-r-12

Offset = 3 and Lenght = 5 : 3-e-4-f-5-i-6-l-7-n-8 : five bytes starting from offset three is “efiln”

String(byte[] bytes, int offset, int length, Charset charset)

Page 7: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

The constructor String(byte[] bytes, int offset, int length, Charset charset) creates a new string and initialize thestring with sequence of characters starting from a position in bytes array specified by “offset”, and consideringthe “length” number of bytes from the offset. The bytes are decoded using the specified charset.

Example : An example program to demonstrate String(byte[] bytes, int offset, int length, Charset charset)constructor.

StringClassExample.java

import java.nio.charset.Charset;

public class StringClassExample {

public static void main(String[] args) { byte[] bytes = {97,98,99,101,102,105,108,110,111,112,113,114}; //"abcefilnopqr" String str = new String(bytes, 3, 5, Charset.forName("ISO-8859-1")); System.out.println("Value of str is \""+str+"\""); }}

import java.nio.charset.Charset;

public class StringClassExample {

public static void main(String[] args) {

byte[] bytes = {97,98,99,101,102,105,108,110,111,112,113,114}; //"abcefilnopqr"

String str = new String(bytes, 3, 5, Charset.forName("ISO-8859-1"));

System.out.println("Value of str is \""+str+"\"");

}

}

Output to console is :

Value of str is "efiln"

Value of str is "efiln"

The bytes when decoded using ISO-8859-1 Charset which is specified in the constructor,

bytes = abcefilnopqr

and the offset is given by the integers in bold : 0-a-1-b-2-c-3-e-4-f-5-i-6-l-7-n-8-o-9-p-10-q-11-r-12

Offset = 3 and Lenght = 5 : 3-e-4-f-5-i-6-l-7-n-8 : five bytes starting from offset three is “efiln”

String(byte[] bytes, int offset, int length, String charsetName)

The constructor String(byte[] bytes, int offset, int length, String charsetName) creates a new string andinitializes the string with sequence of characters starting from a position in bytes array specified by “offset”,and considering the “length” number of bytes from the offset. The bytes are decoded using the specifiedcharsetName.

Example : An example program to demonstrate String(byte[] bytes, int offset, int length, String charsetName)constructor.

Page 8: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

StringClassExample.java

import java.io.UnsupportedEncodingException;

public class StringClassExample {

public static void main(String[] args) { byte[] bytes = {97,98,99,101,102,105,108,110,111,112,113,114}; //"abcefilnopqr" try { String str = new String(bytes, 3, 5, "ISO-8859-1"); System.out.println("Value of str is \""+str+"\""); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }}

import java.io.UnsupportedEncodingException;

public class StringClassExample {

public static void main(String[] args) {

byte[] bytes = {97,98,99,101,102,105,108,110,111,112,113,114}; //"abcefilnopqr"

try {

String str = new String(bytes, 3, 5, "ISO-8859-1");

System.out.println("Value of str is \""+str+"\"");

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

}

}

Output to console is :

Value of str is "efiln"

Value of str is "efiln"

The bytes when decoded using ISO-8859-1 Charset which is specified in the constructor,

bytes = abcefilnopqr

and the offset is given by the integers in bold : 0-a-1-b-2-c-3-e-4-f-5-i-6-l-7-n-8-o-9-p-10-q-11-r-12

Offset = 3 and Lenght = 5 : 3-e-4-f-5-i-6-l-7-n-8 : five bytes starting from offset three is “efiln”

String(byte[] bytes, String charsetName)

The constructor String(byte[] bytes, String charsetName) creates a new string and initializes the string with thebytes decoded using the charsetName speicified.

Example : An example program to demonstrate String(byte[] bytes, String charsetName) constructor.

StringClassExample.java

import java.io.UnsupportedEncodingException;

public class StringClassExample {

public static void

Page 9: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

import java.io.UnsupportedEncodingException;

public class StringClassExample {

public static void main(String[] args) {

byte[] bytes = {97,98,99,101,102,105,108,110};

try {

String str = new String(bytes, "ISO-8859-1");

System.out.println("Value of str is \""+str+"\" using ISO-8859-1 charset");

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

}

}

Output to console is :

Value of str is "abcefiln" using ISO-8859-1 charset

Value of str is "abcefiln" using ISO-8859-1 charset

String(char[] chars)

The constructor String(char[] chars) creates a new string and initializes the string with the characters fromchars array.

Example : An example program to demonstrate String(char[] chars) constructor.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { char[] chars = {'t','u','t','o','r','i','a','l','k','a','r','t'}; String str = new String(chars); System.out.println("Value of str is \""+str+"\""); }}

public class StringClassExample {

public static void main(String[] args) {

char[] chars = {'t','u','t','o','r','i','a','l','k','a','r','t'};

String str = new String(chars);

System.out.println("Value of str is \""+str+"\"");

}

}

Output to console is :

Page 10: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

Value of str is "tutorialkart"

Value of str is "tutorialkart"

String(char[] chars, int offset, int count)

The constructor String(char[] chars, int offset, int count) creates a new string and initializes the string withsequence of characters starting from a position in chars array specified by “offset”, and considering the“length” number of characters from the offset.

Example : An example program to demonstrate String(char[] chars) constructor.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { char[] chars = {'t','u','t','o','r','i','a','l','k','a','r','t'}; String str = new String(chars); System.out.println("Value of str is \""+str+"\""); }}

public class StringClassExample {

public static void main(String[] args) {

char[] chars = {'t','u','t','o','r','i','a','l','k','a','r','t'};

String str = new String(chars);

System.out.println("Value of str is \""+str+"\"");

}

}

Output to console is :

Value of str is "tutorialkart"

Value of str is "tutorialkart"

chars = tutorialkart

and the offset is given by the integers in bold : 0-t-1-u-2-t-3-o-4-r-5-i-6-a-7-l-8-k-9-a-10-r-11-t-12

Offset = 3 and Length = 4 : 3-o-4-r-5-i-6-a-7 : four characters of chars array starting from offset three is “oria”

String(int[] codePoints, int offset, int count)

The constructor String(int[] codePoints, int offset, int count) creates a new string and initializes the string withsequence of Unicode code points starting from a position in codePoints integer array specified by “offset”, andconsidering the “length” number of Unicode code points from the offset.

Page 11: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

Example : An example program to demonstrate String(int[] codePoints, int offset, int count) constructor.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { int[] codePoints = {116,117,116,111,114,105,97,108,107,97,114,116}; String str = new String(codePoints, 3, 5); System.out.println("Value of str is \""+str+"\""); }}

public class StringClassExample {

public static void main(String[] args) {

int[] codePoints = {116,117,116,111,114,105,97,108,107,97,114,116};

String str = new String(codePoints, 3, 5);

System.out.println("Value of str is \""+str+"\"");

}

}

Output to console is :

Value of str is "orial"

Value of str is "orial"

Unicode code points of integer array codePoints = tutorialkart

and the offset is given by the integers in bold : 0-t-1-u-2-t-3-o-4-r-5-i-6-a-7-l-8-k-9-a-10-r-11-t-12

Offset = 3 and Length = 5 : 3-o-4-r-5-i-6-a-7-l-8 : five code points of codePoints integer array starting fromoffset three is “orial”

String(String original)

The constructor String(String original) creates a new string and initializes the string with the value of originalstring received in the arguments.

Example : An example program to demonstrate String(String original) constructor.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { String original = "tutorialkart"; String str = new String(original); System.out.println("Value of str is \""+str+"\""); }}

Page 12: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

public class StringClassExample {

public static void main(String[] args) {

String original = "tutorialkart";

String str = new String(original);

System.out.println("Value of str is \""+str+"\"");

}

}

Output to console is :

Value of str is "tutorialkart"

Value of str is "tutorialkart"

String(StringBuffer buffer)

The constructor String(StringBuffer buffer) creates a new string and initializes the string with the sequence ofcharacters present in buffer.

Example : An example program to demonstrate String(StringBuffer buffer) constructor.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { StringBuffer buffer = new StringBuffer("tutorialkart"); String str = new String(buffer); System.out.println("Value of str is \""+str+"\""); }}

public class StringClassExample {

public static void main(String[] args) {

StringBuffer buffer = new StringBuffer("tutorialkart");

String str = new String(buffer);

System.out.println("Value of str is \""+str+"\"");

}

}

Output to console is :

Value of str is "tutorialkart"

Value of str is "tutorialkart"

Page 13: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

String(StringBuilder buffer)

The constructor String(StringBuilder buffer) creates a new string and initializes the string with the sequence ofcharacters present in buffer.

Example : An example program to demonstrate String(StringBuilder buffer) constructor.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { StringBuilder builder = new StringBuilder("tutorialkart"); String str = new String(builder); System.out.println("Value of str is \""+str+"\""); }}

public class StringClassExample {

public static void main(String[] args) {

StringBuilder builder = new StringBuilder("tutorialkart");

String str = new String(builder);

System.out.println("Value of str is \""+str+"\"");

}

}

Output to console is :

Value of str is "tutorialkart"

Value of str is "tutorialkart"

java.lang.String Methods :

char charAt(int index)

The method java.lang.String.charAt(int index) returns the character value at the index specified in thearguments.Example : An example program to demonstrate charAt(int index) method.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { String str = "tutorialkart"; char ch1 = str.charAt(5); System.out.println("Character at index 5 is "+ch1); char ch2 = str.charAt(8);

Page 14: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

public class StringClassExample {

public static void main(String[] args) {

String str = "tutorialkart";

char ch1 = str.charAt(5);

System.out.println("Character at index 5 is "+ch1);

char ch2 = str.charAt(8);

System.out.println("Character at index 8 is "+ch2);

}

}

Output to console is :

Character at index 5 is iCharacter at index 8 is k

Character at index 5 is i

Character at index 8 is k

int codePointAt(int index)

The method java.lang.String.codePointAt(int index) returns the Unicode code point at the index specified in thearguements.Example : An example program to demonstrate codePointAt(int index) method.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { String str = "tutorialkart"; int codePoint1 = str.codePointAt(5); System.out.println("codePoint at index 5 is "+codePoint1); int codePoint2 = str.codePointAt(8); System.out.println("codePoint at index 8 is "+codePoint2); }}

public class StringClassExample {

public static void main(String[] args) {

String str = "tutorialkart";

int codePoint1 = str.codePointAt(5);

System.out.println("codePoint at index 5 is "+codePoint1);

int codePoint2 = str.codePointAt(8);

System.out.println("codePoint at index 8 is "+codePoint2);

}

}

Output to console is :

codePoint at index 5 is 105codePoint at index 8 is 107

Page 15: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

codePoint at index 5 is 105

codePoint at index 8 is 107

int codePointBefore(int index)

The method java.lang.String.codePointBefore(int index) returns the Unicode code point character just beforethe index specified in the arguements.Example : An example program to demonstrate codePointBefore(intindex) method.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { String str = "tutorialkart"; int codePoint1 = str.codePointAt(5); System.out.println("codePoint at index 5 is "+codePoint1); int codePoint2 = str.codePointBefore(6); System.out.println("codePoint at index before 6 is "+codePoint2); }}

public class StringClassExample {

public static void main(String[] args) {

String str = "tutorialkart";

int codePoint1 = str.codePointAt(5);

System.out.println("codePoint at index 5 is "+codePoint1);

int codePoint2 = str.codePointBefore(6);

System.out.println("codePoint at index before 6 is "+codePoint2);

}

}

Output to console is :

codePoint at index 5 is 105codePoint at index before 6 is 105

codePoint at index 5 is 105

codePoint at index before 6 is 105

int codePointCount(int beginIndex, int endIndex)

The method java.lang.String.codePointCount(int beginIndex, int endIndex) returns the number of Unicode codepoints in the specified text range of this String.Example : An example program to demonstratecodePointCount(int beginIndex, int endIndex) method.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { String str = "tutorialkart";

Page 16: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

public class StringClassExample {

public static void main(String[] args) {

String str = "tutorialkart";

int count = str.codePointCount(2,5);

System.out.println("number of code points from 2 to 5 is "+count);

}

}

Output to console is :

number of code points from 2 to 5 is 3

number of code points from 2 to 5 is 3

int compareTo(String anotherString)

The method java.lang.String.compareTo(String anotherString) compares two strings lexicographically (the orderin which words are arranged in a dictionary). Returns a positive value if the string should occur later to thestring provided in the arguements. Returns a negative value if the string should occur prior to the stringprovided in the arguements.Example : An example program to demonstrate compareTo(String anotherString)method.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { compare("tutorialkart","tutorialkart"); compare("tutorialkart","autorialkart"); compare("autorialkart","tutorialkart"); } public static void compare(String str, String anotherStr){ int difference = str.compareTo(anotherStr); if(difference &lt; 0){ System.out.println("The string, \""+str+"\" should occur prior to the string, \""+anotherStr+"\" provided in the arguements."); } else if(difference &gt;0){

Page 17: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

public class StringClassExample {

public static void main(String[] args) {

compare("tutorialkart","tutorialkart");

compare("tutorialkart","autorialkart");

compare("autorialkart","tutorialkart");

}

public static void compare(String str, String anotherStr){

int difference = str.compareTo(anotherStr);

if(difference &lt; 0){ System.out.println("The string, \""+str+"\" should occur prior to the string, \""+anotherStr+"\"

provided in the arguements."); } else if(difference &gt;0){

System.out.println("The string, \""+str+"\" should occur later to the string, \""+anotherStr+"\" provided in the

arguements.");

} else {

System.out.println("The two strings, \""+str+"\" and \""+anotherStr+"\" are equal.");

}

}

}

Output to console is :

The two strings, "tutorialkart" and "tutorialkart" are equal.The string, "tutorialkart" should occur later to the string, "autorialkart" provided in the arguements.The string, "autorialkart" should occur prior to the string, "tutorialkart" provided in the arguements.

The two strings, "tutorialkart" and "tutorialkart" are equal.

The string, "tutorialkart" should occur later to the string, "autorialkart" provided in the arguements.

The string, "autorialkart" should occur prior to the string, "tutorialkart" provided in the arguements.

int compareToIgnoreCase(String anotherString)

The method java.lang.String.compareToIgnoreCase(String anotherString) compares two stringslexicographically (the order in which words are arranged in a dictionary) without considering if characters’case. Returns a negative value if the string should occur later to the string provided in the arguements Returnsa negative value if the string should occur prior to the string provided in the arguementsExample : An exampleprogram to demonstrate compareToIgnoreCase(String anotherString) method.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { compare("Tutorialkart","tutorialkart"); compare("tutorialkart","AutoRialkart");

Page 18: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

public class StringClassExample {

public static void main(String[] args) {

compare("Tutorialkart","tutorialkart");

compare("tutorialkart","AutoRialkart");

compare("AutorialkaRt","tutoriAlkart");

}

public static void compare(String str, String anotherStr){

int difference = str.compareToIgnoreCase(anotherStr);

if(difference &lt; 0){ System.out.println("The string, \""+str+"\" should occur prior to the string, \""+anotherStr+"\"

provided in the arguements."); } else if(difference &gt;0){

System.out.println("The string, \""+str+"\" should occur later to the string, \""+anotherStr+"\" provided in the

arguements.");

} else {

System.out.println("The two strings, \""+str+"\" and \""+anotherStr+"\" are equal.");

}

}

}

Output to console is :

The two strings, "Tutorialkart" and "tutorialkart" are equal.The string, "tutorialkart" should occur later to the string, "AutoRialkart" provided in the arguements.The string, "AutorialkaRt" should occur prior to the string, "tutoriAlkart" provided in the arguements.

The two strings, "Tutorialkart" and "tutorialkart" are equal.

The string, "tutorialkart" should occur later to the string, "AutoRialkart" provided in the arguements.

The string, "AutorialkaRt" should occur prior to the string, "tutoriAlkart" provided in the arguements.

String concat(String str)

The method java.lang.String.concat(String str) concatenates the string specified in the arguements to the endof this string.Example : An example program to demonstrate concat(String str) method.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { String thisStr = "tutorialkart"; String str = ".com is the best website to understand java programming language."; String resultStr = thisStr.concat(str); System.out.println(resultStr);

Page 19: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

public class StringClassExample {

public static void main(String[] args) {

String thisStr = "tutorialkart";

String str = ".com is the best website to understand java programming language.";

String resultStr = thisStr.concat(str);

System.out.println(resultStr);

}

}

Output to console is :

tutorialkart.com is the best website to understand java programming language.

tutorialkart.com is the best website to understand java programming language.

boolean contains(CharSequence s)

The method java.lang.String.contains(CharSequence s) returns true if and only if this string contains thesequence of char values specified in the arguements.Example : An example program to demonstratecontains(CharSequence s) method.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { CharSequence chSeq = "kart"; String str = "tutorialkart"; boolean isPresent = str.contains(chSeq); if(isPresent){ System.out.println("CharSequence \""+chSeq+"\" is present in the str, \""+str+"\""); } else { System.out.println("CharSequence \""+chSeq+"\" is not present in the str, \""+str+"\""); } }}

public class StringClassExample {

public static void main(String[] args) {

CharSequence chSeq = "kart";

String str = "tutorialkart";

boolean isPresent = str.contains(chSeq);

if(isPresent){

System.out.println("CharSequence \""+chSeq+"\" is present in the str, \""+str+"\"");

} else {

System.out.println("CharSequence \""+chSeq+"\" is not present in the str, \""+str+"\"");

}

}

}

Output to console is :

Page 20: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

CharSequence "kart" is present in the str, "tutorialkart"

CharSequence "kart" is present in the str, "tutorialkart"

boolean contentEquals(CharSequence cs)

The method java.lang.String.contentEquals(CharSequence cs) compares this string to the specifiedCharSequence and returns true if both the String and CharSequence are equal, else it returns false.Example :An example program to demonstrate contentEquals(CharSequence cs) method.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { CharSequence chSeq = "tutorialkart"; String str = "tutorialkart"; boolean isPresent = str.contentEquals(chSeq); if(isPresent){ System.out.println("CharSequence \""+chSeq+"\" equals the string str, \""+str+"\""); } else { System.out.println("CharSequence \""+chSeq+"\" is not equal to the string str, \""+str+"\""); } }}

public class StringClassExample {

public static void main(String[] args) {

CharSequence chSeq = "tutorialkart";

String str = "tutorialkart";

boolean isPresent = str.contentEquals(chSeq);

if(isPresent){

System.out.println("CharSequence \""+chSeq+"\" equals the string str, \""+str+"\"");

} else {

System.out.println("CharSequence \""+chSeq+"\" is not equal to the string str, \""+str+"\"");

}

}

}

Output to console is :

CharSequence "tutorialkart" equals the string str, "tutorialkart"

CharSequence "tutorialkart" equals the string str, "tutorialkart"

boolean contentEquals(StringBuffer sb)

The method java.lang.String.contentEquals(StringBuffer sb) compares this string to the specified StringBuffer,and returns true if both the String and StringBuffer are equal, else it returns false.Example : An exampleprogram to demonstrate contentEquals(StringBuffer sb) method.

StringClassExample.java

Page 21: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

public class StringClassExample {

public static void main(String[] args) { StringBuffer buffer = new StringBuffer("tutorialkart"); String str = "tutorialkart"; boolean isPresent = str.contentEquals(buffer); if(isPresent){ System.out.println("CharSequence \""+buffer+"\" equals the string str, \""+str+"\""); } else { System.out.println("CharSequence \""+buffer+"\" is not equal to the string str, \""+str+"\""); } }}

public class StringClassExample {

public static void main(String[] args) {

StringBuffer buffer = new StringBuffer("tutorialkart");

String str = "tutorialkart";

boolean isPresent = str.contentEquals(buffer);

if(isPresent){

System.out.println("CharSequence \""+buffer+"\" equals the string str, \""+str+"\"");

} else {

System.out.println("CharSequence \""+buffer+"\" is not equal to the string str, \""+str+"\"");

}

}

}

Output to console is :

CharSequence "tutorialkart" equals the string str, "tutorialkart"

CharSequence "tutorialkart" equals the string str, "tutorialkart"

static String copyValueOf(char[] data)

This method java.lang.String.copyValueOf(char[] data) creates a new String object with the sequence ofcharacters speicified by char[] data.Example : An example program to demonstrate copyValueOf(char[]data) method.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { char[] chArr = {'t','u','t','o','r','i','a','l','k','a','r','t'}; String str = String.copyValueOf(chArr); System.out.println("Value of str is \""+str+"\""); }}

public class StringClassExample {

public static void main(String[] args) {

char[] chArr = {'t','u','t','o','r','i','a','l','k','a','r','t'};

String str = String.copyValueOf(chArr);

System.out.println("Value of str is \""+str+"\"");

}

}

Output to console is :

Page 22: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

Value of str is "tutorialkart"

Value of str is "tutorialkart"

static String copyValueOf(char[] data, int offset, int count)

This method java.lang.String.copyValueOf(char[] data, int offset, int count) creates a new string object with thesequence of “count” number of characters of char[] data starting from offset position.Example : An exampleprogram to demonstrate copyValueOf(char[] data, int offset, int count) method.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { char[] chArr = {'t','u','t','o','r','i','a','l','k','a','r','t'}; String str = String.copyValueOf(chArr,6,5); System.out.println("Value of str is \""+str+"\""); }}

public class StringClassExample {

public static void main(String[] args) {

char[] chArr = {'t','u','t','o','r','i','a','l','k','a','r','t'};

String str = String.copyValueOf(chArr,6,5);

System.out.println("Value of str is \""+str+"\"");

}

}

Output to console is :

Value of str is "alkar"

Value of str is "alkar"

boolean endsWith(String suffix)

The method java.lang.String.endsWith(String suffix) tests if this string ends with the specified suffix.Example :An example program to demonstrate endsWith(String suffix) method.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { String str = "tutorialkart"; String suffix = "kart"; boolean isEnding = str.endsWith(suffix); if(isEnding){

Page 23: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

public class StringClassExample {

public static void main(String[] args) {

String str = "tutorialkart";

String suffix = "kart";

boolean isEnding = str.endsWith(suffix);

if(isEnding){

System.out.println("Suffix \""+suffix+"\" is present at the end of string str, \""+str+"\"");

} else {

System.out.println("Suffix \""+suffix+"\" is not present at the end of string str, \""+str+"\"");

}

}

}

Output to console is :

Suffix "kart" is present at the end of string str, "tutorialkart"

Suffix "kart" is present at the end of string str, "tutorialkart"

boolean equals(Object anObject)

The method java.lang.String.equals(Object anObject) compares this string to the specified object.Example : Anexample program to demonstrate equals(Object anObject) method.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { String str = "tutorialkart"; Object otherStr = "kart"; boolean isEqual = str.equals(otherStr); if(isEqual){ System.out.println("Two string, \""+otherStr+"\" and \""+str+"\" are equal."); } else { System.out.println("Two string, \""+otherStr+"\" and \""+str+"\" are not equal."); } }}

public class StringClassExample {

public static void main(String[] args) {

String str = "tutorialkart";

Object otherStr = "kart";

boolean isEqual = str.equals(otherStr);

if(isEqual){

System.out.println("Two string, \""+otherStr+"\" and \""+str+"\" are equal.");

} else {

System.out.println("Two string, \""+otherStr+"\" and \""+str+"\" are not equal.");

}

}

}

Page 24: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

Output to console is :

Two string, "kart" and "tutorialkart" are not equal.

Two string, "kart" and "tutorialkart" are not equal.

boolean equalsIgnoreCase(String anotherString)

The method java.lang.String.equalsIgnoreCase(String anotherString) compares this String to another String,ignoring if the characters in the Strings are uppercase or lowercase.Example : An example program todemonstrate equalsIgnoreCase(String anotherString) method.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { String str = "tutorialkart"; String otherStr = "TUTORIALKART"; boolean isEqual = str.equalsIgnoreCase(otherStr); if(isEqual){ System.out.println("Two strings, \""+otherStr+"\" and \""+str+"\" are equal."); } else { System.out.println("Two strings, \""+otherStr+"\" and \""+str+"\" are not equal."); } }}

public class StringClassExample {

public static void main(String[] args) {

String str = "tutorialkart";

String otherStr = "TUTORIALKART";

boolean isEqual = str.equalsIgnoreCase(otherStr);

if(isEqual){

System.out.println("Two strings, \""+otherStr+"\" and \""+str+"\" are equal.");

} else {

System.out.println("Two strings, \""+otherStr+"\" and \""+str+"\" are not equal.");

}

}

}

Output to console is :

Two string, "TUTORIALKART" and "tutorialkart" are equal.

Two string, "TUTORIALKART" and "tutorialkart" are equal.

static String format(Locale l, String format, Object… args)

The method java.lang.String.format(Locale l, String format, Object… args) returns a formatted string using thespecified locale, format string, and arguments.Example : An example program to demonstrate format(Locale l,String format, Object… args) method.

Page 25: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

StringClassExample.java

import java.util.Locale;

public class StringClassExample {

public static void main(String[] args) { String str = String.format(Locale.ENGLISH,"%s %d days %s", "There are",365,"in an year."); System.out.println(str); }}

import java.util.Locale;

public class StringClassExample {

public static void main(String[] args) {

String str = String.format(Locale.ENGLISH,"%s %d days %s", "There are",365,"in an year.");

System.out.println(str);

}

}

Output to console is :

There are 365 days in an year.

There are 365 days in an year.

In the above program : “Locale” is Locale.ENGLISH “format” is : “%s %d days %s” And there are three args(%s,%d,%s in format string) respectively are : “There are”, 365, “in an year.”

static String format(String format, Object… args)

The method java.lang.String.format(String format, Object… args) returns a formatted string using the specifiedformat string and arguments.Example : An example program to demonstrate format(String format, Object…args) method.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { String str = String.format("%s %d days %s", "There are",365,"in an year."); System.out.println(str); }}

public class StringClassExample {

public static void main(String[] args) {

String str = String.format("%s %d days %s", "There are",365,"in an year.");

System.out.println(str);

}

}

Output to console is :

Page 26: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

There are 365 days in an year.

There are 365 days in an year.

In the above program “format” is : “%s %d days %s” And there are three args : “There are”, 365, “in an year.”

byte[] getBytes()

The method java.lang.String.getBytes() encodes this String into a sequence of bytes using the platform’sdefault charset, storing the result into a new byte array.Example : An example program to demonstrategetBytes() method.

StringClassExample.java

public class StringClassExample {

public static void main(String[] args) { String str = "tutorialkart"; byte[] bytes = str.getBytes(); System.out.print("The bytes are : "); for(byte b:bytes) System.out.print(b+","); }}

public class StringClassExample {

public static void main(String[] args) {

String str = "tutorialkart";

byte[] bytes = str.getBytes();

System.out.print("The bytes are : ");

for(byte b:bytes) System.out.print(b+",");

}

}

Output to console is :

The bytes are : 116,117,116,111,114,105,97,108,107,97,114,116,

The bytes are : 116,117,116,111,114,105,97,108,107,97,114,116,

byte[] getBytes(Charset charset)

The method java.lang.String.getBytes(Charset charset) encodes this String into a sequence of bytes using thegiven charset, storing the result into a new byte array.Example : An example program to demonstrategetBytes(Charset charset) method.

StringClassExample.java

import java.nio.charset.Charset;

public class

Page 27: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

import java.nio.charset.Charset;

public class StringClassExample {

public static void main(String[] args) {

String str = "tutorialkart";

byte[] bytes = str.getBytes(Charset.forName("UTF-8"));

System.out.print("The bytes are : ");

for(byte b:bytes) System.out.print(b+",");

}

}

Output to console is :

The bytes are : 116,117,116,111,114,105,97,108,107,97,114,116,

The bytes are : 116,117,116,111,114,105,97,108,107,97,114,116,

byte[] getBytes(String charsetName)

The method java.lang.String.getBytes(String charsetName) encodes this String into a sequence of bytes usingthe named charset, storing the result into a new byte array.Example : An example to demonstrategetBytes(String charsetName) method.

StringClassExample.java

import java.io.UnsupportedEncodingException;

public class StringClassExample {

public static void main(String[] args) { String str = "tutorialkart"; byte[] bytes = null; try { bytes = str.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.print("The bytes are : "); for(byte b:bytes) System.out.print(b+","); }}

Page 28: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

import java.io.UnsupportedEncodingException;

public class StringClassExample {

public static void main(String[] args) {

String str = "tutorialkart";

byte[] bytes = null;

try {

bytes = str.getBytes("UTF-8");

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.print("The bytes are : ");

for(byte b:bytes) System.out.print(b+",");

}

}

Output to console is :

The bytes are : 116,117,116,111,114,105,97,108,107,97,114,116,

The bytes are : 116,117,116,111,114,105,97,108,107,97,114,116,

Java Tutorial

⊩ Java Tutorial

⊩ Java Introduction

⊩ Java Installation

⊩ IDEs for Java Development

⊩ Java HelloWorld Program

⊩ Java Program Structure

Java - Object Oriented Concepts

⊩ Java Classes and Objects

⊩ Java - Inheritance

⊩ Java - Polymorphism

⊩ Java - Method Overloading

⊩ Java - Method Overriding

⊩ Java - Final Keyword

Page 29: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

⊩ Java - Final Keyword

⊩ Java - Java Abstraction

⊩ Java - Java Interfaces

⊩ Java - Abstract Methods and Classes

⊩ Java - Encapsulation

Java Programming Concepts

⊩ Java Data Types

⊩ Java Variable Types

⊩ Java Access Modifiers

⊩ Java Arithmetic Operators

⊩ Java Relational Operators

⊩ Java Decision Making

⊩ Java If Else

⊩ Java Switch

⊩ Java Loops

⊩ Java While Loop

⊩ Java For Loop

⊩ Java forEach

⊩ Java - String

⊩ Java - String Operations

⊩ Java Read Integer from Console

⊩ Java String to Integer

Java Exception Handling

⊩ Java - Call Stack

⊩ Java - Try Catch

⊩ Java - ArithmeticException

⊩ Java - ArrayIndexOutOfBoundsException

⊩ Java - ArrayStoreException

⊩ Java - NullPointerException

⊩ Java - NumberFormatException

⊩ Java - StringIndexOutOfBoundException

Java Design Pattern

⊩ Java - Singleton Pattern

Java - Files

Page 30: java.lang.String Constructors and Methods with Examples ... · java.lang.Stirng Constructors and Methods with Examples String is a sequence of characters. java.lang.String class provides

⊩ Java - List files and sub-folders

⊩ Java - Read File contents line by line using Stream

⊩ Java - Read File contents line by line using BufferedReader

⊩ Java - Replace String in File

Java ArrayList

⊩ Java - Print all Elements of ArrayList

⊩ Java - Delete Nth Element of ArrayList

⊩ Java ArrayList - Insert Element at Specific Position

⊩ Conver Java String Array to ArrayList

Java Date

⊩ Java - Date and Time

Java MySQL

⊩ Java JDBC - List all databases in MySQL Server