Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. /**
  2. * ParsingAndCasting: Class used to provide examples and practice in working with Strings
  3. * Course: ADEV-1003
  4. * Section: Section_number
  5. * Date Created: May 1, 2017
  6. * Last Updated: May 1, 2017
  7. */
  8. public class StringManipulation
  9. {
  10. public static void main(String[] args)
  11. {
  12.  
  13. // Strings can also be created using the new keyword
  14. // Strings are an array of characters
  15. char[] helloMessage = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'};
  16. String message = new String(helloMessage);
  17.  
  18.  
  19.  
  20. int randomNumber = 5554545;
  21. String randomString;
  22. String initialStringValue = "A String is an array of characters";
  23. String moreInformation = " with some built in methods";
  24. String concatenatedString;
  25.  
  26.  
  27. //1. convert randomNumber to a String store the result in randomString
  28. System.out.printf("Question 1: %s%n", randomString = Integer.toString(randomNumber));
  29.  
  30.  
  31.  
  32. //2. print randomNumber using the substring method such that
  33. //it prints like a telephone number - "555-4545"
  34. System.out.printf("Question 2: %s%n",randomString.substring(0, 3) + "-" + randomString.substring(3, 7));
  35.  
  36.  
  37. //3. Determine the length of the string initialStringValue
  38. // Print the result to the console (ensure a line feed follows)
  39. System.out.printf("Question 3: %s%n", initialStringValue.length());
  40.  
  41.  
  42. //4. Use the appropriate method to concatenate moreInformation to the end of initialStringValue
  43. // Print the result to the console (ensure a line feed follows)
  44.  
  45.  
  46. //5. Determine the length of the string concatenatedString
  47. // Print the result to the console (ensure a line feed follows)
  48.  
  49.  
  50. //6. Determine whether moreInformation ends with the string "ods"
  51. // Print the result to the console (ensure a line feed follows)
  52.  
  53.  
  54. //7. Update the value in concatenatedString by replacing all instances of the string "ar"
  55. // in concatenatedString with the string "X-"
  56. // Print the result to the console (ensure a line feed follows)
  57.  
  58.  
  59. //8. Use the String.format to produce a new String using substring values of
  60. // concatenatedString at the following indexes: (0,12), (0,12) and (0,8). Ensure
  61. // print the result to the console - ensure the results are in uppercase characters
  62. // also ensure a line feed follows.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement