字符串转数组

简介: 字符串转数组

字符串转数组
public static void main(String[] args) {

    String str = new String("abcd");
    char[] ch = str.toCharArray();
    for (int i = 0; i < ch.length; i++) {
        System.out.print(ch[i]+" ");
    }
}

1
2
3
4
5
6
7

  1. 格式化字符串

public static void main(String[] args) {

    String str = String.format("%d-%d-%d",2022,8,31);
    System.out.println(str);
}

1
2
3
4

五、字符串截取与替换

  1. trim()

去掉字符串左右所有的空格会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等

public static void main(String[] args) {

    String str = "  hello    ";
    System.out.println(str.trim());
}

1
2
3
4

  1. subString()

传入一个参数,是截取此位置到结尾.

public static void main(String[] args) {

    String str = "hello world!";
    System.out.println(str.substring(6));
}

1
2
3
4

传入两个参数,是截取指定范围内容

public static void main(String[] args) {

    String str = "woyaojindachang";
    System.out.println(str.substring(2,8));
}

1
2
3
4

3.replace()
使用一个指定的新的字符串替换掉已有的字符串数据,可用的方法如下:

public static void main(String[] args) {

    String str = new String("2022.8.31");
    System.out.println(str.replace('.','-'));
}

1
2
3
4

如果只想替换第一个内容使用replaceFirst()即可

六、字符串拆分
方法 功能
String[] split(String regex) 字符串全部拆分
String[] split(String regex, int limit) 将字符串以指定的格式,拆分为limit组
public static void main(String[] args) {

    String str = "wo yao jin da chang";
    String[] arr = str.split(" ");
    for (String s: arr) {
        System.out.println(s);
    }
}

1
2
3
4
5
6
7

public static void main(String[] args) {

    String str = "wo yao jin da chang";
    String[] arr = str.split(" ",2);
    for (String s: arr) {
        System.out.println(s);
    }
}

1
2
3
4
5
6
7

特殊情况的拆分:
1.如果一个字符串中有多个分隔符,可以用"|"作为连字符.

public static void main(String[] args) {

    String str = "wo&jin=da=chang";
    String[] arr = str.split("=|&");
    for (String s:arr) {
        System.out.println(s);
    }
}

1
2
3
4
5
6
7

  1. 字符"|“,”*“,”+"都得加上转义字符,前面加上 “\”

public static void main(String[] args) {

    String str = "2002.8.31";
    String[] s = str.split("\\.");
    for (String ss:s) {
        System.out.println(ss);
    }
}

1
2
3
4
5
6
7

  1. 如果是 “” ,那么就得写成 “\” .

public static void main(String[] args) {

    String str = "2002\\8\\31";
    String[] s = str.split("\\\\");
    for (String ss:s) {
        System.out.println(ss);
    }
}
相关文章
|
小程序 JavaScript 前端开发
微信小程序-更加简洁的{{ ... }}语法
微信小程序可有意思了
934 0
|
安全 网络协议 Shell
学习干货|实战某次行业攻防应急响应
学习干货|实战某次行业攻防应急响应
370 4
|
存储 安全
【汇编】在代码段使用数据,在代码段使用栈
【汇编】在代码段使用数据,在代码段使用栈
862 0
【汇编】在代码段使用数据,在代码段使用栈
|
定位技术
eharts 中国地图添加城市(散点图实现,含获取城市坐标、图片转base64、自定义散点样式)
eharts 中国地图添加城市(散点图实现,含获取城市坐标、图片转base64、自定义散点样式)
1576 9
|
移动开发 JavaScript HTML5
Vue3视频播放(Video)
这篇文章介绍了如何在Vue 3框架中创建一个视频播放组件(Video),支持自定义视频源、封面、自动播放等多种播放选项和样式设置。
2661 1
Vue3视频播放(Video)
|
存储 Java 编译器
心得经验总结:源代码、目标代码、可执行代码、本地代码的区别
心得经验总结:源代码、目标代码、可执行代码、本地代码的区别
652 0
|
缓存 JavaScript 前端开发
vue3+elementplus后台管理系统,实现用户登录
vue3+elementplus后台管理系统,实现用户登录
|
Ubuntu Python
全网最简约的Vscode配置Anaconda环境(百分百成功)
全网最简约的Vscode配置Anaconda环境(百分百成功)
32227 0
全网最简约的Vscode配置Anaconda环境(百分百成功)
|
JavaScript
vue实现多个el-form表单提交统一校验的2个方法
vue实现多个el-form表单提交统一校验的2个方法
1152 0
|
资源调度 JavaScript 前端开发
vue3实现页面跳转
vue3实现页面跳转
1589 0