1 Star 1 Fork 0

RainbowSea/Java语言

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Blog12.java 5.60 KB
一键复制 编辑 原始数据 按行查看 历史
RainbowSea 提交于 2022-07-19 22:06 +08:00 . 有关字符串方法的进一步探究
package Blog1;
import java.util.Scanner;
public class Blog12 {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("a");
for (int i = 0; i < 10; i++) {
stringBuilder.append("a");
}
System.out.println(stringBuilder.append("a"));
}
public static void main16(String[] args) {
String str = "";
for (int i = 0; i < 10; i++) {
str += "a";
}
System.out.println(str);
}
public static void main15(String[] args) {
String str = "abcdefg";
System.out.println(str);
String ret = dans(str,3);
System.out.println(ret);
}
public static String dans(String str, int n) {
if(null == str || n <= 0 || n >= str.length()) {
return null;
}
str = reverse(str,0,n-1);
str = reverse(str,n,str.length()-1);
str = reverse(str,0,str.length()-1);
return str;
}
public static String reverse(String str, int begin, int end) {
char[] value = str.toCharArray(); // 将字符串转为字节char[]数组,
// 一个一个字符,取出后替换,
while(begin < end) {
char tmp = value[begin];
value[begin] = value[end];
value[end] = tmp;
begin++;
end--;
}
return new String(value); // 字符数组构造方法,创建字符串
/* // 或者使用 String.copyValueOf() 将数组转为字符串返回
return String.copyValueOf(value);
*/
}
public static void main14(String[] args) {
String str = "hello world";
int[] arrays = {1,2,3};
System.out.println(str.length());
System.out.println(arrays.length);
}
public static void main13(String[] args) {
String str = "hello";
String str2 = "";
System.out.println(str.isEmpty());
System.out.println(str2.isEmpty());
}
public static void main12(String[] args) {
String str = "aaaAAA你好世界!!!";
String ret = str.toLowerCase(); // 大写转小写
System.out.println(ret);
String ret2 = str.toUpperCase(); // 小写转大写
System.out.println(ret2);
}
public static String func(String str) {
String[] strings = str.split(" ");
String tmp = "";
for (String s:strings) {
tmp += s;
}
return tmp;
}
public static void main11(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
String str = scanner.nextLine();
// 不可以使用next(),因为读取到空格就结束了,
// nextLine(), 不可以于 nextInt()连接使用,nextLine()会读取到回车
String ret = func(str);
System.out.println(ret);
}
}
public static void main10(String[] args) {
String str = "Java-split#18";
String[] strings = str.split("-"); // 第一个拆分符号
for (String s:strings) {
String[] strings1 = s.split("#"); //# 第二个拆分符号,不是特殊符号,不用 \\
for (String tmp: strings1){
System.out.println(tmp);
}
}
}
public static void main9(String[] args) {
String str = "192\\168\\1\\1";
String[] strings = str.split("\\\\");
for (String s:strings) {
System.out.println(s);
}
}
// 以空格为分隔标准
public static void main8(String[] args) {
String str = "hello world !!!";
String[] strings = str.split("AA");
for (String s: strings) {
System.out.println(s);
}
}
public static void main7(String[] args) {
String str = "helloworld";
boolean flg = str.endsWith("world");
boolean flg2 = str.endsWith("hello");
System.out.println(flg);
System.out.println(flg2);
}
public static void main6(String[] args) {
String str = "helloworld";
boolean flg = str.startsWith("hello",0);
boolean flg2 = str.startsWith("hello",6);
System.out.println(flg);
System.out.println(flg2);
}
public static void main5(String[] args) {
String str = "helloworld";
boolean flg = str.startsWith("hello");
boolean flg2 = str.startsWith("world");
System.out.println(flg);
System.out.println(flg2);
}
public static void main4(String[] args) {
String str = "helloworld";
int flg = str.lastIndexOf("world");
int flg2 = str.lastIndexOf("hi");
System.out.println(flg);
System.out.println(flg2);
int flg3 = str.lastIndexOf("world",9); // 从后向前,指定位置开始查找
System.out.println(flg3);
}
public static void main3(String[] args) {
String str = "helloworld";
int flg = str.indexOf("world",3);
int flg2 = str.indexOf("hi",0);
System.out.println(flg);
System.out.println(flg2);
}
public static void main2(String[] args) {
String str = "helloworld";
int flg = str.indexOf("world");
int flg2 = str.indexOf("hi");
System.out.println(flg);
System.out.println(flg2);
}
public static void main1(String[] args) {
String str = "hello world";
boolean flg = str.contains("hello");
System.out.println(flg);
boolean flg2 = str.contains("Hi");
System.out.println(flg2);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Rainbow--Sea/java-language.git
git@gitee.com:Rainbow--Sea/java-language.git
Rainbow--Sea
java-language
Java语言
master

搜索帮助