-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathStringMethods2.java
More file actions
65 lines (51 loc) · 1.93 KB
/
StringMethods2.java
File metadata and controls
65 lines (51 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.Arrays;
public class StringMethods2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String a = "Hello";
char x = a.charAt(0); // Index
System.out.println("Single Char Get "+x); // a[0]
String b = " Hello How are You ";
//trim - it remove the leading and trailing spaces
// it not remove the in between spaces
System.out.println("Before Trim ["+b+"]");
b= b.trim();
System.out.println("After Trim ["+b+"]");
System.out.println("SubString is "+a.substring(2)); // it will give the string from 2nd index
// substring is overloaded
// 2 is index and 4 is position
System.out.println("Now Again SubString "+a.substring(2, 5));
// index always start with 0
// position start with 1
int compare = "Amit".compareTo("Amit");
System.out.println("Compare "+compare);
a= a.concat("How"); // Memory Allocate for Hello and for How and for Hello + How
// + vs concat
a= a.concat("How").concat("Are").concat("You");
a = a +"How"+"Are"+"You";
//a.startsWith("Mr");
if(a.endsWith("You")){
System.out.println("Yes ");
}
else
{
System.out.println("No");
}
// String convert into Byte , so u can write in a file
// also u can transfer the bytes on network
byte barray [] = a.getBytes();
a = "Hello How Are You".toLowerCase();
char w[]= a.toCharArray(); // Convert String into Char Array
Arrays.sort(w);
// Arrays class is used to sort the primitive arrays
String t = new String(w); //convert char array into String
System.out.println("After Sort "+t);
System.out.println("After Replace "+a.replace('e', 'i'));
String h = "Hello How are You";
String array [] = h.split(" ");
System.out.println("Word Count is "+array.length);
String productCode = "FKOD-1001";
String newProductCode = productCode.split("-")[0]+"-" +(Integer.parseInt(productCode.split("-")[1])+1) ;
System.out.println("New Code is "+newProductCode);
}
}