Java String split method is used for splitting a String into its substrings based on the given delimiter or regular expression.
String: chaitanya@singh
Regular Expression: @
Output : {"chaitanya", "singh"}
delimiter is a character that identifies the beginning or the end of a character string. An example of a delimiter is the comma character, which acts as a field delimiter in a sequence of comma-separated values.
Example 1: showing the split(): array output
package com.cerotid.fundamentals.split;
public class SplitDemo {
public static void main(String[] args) {
String str = new String("28/12/2013");
String[] array2 = str.split("/");
for (String temp2 : array2) {
System.out.println(temp2);
}
}
}
Output:
28
12
2013
Example 1: showing the split(): arraylist output
package com.cerotid.fundamentals.split;
import java.util.Arrays;
public class SplitDemo {
public static void main(String[] args) {
String str = new String("28/12/2013");
String[] array1 = str.split("/");
for (String temp1 : array1) {
System.out.println(Arrays.asList(temp1));
}
}
}
Output:
[28]
[12]
[2013]
ArrayList<String> cars = new ArrayList<String>(); // Create an ArrayList object
import java.util.ArrayList;
public class MyClass {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars);
}
}
Output:
[Volvo, BMW, Ford, Mazda]
Java Arrays:
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To create an array of String, you could write:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
To create an array of integers, you could write:
int[] myNum = {10, 20, 30, 40};
Access the Elements of an Array
You access an array element by referring to the index number.
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
// Outputs Volvo
Loop Through an Array with For-Each
The following example outputs all elements in the cars array, using a "for-each" loop:
Loop Through an Array with For-Each
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String i : cars) {
System.out.println(i);
}
Output:
Volvo
BMW
Ford
Mazda
Java ArrayList:
Example
Create an ArrayList object called cars that will store strings:
import java.util.ArrayList; // import the ArrayList class
ArrayList<String> cars = new ArrayList<String>(); // Create an ArrayList object
import java.util.ArrayList;
public class MyClass {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars);
}
}
Output:
[Volvo, BMW, Ford, Mazda]
Loop Through an ArrayList
public class MyClass {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
for (String i : cars) {
System.out.println(i);
}
}
}
Output:
Volvo
BMW
Ford
Mazda
No comments:
Post a Comment