Strings are one of the simplest datatypes in Java, but allows great flexibility. I present the mostly used methods used in string manipulation in Java. Strings are immutable - meaning it cannot be changed. If we need to change anything, we create new String object, with a change. String object support many string manipulation functions. The most commonly used methods are described in this post.
Like in any other programming language, string indexes start at 0. The first letter is at 0th position.
String s = 'Doing things in Java'
//Doing things in Java
//01234567890123456789
int x =s.length() //value = 20
for (int i=0; i < x; i++){
system.out.println("Char at position " + i + " is " + s.get(i));
}
It also works with advanced for statement introduced in Java 5. It works like foreach in other programming language:
for (String s1 : s){
system.out.println(s1);
}
The String class also uses iterator, and we can use iterator
//First Occurrence of the search string
s.indexOf("program") //value = 0
//Another use
s.indexOf("g") //3
s.indexOf("f") //-1 (when it doesn't find, it returns -1)
s.indexOf("g",4) //10 (second parameter is index Where to start search)
//Test whether string starts or ends with specific string
s.startsWith("Prog") //true
s.startsWith("java") //false
s.endsWith('Java') //true
//Return all characters starting at first parameter
//and ending at second parameter,
//but not including index at second parameter
//length of substring in following = 7 - 4 = 3
s.substring(4,7) //ram (consist chars 4,5,6)
//Change case
s.toUpperCase() //PROGRAMMING IN JAVA
Let us define a string first
Strings are defined with String keyword. We must know the difference between char and string. The primitive type char is one character and the class String is zero or more characters long. We use single quotes for char, and double quotes for String. We define character and string as followschar chr = 'a';
String str = "Hello";
Like in any other programming language, string indexes start at 0. The first letter is at 0th position.
String s = 'Doing things in Java'
//Doing things in Java
//01234567890123456789
To define more then one characters, we must define character array.
char[] str1 = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
To convert it back to string, we can use following line
String str2 = new String(str1);
Accessing any character of string
The programmers who come from C/C++ background, use string as array of chars. But in Java, String is a separate class. We can also define and use array of chars for strings, but String class supports many string manipulation functions, not available to array of chars. The Strings are immutable, and cannot be changed. New String should be created in every step. To get single character, we use get() method. Index of the character is passed to the get() method to get the character at specified position.s.get(6);
//Returns 't'
Find the length of string
Length of string can be found with length() function. This function can be useful to avoid index out of bounds problem while manipulating strings. If we need to loop over each character in string, then we have to know the length of string first.
//Length of string sint x =s.length() //value = 20
Iterate over each characters in the String
Let us now iterate over each character in the string. For that, we can use simple for loop, while loop.for (int i=0; i < x; i++){
system.out.println("Char at position " + i + " is " + s.get(i));
}
It also works with advanced for statement introduced in Java 5. It works like foreach in other programming language:
for (String s1 : s){
system.out.println(s1);
}
The String class also uses iterator, and we can use iterator
Find position where substring starts
To find the index of the first occurrence of substring inside a string, we use indexOf() function. We can pass substring as only one argument in the string. It simply returns the position of strings.indexOf("program") //value = 0
//Another use
s.indexOf("g") //3
s.indexOf("f") //-1 (when it doesn't find, it returns -1)
s.indexOf("g",4) //10 (second parameter is index Where to start search)
//Test whether string starts or ends with specific string
s.startsWith("Prog") //true
s.startsWith("java") //false
s.endsWith('Java') //true
//Return all characters starting at first parameter
//and ending at second parameter,
//but not including index at second parameter
//length of substring in following = 7 - 4 = 3
s.substring(4,7) //ram (consist chars 4,5,6)
//Change case
s.toUpperCase() //PROGRAMMING IN JAVA
No comments:
Post a Comment