Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class StringProg
- {
- public static void main(String[]args)
- {
- String s1 = "Happy"; //string literal
- System.out.println("S1 is " + s1);
- String s2; //not initialized to anything
- // System.out.println("S2 is " + s2);
- s2 = "Thanksgiving";
- System.out.println("S2 is " + s2);
- String s3 = new String("turkey");
- System.out.println("S3 is " + s3);
- String s4 = new String();
- System.out.println("S4 is " + s4);
- System.out.println("S4 has a length of " + s4.length());
- s4 = "Stuffing";
- System.out.println("S4 is " + s4);
- System.out.println("S4 has a length of " + s4.length());
- /*
- String s5;
- s5 = s4;
- System.out.println("S5 is " + s5);
- String s6;
- s6 = s1 + " " + s2;
- System.out.println("S6 is " + s6);
- System.out.println("S6 has a length of " + s6.length());
- //concatenate strings
- s1 += s2;
- System.out.println("S1 is now " + s1);
- System.out.println("S1 has a length of " + s1.length());
- s6 = s1.substring(0,5);
- //gives u "happy" (all characters from 0 to 4) h-0 a-1 p-2 p-3 y-4
- System.out.println("S6 is now " + s6);
- s6 = s1.substring(6,10);
- System.out.println("S6 is " + s6);
- s6 = s1.substring(11,17);
- System.out.println("S6 is " + s6);
- s6 = s1.substring(11,20); //will not work because 20 is past
- //the parameters
- // System.out.println("S6 is " + s6);
- s6 = s1.substring(9);
- System.out.println("S6 is " + s6);
- for(int x = 0; x < s1.length();x++)
- System.out.println(s1.substring(x));
- for(int y =0; y<17;y++)
- {
- System.out.println(s1.substring(y,y+1));
- }
- int x;
- x = s1.indexOf("hank");
- System.out.println("hank at position " + x);
- x = s1.indexOf("cat");
- System.out.println("Cat is at position..." + x);
- s1 = "HAPPY";
- s2 = s1.toLowerCase();
- System.out.println("s1 is " + s1);
- System.out.println("s2 is " + s2);
- s2.toUpperCase();
- System.out.println("s2 is now " + s2);
- s1 = "apple";
- s2 = "apple"; //string literals
- if(s1 == s2)
- System.out.println("S1 equals S2");
- //says they are equal because they are at the same memory
- //location
- else
- System.out.println("S1 does not equal S2");
- String s7 = new String("apple");
- if(s1 ==s7)
- System.out.println("S1 equals S7");
- else
- System.out.println("S1 does not equal S7.");
- if(s1.equals(s7))
- System.out.println("S1 equals S7.");
- else
- System.out.println("S1 does not equal S7.");
- //does not work cant use < sign
- //System.out.println(s1.compareTo(s7));
- // if(s1 < s7)
- // System.out.println("Test");
- s1 = "dog";
- System.out.println(s1.compareTo(s7));
- System.out.println(s7.compareTo(s1));
- if(s1.compareTo(s7) < 0)
- System.out.println(s1 + " less than " + s7);
- else
- System.out.println(s7 + " less than " + s1);
- s1 = "Bob";
- s2 = "bob";
- //compares the values using ascii values for this A = 65 a = 97
- s1 = "one";
- s2 = "two";
- for(int y = 0; y < 5; y++)
- s1 += s2;
- System.out.println("S1 is " + s1);
- System.out.println("S2 is " + s2);
- for(int y =0; y<5;y++)
- System.out.println(s1.charAt(y));
- // System.out.println("America converted to pig latin is" + PigLatin.convert("America"));
- // System.out.println("child converted to pig latin is" + PigLatin.convert("child"));
- // System.out.println("happy converted to pig latin is" + PigLatin.convert("happy"));
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement