Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * ParsingAndCasting: Class used to provide examples and practice in working with Strings
- * Course: ADEV-1003
- * Section: Section_number
- * Date Created: May 1, 2017
- * Last Updated: May 1, 2017
- */
- public class StringManipulation
- {
- public static void main(String[] args)
- {
- // Strings can also be created using the new keyword
- // Strings are an array of characters
- char[] helloMessage = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'};
- String message = new String(helloMessage);
- int randomNumber = 5554545;
- String randomString;
- String initialStringValue = "A String is an array of characters";
- String moreInformation = " with some built in methods";
- String concatenatedString;
- //1. convert randomNumber to a String store the result in randomString
- System.out.printf("Question 1: %s%n", randomString = Integer.toString(randomNumber));
- //2. print randomNumber using the substring method such that
- //it prints like a telephone number - "555-4545"
- System.out.printf("Question 2: %s%n",randomString.substring(0, 3) + "-" + randomString.substring(3, 7));
- //3. Determine the length of the string initialStringValue
- // Print the result to the console (ensure a line feed follows)
- System.out.printf("Question 3: %s%n", initialStringValue.length());
- //4. Use the appropriate method to concatenate moreInformation to the end of initialStringValue
- // Print the result to the console (ensure a line feed follows)
- //5. Determine the length of the string concatenatedString
- // Print the result to the console (ensure a line feed follows)
- //6. Determine whether moreInformation ends with the string "ods"
- // Print the result to the console (ensure a line feed follows)
- //7. Update the value in concatenatedString by replacing all instances of the string "ar"
- // in concatenatedString with the string "X-"
- // Print the result to the console (ensure a line feed follows)
- //8. Use the String.format to produce a new String using substring values of
- // concatenatedString at the following indexes: (0,12), (0,12) and (0,8). Ensure
- // print the result to the console - ensure the results are in uppercase characters
- // also ensure a line feed follows.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement