Java . C++ , . , , , . java.lang , , . , String, , . , , . StringBuffer , .
String, StringBuffer final, , . , .
, String new. :
String s = new String():
s String , .
char chars[] = { '', 'b', '' }:
String s = new String(chars);
System.out.println(s):
abc. , 3 :
String(char chars[], int , int );
:
char chars[] = { 'a', 'b', '', 'd', 'e', 'f' }:
String s = new String(chars,2,3);
System.out.println(s);
cde.
Java , . String .
Java , . , char.
String s = "abc";
System.out.println(s);
, String length, . 3, 3 .
String s = "abc";
System.out.println(s.length);
Java , - String, -, . 3.
System.out.println("abc".Length());
String s = is + age + " years old.";
+ , , , , :
String s = new StringBuffer("He is ").append(age);
s.append(" years old.").toString();
String . , . . Java , , String, StringBuffer.
. String, , StringBuffer? . , String Java , .
:
String s = " is " + age + " years old.";
, age String, , , int, . int append StringBuffer, . , , . :
String s = "four: " + 2 + 2;
, , s four: 4? . "four: 22".
, , :
String s = "four: " + (2 + 2);
String toString , , Object. toStrring , .
class Point {
int , ;
Point(int x, int ) {
this.x = ;
this. = ;
}
public String toString() {
return "Point[" + x + ", " + + "]";
} }
class toStringDemo {
public static void main(String args[]) {
Point p = new Point(10, 20);
System.out.println("p = " + p);
} }
, .
, , charAt. , getChars. , String.
class getCharsDemo {
public static void main(String args[]) {
String s = "This is a demo of the getChars method.";
int start = 10;
int end = 14;
char buf[] = new char[end - start];
s.getChars(start, end, buf, 0);
System.out.println(buf);
} }
getChars end. 4 .
demo
String toCharArray, char . byte, 16- . getBytes, , getChars, byte.
, , equals String. equalsIgnoreCase, . , :
class equalDemo {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = "Good-bye";
String s4 = "HELLO";
System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2));
System.out.println(s1 + " equals " + s3 + " -> " + s1.equals(s3));
System.out.println(s1 + " equals " + s4 + " -> " + s1.equals(s4));
System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " +
s1.equalsIgnoreCase(s4));
} }
:
:\> java equalsDemo
Hello equals Hello -> true
Hello equals Good-bye -> false
Hello equals HELLO -> false
Hello equalsIgnoreCase HELLO -> true
String , equals. regionMatches -. startsWith , , . endsWith .
equals == . equal , == - , . , , , , equals == .
class EqualsNotEqualTo {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = new String(s1);
System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2));
System.out.println(s1 + " == " + s2 + ", -> " + (s1 == s2));
} }
:
, . , , , . compareTo String. , , , , , -, . compareTo 0, . , , compareTo. .
class SortString {
static String arr[] = {"Now", "is", "the", "time", "for", "all",
"good", "men", "to", "come", "to", "the",
"aid", "of", "their", "country" };
public static void main(String args[]) {
for (int j = 0; i < arr.length; j++) {
for (int i = j + 1; i < arr.length; i++) {
if (arr[i].compareTo(arr[j]) < 0) {
String t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
System.out.println(arr[j]);
}
} }
indexOf lastIndexOf
String , indexOf lastIndexOf. , , . , -1. , .
class indexOfDemo {
public static void main(String args[]) {
String s = "Now is the time for all good men " +
"to come to the aid of their country " +
"and pay their due taxes.";
System.out.println(s);
System.out.println("indexOf(t) = " + s.indexOf('f));
System.out.println("lastlndexOf(t) = " + s.lastlndexOf('f));
System.out.println("indexOf(the) = " + s.indexOf("the"));
System.out.println("lastlndexOf(the) = " + s.lastlndexOf("the"));
System.out.println("indexOf(t, 10) = " + s.indexOf('f , 10));
System.out.println("lastlndexOf(t, 50) = " + s.lastlndexOf('f , 50));
System.out.println("indexOf(the, 10) = " + s.indexOf("the", 10));
System.out.println("lastlndexOf(the, 50) = " + s.lastlndexOf("the", 50));
} }
. , .
Now is the time for all good men to come to the aid of their country
and pay their due taxes.
indexOf(t) = 7
lastlndexOf(t) = 87
indexOf(the) = 7
lastlndexOf(the) = 77
index0f(t, 10) = 11
lastlndex0f(t, 50) = 44
index0f(the, 10) = 44
lastlndex0f(the, 50) = 44
String , , , StringBuffer, String, , .
substring
String, substring. , . , . , , , ( ) , .
"Hello World".substring(6) -> "World"
"Hello World".substring(3,8) -> "lo Wo"
concat
, concat. String, , .
replace
replace . , , .
toLowerCase toUpperCase
, .
"Hello".toLowerCase() -> "hello"
"Hello".toUpperCase() -> "HELLO"
trim
, , trim .
valueOf
- , . valueOf. Java ( , ). .
StringBuffer
StringBuffer String, , . String , . StringBuffer , . Java , String, +. Java StringBuffer .
StringBuffer , 16 . , . , , , 16 . StringBuffer , length, , StringBuffer capacity. , :
class StringBufferDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello");
System.out.println("buffer = " + sb);
System.out.println("length = " + sb.length());
System.out. println("capacity = " + sb.capacity());
} }
, , String-Buffer .
buffer = Hello
length = 5
capacity = 21
ensureCapacity
StringBuffer , ensureCapacity. , , .
setLength
, setLength. , , , . setCharDemo sstLength .
charAt setCharAt
StringBuffer charAt. setCharAt . :
class setCharAtDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello");
System.out.println("buffer before = " + sb);
System.out.println("charAt(1) before = " + sb.charAt(1));
sb.setCharAt(1, 'i');
sb.setLength(2);
System.out.println("buffer after = " + sb);
System.out.println("charAt(1) after = " + sb.charAt(1));
} }
, .
buffer before = Hello
charAt(1) before = e
buffer after = Hi
charAt(1) after = i
append
append StringBuffer + . String.valueOf StringBuffer. append StringBuffer, . , .
class appendDemo {
public static void main(String args[]) {
String s;
int a = 42;
StringBuffer sb = new StringBuffer(40);
s = sb.append("a = ").append(a).append("!").toString();
System.out.println(s);
} }
:
:\> Java appendDemo
= 42!
insert
insert append , . , append, , String.valueOf, StringBuffer, , . "there" "hello" "world!".
class insertDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("hello world !");
sb.insert(6,"there ");
System.out.println(sb);
} }
:
:\> java insertDemo
hello there world!
Java String StringBuffer. , , URL . Java (Unicode ). , Java , ASCII, , char.
10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _. 10.11.2021 - 12:36: - Conscience -> . ? - _. 10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _. 10.11.2021 - 12:35: - Ecology -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> . - _. 10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _. 10.11.2021 - 09:18: - New Technologies -> , 5G- - _. 10.11.2021 - 09:18: - Ecology -> - _. 10.11.2021 - 09:16: - Ecology -> - _. 10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _. 10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _. |