Tuesday, June 5, 2018

String Basic

String to Int

String myString = "1234";
int foo = Integer.parseInt(myString);
Declare Arry

For primitive types:
int[] myIntArray = new int[3];
int[] myIntArray = {1,2,3};
int[] myIntArray = new int[]{1,2,3};
For classes, for example String, it's the same:
String[] myStringArray = new String[3];
String[] myStringArray = {"a","b","c"};
String[] myStringArray = new String[]{"a","b","c"};
The third way of initializing is useful when you declare the array first and then initialize it. The cast is necessary here.
String[] myStringArray;
myStringArray = new String[]{"a","b","c"};
For-each

for (char ch: "xyz".toCharArray()) {
}
List
List<String> someList = new ArrayList<String>();
Get String character by index

String text = "foo";
char a_char = text.charAt(0);
System.out.println(a_char); // Prints f
Character to String

Character.toString("C")


No comments:

Post a Comment