String Operation
https://www.w3schools.com/java/java_strings.asphttps://www.geeksforgeeks.org/strings-in-java/
// Java code to illustrate String
import java.io.;
import java.lang.;
class BelajarString{
public static void main(String[] args){
String greeting = "Hello";
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is: " + txt.length());
String txt = "Hello World";
System.out.println(txt.toUpperCase()); // Outputs "HELLO WORLD"
System.out.println(txt.toLowerCase()); // Outputs "hello world"
String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate")); // Outputs 7
String firstName = "John";
String lastName = "Doe";
System.out.println(firstName + " " + lastName);
String firstName = "John ";
String lastName = "Doe";
System.out.println(firstName.concat(lastName));
String txt = "We are the so-called "Vikings" from the north.";
String txt = "It's alright.";
String txt = "The character \ is called backslash.";
// \n New Line
// \r Carriage Return
// \t Tab
// \b Backspace
// \f Form Feed
String x = "10";
String y = "20";
String z = x + y; // z will be 1020 (a String)
String x = "10";
int y = 20;
String z = x + y; // z will be 1020 (a String)
// Declare String without using new operator
String s = "GeeksforGeeks";
// Prints the String.
System.out.println("String s = " + s);
// Declare String using new operator
String s1 = new String("GeeksforGeeks");
// Prints the String.
System.out.println("String s1 = " + s1);
// StringBuffer: StringBuffer is a peer class of String that provides much of the
// functionality of strings.
// String represents fixed-length, immutable character sequences while
// StringBuffer represents growable and writable character sequences.
StringBuffer s = new StringBuffer("GeeksforGeeks");
byte[] b_arr = {71, 101, 101, 107, 115};
String s_byte =new String(b_arr); //Geeks
byte[] b_arr = {71, 101, 101, 107, 115};
Charset cs = Charset.defaultCharset();
String s_byte_char = new String(b_arr, cs); //Geeks
byte[] b_arr = {71, 101, 101, 107, 115};
String s = new String(b_arr, "US-ASCII"); //Geeks
byte[] b_arr = {71, 101, 101, 107, 115};
String s = new String(b_arr, 1, 3); // eek
byte[] b_arr = {71, 101, 101, 107, 115};
Charset cs = Charset.defaultCharset();
String s = new String(b_arr, 1, 3, cs); // eek
byte[] b_arr = {71, 101, 101, 107, 115};
String s = new String(b_arr, 1, 4, "US-ASCII"); // eeks
char char_arr[] = {'G', 'e', 'e', 'k', 's'};
String s = new String(char_arr); //Geeks
char char_arr[] = {'G', 'e', 'e', 'k', 's'};
String s = new String(char_arr , 1, 3); //eek
int[] uni_code = {71, 101, 101, 107, 115};
String s = new String(uni_code, 1, 3); //eek
StringBuffer s_buffer = new StringBuffer("Geeks");
String s = new String(s_buffer); //Geeks
StringBuilder s_builder = new StringBuilder("Geeks");
String s = new String(s_builder); //Geeks
String s= "GeeksforGeeks";
// or String s= new String ("GeeksforGeeks");
// Returns the number of characters in the String.
System.out.println("String length = " + s.length());
// Returns the character at ith index.
System.out.println("Character at 3rd position = " + s.charAt(3));
// Return the substring from the ith index character
// to end of string
System.out.println("Substring " + s.substring(3));
// Returns the substring from i to j-1 index.
System.out.println("Substring = " + s.substring(2,5));
// Concatenates string2 to the end of string1.
String s1 = "Geeks";
String s2 = "forGeeks";
System.out.println("Concatenated string = " + s1.concat(s2));
// Returns the index within the string
// of the first occurrence of the specified string.
String s4 = "Learn Share Learn";
System.out.println("Index of Share " + s4.indexOf("Share"));
// Returns the index within the string of the
// first occurrence of the specified string,
// starting at the specified index.
System.out.println("Index of a = " + s4.indexOf('a',3));
// Checking equality of Strings
Boolean out = "Geeks".equals("geeks");
System.out.println("Checking Equality " + out);
out = "Geeks".equals("Geeks");
System.out.println("Checking Equality " + out);
out = "Geeks".equalsIgnoreCase("gEeks ");
System.out.println("Checking Equality " + out);
//If ASCII difference is zero then the two strings are similar
int out1 = s1.compareTo(s2);
System.out.println("the difference between ASCII value is="+out1);
// Converting cases
String word1 = "GeeKyMe";
System.out.println("Changing to lower Case " + word1.toLowerCase());
// Converting cases
String word2 = "GeekyME";
System.out.println("Changing to UPPER Case " + word2.toUpperCase());
// Trimming the word
String word4 = " Learn Share Learn ";
System.out.println("Trim the word " + word4.trim());
// Replacing characters
String str1 = "feeksforfeeks";
System.out.println("Original String " + str1);
String str2 = "feeksforfeeks".replace('f' ,'g') ;
System.out.println("Replaced f with g -> " + str2);
}
}
Praktikum 6 :
// Tugas Praktikum 6
public class Prak6 {
public static void main(String[] args) {
String teks = "Program Studi Informatika FMIPA UNS adalah salah satu program studi di Fakultas Matematika dan Ilmu Pengetahuan Alam Universitas Sebelas Maret Surakarta yang berdiri sejak tanggal 29 Januari 2007 dengan nama Program Studi Ilmu Komputer melalui SK Pendirian 163/D/T/2007. Pada tahun 2009 berganti nama menjadi Program Studi Informatika berdasarkan pada surat perpanjangan ijin operasional";
// jumlah karakter (spasi)
// jumlah karakter (!spasi)
// jumlah kata
int jml_char_spc = teks.length();
int jml_char = teks.replace(" ","").length();
// ditambah 1 krn agar pas :)
int jml_kata = jml_char_spc - jml_char + 1;
System.out.format("jumlah karakter (termasuk spasi) : %d%n", jml_char_spc);
System.out.format("jumlah karakter (tidak termasuk spasi) : %d%n", jml_char);
System.out.println("jumlah kata : " + jml_kata);
}
}