Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. class StringProg
  2. {
  3. public static void main(String[]args)
  4. {
  5.  
  6. String s1 = "Happy"; //string literal
  7. System.out.println("S1 is " + s1);
  8. String s2; //not initialized to anything
  9. // System.out.println("S2 is " + s2);
  10. s2 = "Thanksgiving";
  11. System.out.println("S2 is " + s2);
  12.  
  13. String s3 = new String("turkey");
  14. System.out.println("S3 is " + s3);
  15.  
  16. String s4 = new String();
  17. System.out.println("S4 is " + s4);
  18.  
  19. System.out.println("S4 has a length of " + s4.length());
  20. s4 = "Stuffing";
  21.  
  22. System.out.println("S4 is " + s4);
  23. System.out.println("S4 has a length of " + s4.length());
  24. /*
  25. String s5;
  26. s5 = s4;
  27. System.out.println("S5 is " + s5);
  28.  
  29. String s6;
  30. s6 = s1 + " " + s2;
  31. System.out.println("S6 is " + s6);
  32. System.out.println("S6 has a length of " + s6.length());
  33.  
  34. //concatenate strings
  35. s1 += s2;
  36. System.out.println("S1 is now " + s1);
  37. System.out.println("S1 has a length of " + s1.length());
  38.  
  39. s6 = s1.substring(0,5);
  40. //gives u "happy" (all characters from 0 to 4) h-0 a-1 p-2 p-3 y-4
  41. System.out.println("S6 is now " + s6);
  42. s6 = s1.substring(6,10);
  43. System.out.println("S6 is " + s6);
  44. s6 = s1.substring(11,17);
  45. System.out.println("S6 is " + s6);
  46. s6 = s1.substring(11,20); //will not work because 20 is past
  47. //the parameters
  48. // System.out.println("S6 is " + s6);
  49.  
  50. s6 = s1.substring(9);
  51. System.out.println("S6 is " + s6);
  52. for(int x = 0; x < s1.length();x++)
  53. System.out.println(s1.substring(x));
  54.  
  55. for(int y =0; y<17;y++)
  56. {
  57. System.out.println(s1.substring(y,y+1));
  58. }
  59.  
  60. int x;
  61. x = s1.indexOf("hank");
  62. System.out.println("hank at position " + x);
  63. x = s1.indexOf("cat");
  64. System.out.println("Cat is at position..." + x);
  65.  
  66. s1 = "HAPPY";
  67. s2 = s1.toLowerCase();
  68. System.out.println("s1 is " + s1);
  69. System.out.println("s2 is " + s2);
  70. s2.toUpperCase();
  71. System.out.println("s2 is now " + s2);
  72.  
  73. s1 = "apple";
  74. s2 = "apple"; //string literals
  75.  
  76. if(s1 == s2)
  77. System.out.println("S1 equals S2");
  78. //says they are equal because they are at the same memory
  79. //location
  80. else
  81. System.out.println("S1 does not equal S2");
  82.  
  83. String s7 = new String("apple");
  84. if(s1 ==s7)
  85. System.out.println("S1 equals S7");
  86. else
  87. System.out.println("S1 does not equal S7.");
  88.  
  89. if(s1.equals(s7))
  90. System.out.println("S1 equals S7.");
  91. else
  92. System.out.println("S1 does not equal S7.");
  93.  
  94. //does not work cant use < sign
  95. //System.out.println(s1.compareTo(s7));
  96. // if(s1 < s7)
  97. // System.out.println("Test");
  98.  
  99.  
  100. s1 = "dog";
  101. System.out.println(s1.compareTo(s7));
  102. System.out.println(s7.compareTo(s1));
  103.  
  104. if(s1.compareTo(s7) < 0)
  105. System.out.println(s1 + " less than " + s7);
  106. else
  107. System.out.println(s7 + " less than " + s1);
  108.  
  109. s1 = "Bob";
  110. s2 = "bob";
  111. //compares the values using ascii values for this A = 65 a = 97
  112.  
  113. s1 = "one";
  114. s2 = "two";
  115.  
  116. for(int y = 0; y < 5; y++)
  117. s1 += s2;
  118. System.out.println("S1 is " + s1);
  119. System.out.println("S2 is " + s2);
  120.  
  121. for(int y =0; y<5;y++)
  122. System.out.println(s1.charAt(y));
  123.  
  124. // System.out.println("America converted to pig latin is" + PigLatin.convert("America"));
  125. // System.out.println("child converted to pig latin is" + PigLatin.convert("child"));
  126. // System.out.println("happy converted to pig latin is" + PigLatin.convert("happy"));
  127.  
  128. */
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement