String manipulation Flashcards
How do you split a sentence (ie., “My name is Bob”) by a space?
String s = “My name is Bob”;
String[] words = s.split(“ “);
If you have int[] numbers = { 1, 2, 3 }; what built in method can you used to sort it?
Arrays.sort(numbers);
If you have int[] array1 = { 1, 2, 3 }; and int[] array2 = { 1, 2, 3 };
What built in method can you use to check the 2 arrays are equal?
Arrays.equal(array1, array2);
How do you represent a int[] as a List<Integer>?</Integer>
int[] numbers = {1,2,3};
List<Integer> list = Arrays.asList(numbers);</Integer>
How can you use a built in Collections method to sort List<Integer> list = Arrays.asList(numbers);?</Integer>
Collections.sort(list);
How can you use a built in Collections method to reverse List<Integer> list = Arrays.asList(numbers);?</Integer>
Collections.reverse(list);
How do you check if a string contains vowels?
- Store the vowels as a String vowels = “aeiou”
- Loop through each char in s.toCharArray()
- and check if vowels.contains(String.valueOf(char))
- if it does increment the counter
- return the counter