0% found this document useful (0 votes)
14 views11 pages

PBO Week 04 Strings Latihan

Strings on Java

Uploaded by

priyowbsarjono
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views11 pages

PBO Week 04 Strings Latihan

Strings on Java

Uploaded by

priyowbsarjono
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Project 404

package app_404;

public class StringConstructors


{
public static void main(String[] args)
{
char[] charArray = {'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y'};
String s = new String("hello");

String s1 = new String(); //no-argument constructor, length: 0


String s2 = new String(s); //String object as an argument
String s3 = new String(charArray); //array as an argument

//a char array and two integers as arguments


String s4 = new String(charArray, 6, 3);

System.out.printf("s1 = %s%ns2 = %s%ns3 = %s%ns4 = %s%n", s1, s2, s3, s4);


}

}
Project 405

package app_405;

public class StringMiscellaneous


{
public static void main(String[] args)
{
String s1 = "hello there";

char[] charArray = new char[5];


System.out.printf("s1: %s", s1);

// test length method


System.out.printf("%nLength of s1: %d", s1.length());

// loop through characters in s1 with charAt and display reversed


System.out.printf("%nThe string reversed is: ");
for (int count = s1.length() - 1; count >= 0; count--)
System.out.printf("%c ", s1.charAt(count));

// copy characters from string into charArray


s1.getChars(0, 5, charArray, 0);
System.out.printf("%nThe character array is: ");
for (char character : charArray)
System.out.print(character);

System.out.println();

}
}
Project 406

package app_406;

public class StringOperation


{
public static void main(String args[])
{
String s1 = new String("Saya makan malam sendiri");
String s2 = new String(s1 + " di rumah");
System.out.println(s2);
String s3 = s1.concat(" di rumah");
System.out.println(s3);
String s4 = s3.replace('s', 'm');
System.out.println(s4);
String s5 = s1.substring(4,10);
System.out.println(s5);
String s6 = s5.trim();
System.out.println(s6);
String s7 = s6.toUpperCase();
System.out.println(s7);
String s8 = s7.toLowerCase();
System.out.println(s8);
String s9 = "123";

//"type wrapper" classes (Integer, Double, Float, and Long)


Integer a = Integer.valueOf(s9);
Double b = Double.valueOf(s9);
Float c = Float.valueOf(s9);
Long d = Long.valueOf(s9);
//print the values
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}

}
Project 407

package app_407;

public class StringCompare


{
public static void main(String args[])
{
String aName = "ROcky";
String bName = "Rocky";

if (aName == bName)
{
System.out.println("== worked!");
}else
System.out.println("== didn't work!");

if (aName.equals(bName))
{
System.out.println("equals worked!");
}else
System.out.println("equals didn't work!");

if (aName.equalsIgnoreCase(bName))
{
System.out.println("equalsIgnoreCase worked!");
}else
System.out.println("equalsIgnoreCase didn't work!");

String s1 = new String("abcfj");


String s2 = new String("abcdz");
System.out.println("String s1 = "+ s1);
System.out.println("String s2 = "+ s2);
System.out.println("Hasil perbandingan antara s1 dan s1 : " + s1.compareTo(s2));
System.out.println("Hasil perbandingan antara s1 dan s1 : " + s2.compareTo(s1));
}

}
Project 408

package app_408;

public class StringBuilderConstructors


{
public static void main(String[] args)
{
StringBuilder buffer1 = new StringBuilder();
StringBuilder buffer2 = new StringBuilder(10);
StringBuilder buffer3 = new StringBuilder("hello");

System.out.printf("buffer1 = \"%s\", Kapasitas: %d %n", buffer1, buffer1.capacity());


System.out.printf("buffer2 = \"%s\", Kapasitas: %d %n", buffer2, buffer2.capacity());
System.out.printf("buffer3 = \"%s\", Kapasitas: %d %n", buffer3, buffer3.capacity());
}

}
Project 409

package app_409;

public class StringBuilderChars


{
public static void main(String[] args)
{
StringBuilder buffer = new StringBuilder("hello there");

//returns the String representation of buffer object


System.out.printf("buffer = %s%n", buffer.toString());

System.out.printf("Character at 0: %s%nCharacter at 4: %s%n%n",


buffer.charAt(0), buffer.charAt(4));

char[] charArray = new char[buffer.length()];


buffer.getChars(0, buffer.length(), charArray, 0);
System.out.print("The characters are: ");
for (char character : charArray)
System.out.print(character);

buffer.setCharAt(0, 'H');
buffer.setCharAt(6, 'T');
System.out.printf("%n%nbuffer = %s", buffer.toString());

//rearrange in the opposite order


buffer.reverse();

System.out.printf("%n%nbuffer = %s%n", buffer.toString());


}

}
Project 410

package app_410;

public class StringBufferMethods


{
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer(14);
System.out.println("Kapasitas s1 = " + s1.capacity());
System.out.println("Panjang s1 = " + s1.length());
s1.append("Saya");
System.out.println(s1);
System.out.println("Kapasitas s1 = " + s1.capacity());
System.out.println("Panjang s1 = " + s1.length());
s1.setLength(3);
System.out.println(s1);
System.out.println("Kapasitas s1 = " + s1.capacity());
System.out.println("Panjang s1 = " + s1.length());

You might also like