Substring function to retrieve specific words from a string.
remember Substring count from zero
Based on the data of primitive data types can be grouped into four:
1. Integer data type or integers include: byte, short, int, long
2. Data type float or real numbers include: float and double
3. Char data type or character that is: char
4. Boolean data type: boolean
=====================================
// file HitungNilai.java
class Bilangan {
public float Nlai(int a, int b) {
float c = 0;
c = (a + b) / 2;
if (c < 0) {
c = 0;
}
return c;
}
}
public class HitungNilai {
public static void main(String[] args) {
String Hasil = "";
Hasil = Tampil();
System.out.println(Hasil);
}
public static String Tampil() {
string tmp = "";
float n = 0;
Bilangan mBil = new Bilangan();
n = mBil.Nila(5, 3);
if (n > 0) {
tmp = "Nlai Positif yaitu : " + n;
} else if (n < 0) {
tmp = "Nilai Negatif atau Nol : " + n;
}
return tmp;
}
}
==========================================
Example program using substring:
public class Substring {
public static void main (String Args [] ){
String data = "Mencoba String Pada Java";
String sub = data.substring(8);
String sub2 =data.substring(8,14);
System.out.println(data);
System.out.println(sub);
System.out.print(sub2);
}
}
Once the program is on the run, the result is as follows:
Trying String In Java
String In Java
string
========================================
jadi :
M = 0
e = 1
n = 2
c = 3
o = 4
b = 5
a = 6
(spasi)= 7
S = 8
t = 9
r = 10
i = 11
n = 12
g = 13
(spasi)= 14
P = 15
a = 16
d = 17
a = 18
(spasi)= 19
J = 20
a = 21
v = 22
a = 23
*) String sub = data.substring (8);
begin with the letter S to 8 and so on until the last letter ..
result: String In Java
*) String Sub2 = data.substring (8.14);
begin with the letter S to the 8 to the 13 letters (letters before 14) ..
result: String
Related Post:
Title : Regarding about the substring and its function in java language
Description : Substring function to retrieve specific words from a string. remember Substring count from zero Based on the data of primitive data typ...