, , charAt.
char charAt(int index) , . char. , . , , 0.
5: .
String str = " - ";
int last = str.length()-1;// - 1, 0
char ch = str.charAt(last);
System.out.println(ch);
, , toCharArray.
char[] toCharArray() .
6: ( , , ).
: charAt. , .
String str = "1 000 000 000";
//
char[] chArray = str.toCharArray();
//
for(int i = 0; i<chArray.length; i++){
//
if(chArray[i] == ' '){
//
chArray[i] = '.'; } }
//
System.out.println(chArray);
String substring(int beginIndex, int endIndex) substring(int beginIndex) , . , , . . .
7.
String s = "www.mysite.com";
String name = s.substring(4, s.length()-4);
System.out.println(name); // "mysite"
String domain = s.substring(4);
System.out.println(domain); // "mysite.com"
String[] split(String regex), . . .
8.
String isbn = "978-3-16-148410-0";
String[] isbnParts = isbn.split("-");
System.out.println(" EAN.UCC: " + isbnParts[0]);
System.out.println(" : " + isbnParts[1]);
System.out.println(" : " + isbnParts[2]);
System.out.println(" : " + isbnParts[3]);
System.out.println(" : " + isbnParts[4]);
|
|
boolean contains(CharSequence s) , true false.
9.
String s = "www.mysite.com";
boolean isContain1 = s.contains("mysite");
System.out.println(isContain1);// - true
boolean isContain2 = s.contains("mysite.ru");
System.out.println(isContain2);// - false
boolean endsWith(String suffix) , true false.
10.
String s = "www.mysite.com";
//, "com"
boolean isComEnding = s.endsWith("com");
System.out.println(isComEnding);// true
//, "ru"
boolean isRuEnding = s.contains("ru");
System.out.println(isRuEnding);// false
boolean startsWith(String prefix) startsWith(String prefix, int toffset) , . , .
11.
String s = "www.mysite.com";
//, www
boolean isWWW = s.startsWith("www");
if(isWWW){
/* E , ,
"my". www,
4 */
boolean isNameStarts = s.startsWith("my", 4);
}else{
/* E , ,
"my". www
*/
boolean isNameStarts = s.startsWith("my");
}
int indexOf(int ch), indexOf(int ch, int fromIndex), indexOf(String str), indexOf(String str, int fromIndex) indexOf . , . . . , -1.
12
String data = "name:Igor\nsurname:Kolashnikov\nage:14\ntime:14:55";
//
//
String[]lines=data.split("\n");
//
for (String line: lines){
// ":"
int pos = line.indexOf(":");
//
String attributeName= line.substring(0,pos);
//
String value = line.substring(pos+1,line.length());
// .
System.out.println(attributeName + " - " + value);}
int lastIndexOf(int ch), lastIndexOf(int ch, int fromIndex), lastIndexOf(String str), lastIndexOf(String str, int fromIndex) , .
. , String . . .
|
|
toLowerCase() ;
toUpperCase() ;
trim() ;
13
String str = " ";
//
str = str.trim();
//
System.out.println(str.toLowerCase());
//
System.out.println(str.toUpperCase());
String replace(char oldChar, char newChar), replace(CharSequence target, CharSequence replacement) .
6, replace:
String str = "1 000 000 000";
String newStr = str.replace(" ", ".");
System.out.println(newStr);
boolean equals(Object anObject) . true , .
14
String str = " ";
String str2 = " ";
//
System.out.println(str.equals(str2)); //false
//
//
System.out.println(str.toLowerCase().equals(str2)); // true
int compareTo(String anotherString) , , equals :
, ,
,
,
. . , , .
. ( 15), compareTo , ( ) , ( ). . , , .
15
String name1 = "";
String name2 = "";
System.out.println(name1.compareTo(name2)); //-8
System.out.println(name2.compareTo(name1)); //8
, , , .
System.out.println("".compareTo("")); //32
. , .
int compareToIgnoreCase(String str)
System.out.println("".compareToIgnoreCase("")); //-8
, , .