0% found this document useful (0 votes)
75 views8 pages

String (2 1 11)

The document contains code examples demonstrating various methods for working with Strings in Java, including: 1. Creating Strings using literals, constructors, and arrays. 2. Getting string lengths and performing operations like concatenation, case conversion, replacement, and reversal. 3. Extracting substrings and splitting strings. 4. Comparing strings for equality. 5. Trimming whitespace from strings.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views8 pages

String (2 1 11)

The document contains code examples demonstrating various methods for working with Strings in Java, including: 1. Creating Strings using literals, constructors, and arrays. 2. Getting string lengths and performing operations like concatenation, case conversion, replacement, and reversal. 3. Extracting substrings and splitting strings. 4. Comparing strings for equality. 5. Trimming whitespace from strings.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

package com;

public class StringCreate {

public static void main(String[] args) {

// You can create string Object with it's value-Direct way to create
// string.
// Here STRING is called String literal(ie,. A sequence of chars
// enclosed with double quotes).
String string = "STRING CREATE";
String string2 = "*************";

// You can create string object with new keyword and constructor.
String string3 = new String("Hello M2iss");
String string4 = new String(
"String is a sequence of chars.I can't be changed once
created-immutable");

System.out.println(string);
System.out.println(string2);
System.out.println(string3);
System.out.println(string4);

}
OUTPUT

STRING CREATE
*************
Hello M2iss
String is a sequence of chars.I can't be changed once created-immutable

package com;

public class StringCreateArray {

public static void main(String[] args) {

char[] c= {'m','2','i','s','s'};
String string = new String(c);

System.out.println("StringCreateArray");
System.out.println("*****************");
System.out.println(string);
}

}
OUTPUT
StringCreateArray
*****************
m2iss

package com;

public class StringLength {


public static void main(String[] args) {

String string = "Java-String";


String string1 = "Java - String";

// The length() method,returns the number of characters contained


// in the string object.
String string2 = new String("java-Stringlength()");
int l = string.length();

System.out.println("Stringlength()");
System.out.println("**************");

System.out.println("The length of first String is");


System.out.println(l);

System.out.println("The length of second String is");


System.out.println(string1.length());

System.out.println("The length of Third String is");


System.out.println(string2.length());
}
}

OUTPUT
Stringlength()
**************
The length of first String is
11
The length of second String is
13
The length of Third String is
19

package com;

public class StringConcatenation {

public static void main(String[] args) {

String string = "Java" + "String";


String string1 = new String("Concate" + "nation");
String string2 = "Concatenation";

System.out.println("StringConcatenation");
System.out.println("*******************");

System.out.println(string);
System.out.println(string1);
System.out.println(string+string2);
}

}
OUTPUT

StringConcatenation
*******************
JavaString
Concatenation
JavaStringConcatenation
package com;

public class StringCases {

public static void main(String[] args) {

String string = "SOFTWARE SOLUTION";


String string2 = "software solution";

System.out.println("STRING CASES");
System.out.println("*************");

// Returns a copy of given string converted to lowercase.


System.out.println("Given String to Lowercase:");
System.out.println(string.toLowerCase());

// Returns a copy of given string converted to Uppercase.


System.out.println("Given String to Uppercase:");
System.out.println(string2.toUpperCase());

STRING CASES
*************
Given String to Lowercase:
software solution
Given String to Uppercase:
SOFTWARE SOLUTION

package com;

public class StringReplace {

public static void main(String[] args) {

String string = "GURU";


String string2 = "GURU BUY BRU";
String string3 = "Live Life Well";

System.out.println("StringReplace");
System.out.println("*************");

System.out.println(string.replace('G', 'K'));
System.out.println(string2.replaceFirst("RU", "NA"));
System.out.println(string3.replaceAll("Li", "O"));
System.out.println(string2.replace("GURU BUY BRU", "RAM IS living in
Singapore"));

StringReplace
*************
KURU
GUNA BUY BRU
Ove Ofe Well
RAM IS living in Singapore

package com;

public class StringReverse {

public static void main(String[] args) {

String string = "NAGULAN";


System.out.println("Original String IS:");
System.out.println("******************");
System.out.println(string);

string = new StringBuffer(string).reverse().toString();


System.out.println("");
System.out.println("Reversal String IS:");
System.out.println("******************");
System.out.println(string);

Original String IS:


******************
NAGULAN

Reversal String IS:


******************
NALUGAN

package com;

public class StringSplit {

public static void main(String args[]) {

/*
* Java String class defines following methods to split Java String
* object. String[] split( String regularExpression ) Splits the string
* according to given regular expression. String[] split( String
* reularExpression, int limit ) Splits the string according to given
* regular expression. The number of resultant substrings by splitting
* the string is controlled by limit argument.
*/

// String to split.

String str = "one-two-three-four-five";

String[] temp;

// delimiter

String delimiter = "-";

// given string will be split by the argument delimiter provided.


//String[] split(String regex)
temp = str.split(delimiter);

// print substrings

for (int i = 0; i < temp.length; i++)

System.out.println(temp[i]);

System.out.println("");

// given string will be split by the argument delimiter provided.


//String[] split(String regex, int limit)
temp = str.split(delimiter, 3);

for (int i = 0; i < temp.length; i++)

System.out.println(temp[i]);

}
one
two
three
four
five

one
two
three-four-five

package com;

public class StringSubString {

public static void main(String[] args) {

String string = "Hello M2iss";

System.out.println("SubString");
System.out.println("**********");

//1) public String substring(int startIndex)


//This method returns new String object containing the substring of the
given string from specified startIndex (inclusive).

/*Note:This method can throw IndexOutOfBoundException


if startIndex is negative or grater than length of the string.*/

System.out.println(string.substring(6));

//2) public String substring(int startIndex,int endIndex)


//This method returns new String object containing the substring of the
given string from specified startIndex to endIndex.
//Here, startIndex is inclusive while endIndex is exclusive.

/*Note: This method can throw IndexOutOfBoundException


if startIndex is negative and if startIndex of endIndex is grater than
the string length.*/

System.out.println(string.substring(0,5));
}
}

SubString
**********
M2iss
Hello

package com;

public class StringEqual {

public static void main(String[] args) {

String string = "m2iss";


String string2 = "M2iss";
String string3 = new String(string2);

System.out.println("StringEqual");
System.out.println("************");

if (string == string2) {
System.out.println("The strings are equal" + string2);
} else
System.out.println("The strings are not equal");

if (string2.equals(string3)) {
System.out.println("The strings are equal" + "" + string2);
} else
System.out.println("The strings are not equal");

if (string.equals(string3)) {
System.out.println("The strings are equal" + string);
} else
System.out.println("The strings are not equal");
}
}

StringEqual
************
The strings are not equal
The strings are equalM2iss
The strings are not equal

package com;

public class StringTrim {

public static void main(String[] args) {

String string = " RAJARAM";

System.out.println("StringTrim()");
System.out.println("*************");
System.out.println("Before Trim()");
System.out.println(string);
System.out.println("After Trim()");
System.out.println(string.trim());
}
}

StringTrim()
*************
Before Trim()
RAJARAM
After Trim()
RAJARAM
package com;

public class StringSubSequence {

public static void main(String[] args) {

String string = "Hello World";

System.out.println("StringSubSequence");
System.out.println("******************");

// subSequence(int beginIndex, int endIndex)


/*
* beginIndex : the begin index, inclusive. endIndex : the end index,
* exclusive.
*/
System.out.println(string.subSequence(6, 11));

}
}

StringSubSequence
******************
World

package com;

public class StringIndexOf {

public static void main(String[] args) {

String string = "M2i Software Solution";


//String string2 = "Honesty is the best Policy";

System.out.println("StringIndexOf(char)");
System.out.println(string.indexOf('o'));
System.out.println("StringLastIndexOf(char)");
System.out.println(string.lastIndexOf('o'));
System.out.println("StringIndexOf(String)");
System.out.println(string.indexOf("Software"));
System.out.println("StringLastIndexOf(String)");
System.out.println(string.lastIndexOf("Solution"));
System.out.println("StringIndexOf(Char,FromIndex)");
System.out.println(string.indexOf('o', 6));
System.out.println("StringLastIndexOf(Char,FromIndex)");
System.out.println(string.lastIndexOf('o',6 ));
}

StringIndexOf(char)
5
StringLastIndexOf(char)
19
StringIndexOf(String)
4
StringLastIndexOf(String)
13
StringIndexOf(Char,FromIndex)
14
StringLastIndexOf(Char,FromIndex)
5

http://www.codingdiary.com/developers/developers/diary/javaapi/java/lang/SampleCode/IndexOf
StringExampleCode.html

http://download.oracle.com/javase/tutorial/java/data/manipstrings.html

http://www.tutorialspoint.com/java/java_date_time.htm

You might also like