Java Tutorial
IntroductionEnvironment SetupIDEBuild ManagementLanguage SpecificationBasic ProgramVariablesData TypesPackagesModifiersConditionalsLoopsObject OrientedClassesSuperInterfacesEnumStatic importInheritanceAbstractionEncapsulationPolymorphismBoxing & UnboxingConversion Formatting numbers Arrays Command line arguments in java Variable Number of arguments in Java Exception handling in Java String handling in Java StringBuffer and StringBuilder in Java Mathematical Operations in Java Date and Time in Java Regular expressions in Java Input output programming in Java File Handling Nested Classes Collections Generics Serialization Socket programming Multi-Threading Annotations Lambda Expressions Reflections in Java Singleton class in Java Runtime Class in JavaHow to load resource in JavaHow to load properties file in JavaAdvanced
Log4j – Logging framework in JavaInterview Questions in JavaString is nothing but a sequence of characters. Various ways in which you can create strings in Java are given below.
char [] city = {‘B’,’r’,’i’,’s’,’b’,’a’,’n’,’e’}
String city = “Brisbane”
String city = new String(city)
String city = new String(“Brisbane”)
StringBuffer city = new StringBuffer(“Brisbane”)
StringBuilder city = new StringBuilder(“Brisbane”)
There are 2 types of strings in Java.- Immutable – Strings created in examples 1 – 4 are immutable strings
- Mutable – StringBuffer and StringBuilder. When you think that strings are going to modified many times, you can use these classes. Difference between StringBuffer and StringBuilder is that StringBuffer is synchronized while StringBuilder is not.
package strings;
/**
* Created by Sagar on 16-04-2016.
*/
public class strings {
public static void main(String [] args){
String city = new String("Brisbane");
System.out.println("String in focus -> " + city);
//To get the character at specific index
System.out.println("Character at index 0 -> " + city.charAt(0));
//To get the ascii value of a character at specific index
System.out.println("Ascii value of Character at index 0 -> " + city.codePointAt(0));
//To compare the strings lexicographically/alphabetically
System.out.println("Comparing Brisbane with Sydney lexicographically -> " + city.compareTo("Sydney"));
System.out.println("Comparing Brisbane with Sydney in case insensitive manner -> " + city.compareToIgnoreCase("Sydney"));
//To concatinate the string
System.out.println("Concatinating Sydney to Brisbane -> " + city.concat("Sydney"));
System.out.println("city is still pointing to Brisbane -> " + city);
//To check if String contains other string
System.out.println("Brisbane contains bane? -> " + city.contains("bane"));
//To check if string1's content is equal to string 2
System.out.println("is Brisbane equal to Sydney? -> " + city.contentEquals("Sydney"));
System.out.println("is Brisbane equal to Brisbane? -> " + city.contentEquals("Brisbane"));
//To check if String ends with other string
System.out.println("Brisbane ends with bane? -> " + city.endsWith("bane"));
//To check if String starts with other string
System.out.println("Brisbane starts with bane? -> " + city.startsWith("bane"));
//To check if String matches with other string in case insensitive manner
System.out.println("Brisbane is equals to Sydney? -> " + city.equalsIgnoreCase("Sydney"));
//To check if String variable is empty
System.out.println("Brisbane is empty? -> " + city.isEmpty());
//To get the length of the string
System.out.println("Brisbane length is -> " + city.length());
//replace - to replace the character in string by other character
System.out.println("Replacing B by C -> " + city.replace('B','C'));
//replaceAll - to replace the pattern in string with other string
System.out.println("Replacing B by C -> " + city.replaceAll("B","C"));
System.out.println("Replacing [Bb] by C -> " + city.replaceAll("[Bb]","C"));
//matches - to check if string matches with the pattern as it is
System.out.println("Brisbane matches with Brisbane? -> " + city.matches("Brisbane"));
//split - to split the string by delimiter
System.out.println("Splitting Brisbane by s -> " + city.split("s")[0]);
System.out.println("Splitting Brisbane by s -> " + city.split("s")[1]);
//substring - to get the substring from original string
System.out.println("Substring of Brisbane in the range 4-8 -> " + city.substring(4,8));
//toCharArray - to convert the string into array
char [] array = city.toCharArray();
System.out.println("Brisbane in array -> " + array[0] + array[1] + array[2]);
//toLowerCase - to convert the string to lower case
System.out.println("Brisbane in lower case is -> " + city.toLowerCase());
//toUpperCase - to convert the string to upper case
System.out.println("Brisbane in upper case is -> " + city.toUpperCase());
//trim - to remove the white spaces from start and end of the string
System.out.println("Brisbane after trimming is -> " + city.trim());
}
}
Here is the output of above example.
String in focus -> Brisbane
Character at index 0 -> B
Ascii value of Character at index 0 -> 66
Comparing Brisbane with Sydney lexicographically -> -17
Comparing Brisbane with Sydney in case insensitive manner -> -17
Concatinating Sydney to Brisbane -> BrisbaneSydney
city is still pointing to Brisbane -> Brisbane
Brisbane contains bane? -> true
is Brisbane equal to Sydney? -> false
is Brisbane equal to Brisbane? -> true
Brisbane ends with bane? -> true
Brisbane starts with bane? -> false
Brisbane is equals to Sydney? -> false
Brisbane is empty? -> false
Brisbane length is -> 8
Replacing B by C -> Crisbane
Replacing B by C -> Crisbane
Replacing [Bb] by C -> CrisCane
Brisbane matches with Brisbane? -> true
Splitting Brisbane by s -> Bri
Splitting Brisbane by s -> bane
Substring of Brisbane in the range 4-8 -> bane
Brisbane in array -> Bri
Brisbane in lower case is -> brisbane
Brisbane in upper case is -> BRISBANE
Brisbane after trimming is -> Brisbane
Web development and Automation testing
solutions delivered!!