String

Nannan Lv5

String

charAt

返回指定索引的字符:

1
2
String a = new String("Hello world\n");
char c = a.charAt(4);

compareTo

与其他字符串比较

  • 相同为0
  • 用大比小:正
  • 用小比大:负
1
2
3
4
5
6
7
8
String a = new String("hello");
String b = new String("helle");

int x = a.compareTo(b); // o - e
int y = b.compareTo(a); // e - o
int z = a.compareTo(a); // o - o
cout.write(x+" "+y+" "+z+"\n");
cout.close();

输出:

1
10 -10 0

compareTolgnoreCase

与其他字符串比较,忽略大小写

concat

返回字符串拼接值

1
2
3
4
5
String a = new String ("hello ");
String b = new String ("world");

String c = a.concat(b); // hello world

length

返回字符串长度

1
2
3
String a = new String ("hello world");
int t = a.length(); // 11

  • Title: String
  • Author: Nannan
  • Created at : 2024-03-11 20:27:00
  • Updated at : 2024-09-30 20:58:33
  • Link: https://redefine.ohevan.com/2024/03/11/七、String/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments