文章目录
数组操作打印数组内容:Arrays.toString()数组排序:Arrays.sort()数组复制1:Arrays.copyOf()数组复制2:System.arraycopy()两数组是否相等:Arrays.equals()
字符串操作串转化组:str.toCharArray()字符串截取:str.substring()字符获取:str.charAt()是否存在子串:str.contains()字符串分割:str.split()字符串置换:str.replace()串是否由空格组成:str.trim().isEmpty()字符串开头:str.startsWith()
字符操作字符是否为字母:Character.isLetter()字符转整型:str.charAt(i) - '0'字符是否为字母/数字:Character.isLetterOrDigit()
StringBuffer操作String转为StringBufferStringBuffer转为String字符串追加:append()字符串反转:reverse()
数组操作
打印数组内容:Arrays.toString()
int[][] test
= {{1,2,3}, {4,5,6}, {7,8,9}};
for (int i
= 0; i
< test
.length
; i
++) {
System
.out
.println(Arrays
.toString(test
[i
]));
}
toString方法源码
public static String
toString(int[] a
) {
if (a
== null
)
return "null";
int iMax
= a
.length
- 1;
if (iMax
== -1)
return "[]";
StringBuilder b
= new StringBuilder();
b
.append('[');
for (int i
= 0; ; i
++) {
b
.append(a
[i
]);
if (i
== iMax
)
return b
.append(']').toString();
b
.append(", ");
}
}
数组排序:Arrays.sort()
int[] nums
= {4,3,5,6,7};
Arrays
.sort(nums
);
数组复制1:Arrays.copyOf()
int[] arr
= {4,3,5,6,7};
int[] temp
= Arrays
.copyOf(arr
, arr
.length
);
copyOf()方法源码
public static int[] copyOf(int[] original
, int newLength
) {
int[] copy
= new int[newLength
];
System
.arraycopy(original
, 0, copy
, 0,
Math
.min(original
.length
, newLength
));
return copy
;
}
数组复制2:System.arraycopy()
int[] heights
= {1,3,5,3,1};
int[] test
= new int[heights
.length
];
System
.arraycopy(heights
, 0, test
, 0, heights
.length
);
两数组是否相等:Arrays.equals()
Arrays
.equals(chars
, chars1
);
字符串操作
串转化组:str.toCharArray()
String str
= "a-bC-dEf-ghIj";
char[] cs
= Str
.toCharArray();
toCharArray()方法源码
public char[] toCharArray() {
char result
[] = new char[value
.length
];
System
.arraycopy(value
, 0, result
, 0, value
.length
);
return result
;
}
字符串截取:str.substring()
return s
.substring(n
) + s
.substring(0, n
);
字符获取:str.charAt()
str
.charAt(i
) != str
.charAt(j
)
是否存在子串:str.contains()
"abc".contains("ab")
字符串分割:str.split()
String
[] s1
= str
.split(" ")
字符串置换:str.replace()
str
.replace(".", "[.]");
串是否由空格组成:str.trim().isEmpty()
String str
= " ";
if (str
.trim().isEmpty()) {
System
.out
.println("是由空格组成");
}
字符串开头:str.startsWith()
String str
= "ROLE_lwz";
if (str
.startsWith("ROLE_")) {
System
.out
.println("字符串是以ROLE_开头");
}
字符操作
字符是否为字母:Character.isLetter()
for (char c
: str
.toCharArray()) {
if (Character
.isLetter(c
)) {
}
}
字符转整型:str.charAt(i) - ‘0’
num1
.charAt(i
) - '0'
字符是否为字母/数字:Character.isLetterOrDigit()
Character
.isLetterOrDigit(ch
)
StringBuffer操作
String转为StringBuffer
StringBuffer stringBuffer
= new StringBuffer(str
);
StringBuffer转为String
stringBuffer
.toString();
字符串追加:append()
StringBuilder b
= new StringBuilder();
b
.append('[');
字符串反转:reverse()
b
.reverse();