-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstring_class.java
executable file
·156 lines (146 loc) · 5.31 KB
/
string_class.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Main.java
*
* Created on 1 ÷åðâíÿ 2007, 15:45
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package javaapplication11;
/**
*
* @author Òåõíèê
*/
class temp_box{
private String s;
temp_box(){
this.s="temp_box";
}
temp_box(String s){
this.s=s;
}
public String toString(){
return this.s;
}
static void clear_array(char[] array_of_chars){
for(int i=0;i<array_of_chars.length;i++){
array_of_chars[i]=' ';
}
}
static void print_bytes(byte[] array_of_bytes){
for(int i=0;i<array_of_bytes.length;i++){
System.out.print(i+" - "+array_of_bytes[i]+"; ");
}
System.out.println();
}
}
public class Main {
public Main() {
}
public static void main(String[] args) {
// TODO code application logic here
System.out.println("<<<Begin");
// Êîíñòðóêòîð
char chars[]={'a','b','c','d','e','f','g','h'};
String s=new String(chars);// toCharArray()
System.out.println("êîíñòðóêòîð:"+s);
String s2=new String("temp string");
System.out.println("êîíñòðóêòîð:"+s2);
String s3=new String(chars,3,chars.length-3);
System.out.println("êîíñòðóêòîð:"+s3);
// Concat - îáúåäèíèòü ñòðîêó
s3=s+s2;
System.out.println(s3);
// ñòàíäàðòíûé âûçîâ ìåòîäà toString()
temp_box box=new temp_box("method toString of <object> of <class temp_box>");
System.out.println("toString():"+box);
// ìåòîä charAt()
char ch="abc".charAt(1);
System.out.println("charAt():"+ch);
// î÷èñòêà ìàññèâà char[]
temp_box.clear_array(chars);
String s4=new String(chars);
System.out.println("Clear_chars:"+s4);
// ìåòîä getChars();
char[] new_char={'1','2','3','4','5','6','7','8'};
s2.getChars(1,3,new_char,0);
String s5=new String(new_char);
System.out.println("getChars():"+s5);
char[] new_char_2=s5.toCharArray();
System.out.println("toCharArray():"+(new String(new_char_2)));
// ìåòîä getBytes();
byte[] array_of_byte;
array_of_byte=s5.getBytes();
System.out.print("getBytes():");temp_box.print_bytes(array_of_byte);
// ìåòîä equals();
String s10=new String("hello");
String s11=new String("hello");
String s12=new String("HELLO");
if(s10.equals(s11)){
System.out.println(s10+" equals "+s11);
}
else {
System.out.println(s10+" NOT equals "+s11);
};
if(s10.equals(s12)){
System.out.println(s10+" equals "+s12);
}
else {
System.out.println(s10+" NOT equals "+s12);
};
// ìåòîä equalsIgnoreCase();
if(s10.equalsIgnoreCase(s12)){
System.out.println(s10+" equalsIgnoreCase "+s12);
}
else {
System.out.println(s10+" NOT equalsIgnoreCase "+s12);
}
// ìåòîäà regionMatches -
if(s10.regionMatches(true,1,s12,1,2)){// <IgnoreCase>,<begin> find in source string, <source> find string, substr find string <begin> <end>
System.out.println("Matches");
}
else {
System.out.println(" Not matches ");
}
// ìåòîä startWith()
if(s10.startsWith("he")){
System.out.println(s10+" startsWith "+"he");
}
else {
System.out.println(s10+" NOT startsWith "+"he");
};
// ìåòîäà endWith()
if(s10.endsWith("llo")){
System.out.println(s10+" endsWith "+"llo");
}
else {
System.out.println(s10+" NOT endsWith "+"llo");
}
// ìåòîä compareTo()
System.out.println(s10+" compareTo() "+s11+" "+(s10.compareTo(s11)));
System.out.println(s10+" compareTo() "+s12+" "+(s10.compareTo(s12)));
// ìåòîä indexOf()
System.out.println("indexOf():"+s10.indexOf("l"));
System.out.println("indexOf():"+s10.indexOf("z"));
// ìåòîä lastIndexOf()
System.out.println("lastIndexOf():"+s10.lastIndexOf("l"));
System.out.println("lastIndexOf():"+s10.lastIndexOf("z"));
// ìåòîä substring()
System.out.println("substring():"+s10.substring(2));
System.out.println("substring():"+s10.substring(1,5));
System.out.println("<<<End");
// ìåòîä concat()
System.out.println("concat():"+s10.concat(s12));
// ìåòîä raplace()
System.out.println("replace():"+s10.replace("l","L"));
// ìåòîä trim()
System.out.println("trim():"+ (new String(" trim string ")).trim() );
// static ìåòîä valueOf() - àíàëîã âûçîâà ôóíêöèè toString() äëÿ êàæäîãî îáúåêòà, èëè ïðîñòîãî òèïà
System.out.println("valueOf():"+String.valueOf(box));
System.out.println("valueOf():"+String.valueOf(s10));
// ìåòîä toLowerCase()
System.out.println("toLowerCase():"+s12+"=>"+s12.toLowerCase());
// ìåòîä toUpperCase()
System.out.println("toUpperCase():"+s10+"=>"+s10.toUpperCase());
}
}