In this post we will see how to iterate ArrayList in java. We can iterate ArrayList either using 1. For loop 2. Iterator. 3. While loop We will see examples for all these methods. 1. Iterate ArrayL
How to initialize ArrayList in java
We have many ways to initialize ArrayList in java. ArrayList can be initialized in one line, Using constructors, Using Arrays. 1. Initialize ArrayList using Constructors [crayon-68ceafbfdce4488797
String trim example
String trim method is used to remove the leading and trailing white spaces in a String.
1 |
public String trim() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kscodes.sampleproject; public class StringTrimExample { public static void main(String[] args) { String testStr1 = " Make me Slim trim "; System.out.println("Original String ::" + testStr1); System.out.println("Original String length ::" + testStr1.length()); System.out.println("----------------------------------"); System.out.println("After trimming ::"+ testStr1.trim()); System.out.println("After trimming String length ::"+ testStr1.trim().length()); } } |
String concat example
Concat method of String in java is used to add a new String at the end of the original String.
1 |
public String concat(String str) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.kscodes.sampleproject; public class StringConcatExample { public static void main(String[] args) { String testStr1 = "This is test String"; String testStr2 = " Used for Concat Example."; System.out.println("After 2 String concat::" + testStr1.concat(testStr2)); System.out.println("Adding 1 more to concat::" + testStr1.concat(testStr2).concat("Added more to this String")); } } |
String endsWith example
String endsWith is used to check if a String ends with the given suffix.
1 |
public boolean endsWith(String suffix) |
String startsWith example
String startsWith method is used to check if a String starts with a specified substring. Startswith method has 2 variations and we will see both these.
1 2 |
public boolean startsWith(String prefix) public boolean startsWith(String prefix,int toffset) |
String replace methods example
String replace methods are used to replace a part of string with a new string. We have 4 variations for replace in String
1 2 3 4 |
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 lastIndexOf example
String lastIndexOf returns the index of the last occurrence of the given String for specified conditions searching backward. String lastIndexOf method can be used with multiple types of parameters
String indexOf example
String indexOf returns the index of the given String for specified conditions. String indexOf method can be used with multiple types of parameters as shown below
1 2 3 4 |
public int indexOf(int ch) public int indexOf(int ch,int fromIndex) public int indexOf(String str) public int indexOf(String str,int fromIndex) |
String toUpperCase example
String toUpperCase method is used to converts all characters in a given string to upper case. String has 2 toUpperCase methods.
1 2 3 |
public String toUpperCase() public String toUpperCase(Locale locale) |