java常见的字符串操作方法示例

发布时间:2020-11-12 15:03:09 作者:小新
来源:亿速云 阅读:95

小编给大家分享一下java常见的字符串操作方法示例,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

一、使用length()方法获取字符串的长度

public class  maintest {
    public static void main(String[] args) {
        String str = "abcdefg";
        //length():统计当前字符串的字符个数
        int i = str.length();
        System.out.println(i);
        }
    }

二、使用indexOf()方法查找指定字符再字符串中的位置

public class  maintest {
    public static void main(String[] args) {
        String str = "abcdefg";
        //indexOf():查找指定字符再字符串中的位置
        int index = str.indexOf("a");
        System.out.println(index);
        }
      }

三、使用toUpperCase()方法将字符串中的字符变为了大写形式

public class  maintest {
    public static void main(String[] args) {
        String str = "abcdefg";
        //小写转大写
        //toUpperCase():将字符串中的字符变为了大写形式
        String str = str.toUpperCase();
        System.out.println(str);
        }
       }

四、使用toLowerCase()方法将字符串中的字符变为小写

public class  maintest {
    public static void main(String[] args) {
        //toLowerCase():将字符串中的字符变为小写
        String str = "WWMMDDHH";
        String str1 = str3.toLowerCase();
        System.out.println(str);
       }
     }

五、使用substring()方法截取字符串

public class  maintest {
    public static void main(String[] args) {
        String str = "abcdefg";
        //substring:截取字符串
        String str = "abcdefg";
        String str = str5.substring(0, 3);
        System.out.println(str);
        String str = str5.substring(3);
        System.out.println(str);
      }
    }

六、使用replaceAll()方法替换当前字符串中指定内容

public class  maintest {
    public static void main(String[] args) {
        String str = "abcdefg";
        String str = str5.replaceAll("abc", "xyz");
        System.out.println(str);
       }
     }

七、使用trim()方法能够去掉当前字符串中两端的空格

public class  maintest {
    public static void main(String[] args) {
        String str = " abc def ";
        System.out.println(str.length());
        String str1 = str9.trim();
        System.out.println(str);
        System.out.println(str1);
    }
 }

八、字符串+字符串  等于拼接

public class  maintest {
    public static void main(String[] args) {
        String str1 = "123";
        String str2 = "100";
        System.out.println(str1+str2);
    }
  }

九、将字符串变为整数

public class  maintest {
    public static void main(String[] args) {
        String str1 = "123";
        String str2 = "100";
        int num1 = Integer.parseInt(str1);
        int num2 = Integer.parseInt(str2);
        System.out.println(num1+num2);
    }
  }

十、使用charAt()找到指定字符串中位置的字符

public class  maintest {
    public static void main(String[] args) {
        String str1000 = "abcde";
        //charAt:找到指定字符串中位置的字符
        char char = str1000.charAt(2);
        System.out.println(char);
    }
   }

以上是java常见的字符串操作方法示例的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. javascript常见数组及操作方法
  2. bootstrap中table常见的操作方法有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java 字符串

上一篇:MySQL数据库中的数据目录如何修改

下一篇:使用Django如何实现自定义YamlField

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》