字符串转数组

简介: 字符串转数组

字符串转数组
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);
    }
}
相关文章
|
4月前
字符和数组问题
字符和数组问题
字符串转数组、数组转字符串、给第一个单词色值
字符串转数组、数组转字符串、给第一个单词色值
|
8月前
|
JavaScript 前端开发 Java
数组转字符串( 字符串转数组~
数组转字符串可以使用不同的方法,具体取决于编程语言和上下文环境。以下是一些通用的示例:
|
8月前
数组、字符串、集合的相互转换
数组、字符串、集合的相互转换
36 0
|
9月前
常用的数组(字符串)方法有哪些?(三)
some:判断数组中有没有符合条件的元素,一个符合的都没有返回false,有一个就是true。
|
9月前
|
JavaScript
常用的数组(字符串)方法有哪些?(一)
1.pop:末位删除,即删除数组的最后一项,返回值是被删除项。 2.shift:首位删除,即删除数组的第一项,返回值是被删除项。 3.splice:指定下标删除元素,返回被删除的元素。第一个参数是从下标几开始删除,第二个参数是删除几个,第三个参数是要插入的元素。splice方法是会改变原数组的。删除功能用的比较多,我个人更喜欢用filter来变相实现删除,splice是会改变原数组的,而filter不会
|
9月前
常用的数组(字符串)方法有哪些?(二)
concat:合并数组或者字符串,concat在项目中用的还是比较多的,最经典的就是一个表格数据是有两个或者三个数组组成的时候会用到,watch监听数组和concat结合使用。下期做一个例子。
|
9月前
|
JavaScript 前端开发
数组和字符串的相互转换
1.Array.join()方法 将数组的每一项用指定字符连接形成一个字符串。默认连接字符为 “,” 逗号。 注:将字符串转化为数组的String.split(“分隔符”)与Array.join(“分隔符”)正好相反;
|
10月前
逆序字符串 和 字符串的逆序输出 的区别~
逆序字符串 和 字符串的逆序输出 的区别~
88 0
|
JavaScript
数组与字符串相互转换
js数组与字符串相互转换
116 0