Java Examples - Converting into uppercase
Problem Description:
How to convert a string totally into upper case?
Solution:
Following example changes the case of a string to upper case by using String toUpperCase() method.
public class StringToUpperCaseEmp {
public static void main(String[] args) {
String str = "string abc touppercase ";
String strUpper = str.toUpperCase();
System.out.println("Original String: " + str);
System.out.println("String changed to upper case: "
+ strUpper);
}
}
Result:
The above code sample will produce the following result.
Original String: string abc touppercase
String changed to upper case: STRING ABC TOUPPERCASE
Java Examples - String region match
Problem Description:
How to match regions in strings ?
Solution:
Following example determines region matchs in two strings by using regionMatches() method.
public class StringRegionMatch{
public static void main(String[] args){
String first_str = "Welcome to Microsoft";
String second_str = "I work with Microsoft";
boolean match = first_str.
regionMatches(11, second_str, 12, 9);
System.out.println("first_str[11 -19] == "
+ "second_str[12 - 21]:-"+ match);
}
}
Result:
The above code sample will produce the following result.
first_str[11 -19] == second_str[12 - 21]:-true
Java Examples - Replacing a String
Problem Description:
How to replace a substring inside a string by another one ?
Solution:
This example describes how replace method of java String class can be used to replace character or substring by new one.
public class StringReplaceEmp{
public static void main(String args[]){
String str="Hello World";
System.out.println( str.replace( 'H','W' ) );
System.out.println( str.replaceFirst("He", "Wa") );
System.out.println( str.replaceAll("He", "Ha") );
}
}
Result:
The above code sample will produce the following result.
Wello World
Wallo World
Hallo World
Java Examples - Comparing performance
Problem Description:
How to compare performance of string creation ?
Solution:
Following example compares the performance of two strings created in two different ways.
public class StringComparePerformance{
public static void main(String[] args){
long startTime = System.currentTimeMillis();
for(int i=0;i<50000;i++){
String s1 = "hello";
String s2 = "hello";
}
long endTime = System.currentTimeMillis();
System.out.println("Time taken for creation"
+ " of String literals : "+ (endTime - startTime)
+ " milli seconds" );
long startTime1 = System.currentTimeMillis();
for(int i=0;i<50000;i++){
String s3 = new String("hello");
String s4 = new String("hello");
}
long endTime1 = System.currentTimeMillis();
System.out.println("Time taken for creation"
+ " of String objects : " + (endTime1 - startTime1)
+ " milli seconds");
}
}
Result:
The above code sample will produce the following result.The result may vary.
Time taken for creation of String literals : 0 milli seconds
Time taken for creation of String objects : 16 milli seconds
Java Examples - String optimization
Problem Description:
How to optimize string creation ?
Solution:
Following example optimizes string creation by using String.intern() method.
public class StringOptimization{
public static void main(String[] args){
String variables[] = new String[50000];
for( int i=0;i <50000;i++){
variables[i] = "s"+i;
}
long startTime0 = System.currentTimeMillis();
for(int i=0;i<50000;i++){
variables[i] = "hello";
}
long endTime0 = System.currentTimeMillis();
System.out.println("Creation time"
+ " of String literals : "+ (endTime0 - startTime0)
+ " ms" );
long startTime1 = System.currentTimeMillis();
for(int i=0;i<50000;i++){
variables[i] = new String("hello");
}
long endTime1 = System.currentTimeMillis();
System.out.println("Creation time of"
+ " String objects with 'new' key word : "
+ (endTime1 - startTime1)
+ " ms");
long startTime2 = System.currentTimeMillis();
for(int i=0;i<50000;i++){
variables[i] = new String("hello");
variables[i] = variables[i].intern();
}
long endTime2 = System.currentTimeMillis();
System.out.println("Creation time of"
+ " String objects with intern(): "
+ (endTime2 - startTime2)
+ " ms");
}
}
Result:
The above code sample will produce the following result.The result may vary.
Creation time of String literals : 0 ms
Creation time of String objects with 'new' key word : 31 ms
Creation time of String objects with intern(): 16 ms
Java Examples - String formatting
Problem Description:
How to format strings ?
Solution:
Following example returns a formatted string value by using a specific locale, format and arguments in format() method
import java.util.*;
public class StringFormat{
public static void main(String[] args){
double e = Math.E;
System.out.format("%f%n", e);
System.out.format(Locale.GERMANY, "%-10.4f%n%n", e);
}
}
Result:
The above code sample will produce the following result.
2.718282
2,7183
Java Examples - Concatenate Strings
Problem Description:
How to optimize string concatenation ?
Solution:
Following example shows performance of concatenation by using "+" operator and StringBuffer.append() method.
public class StringConcatenate{
public static void main(String[] args){
long startTime = System.currentTimeMillis();
for(int i=0;i<5000;i++){
String result = "This is"
+ "testing the"
+ "difference"+ "between"
+ "String"+ "and"+ "StringBuffer";
}
long endTime = System.currentTimeMillis();
System.out.println("Time taken for string"
+ "concatenation using + operator : "
+ (endTime - startTime)+ " ms");
long startTime1 = System.currentTimeMillis();
for(int i=0;i<5000;i++){
StringBuffer result = new StringBuffer();
result.append("This is");
result.append("testing the");
result.append("difference");
result.append("between");
result.append("String");
result.append("and");
result.append("StringBuffer");
}
long endTime1 = System.currentTimeMillis();
System.out.println("Time taken for String concatenation"
+ "using StringBuffer : "
+ (endTime1 - startTime1)+ " ms");
}
}
Result:
The above code sample will produce the following result.The result may vary.
Time taken for stringconcatenation using + operator : 0 ms
Time taken for String concatenationusing StringBuffer : 22 ms
Java Examples - String unicode
Problem Description:
How to determine the Unicode code point in string ?
Solution:
This example shows you how to use codePointBefore() method to return the character (Unicode code point) before the specified index.
public class StringUniCode{
public static void main(String[] args){
String test_string="Welcome to TutorialsPoint";
System.out.println("String under test is = "+test_string);
System.out.println("Unicode code point at"
+" position 5 in the string is = "
+ test_string.codePointBefore(5));
}
}
Result:
The above code sample will produce the following result.
String under test is = Welcome to TutorialsPoint
Unicode code point at position 5 in the string is =111
No comments:
Post a Comment