1.1、字节与字符串相互转换

    |-字节-->String:public String(byte[]  bytes)

       |-String-->字节: public byte[] getBytes(String charsetName)

范例:字节-->字符串

public class Demo1 {

public static void main(String[] args) {

String str="hello world";

byte []b=str.getBytes();

byte []c={66,67,68,69,70,71};      //定义一个byte字节的数组c

String str1=new String(b);         //将b数组转换为字符串型

String str2=new String(c);         //将c数组转换为字符串型

System.out.println(str1);

System.out.println(str2);

}

}

1.2、判断是否以某字符开头,或结尾

|-以某字符结尾:public boolean endsWith(String suffix)

|-以某字符开头:public boolean startsWith(String prefix)

范例:

public class Demo2 {

public static void main(String[] args) {

String str="Hello world";

System.out.println(str.startsWith("h"));      //如果是以h开头,则返回true

System.out.println(str.endsWith("d"));

}

}

 

1.3、替换操作

|-全部替换public String replaceAll(String regex, String replacement)

public class Demo2 {

public static void main(String[] args) {

String str="Hello world";

System.out.println(str.replaceAll("o", "x"));    //将字符串中的o代替为x

}

}


1.4、替换操作

|-字符串截取public String substring(int beginIndex)

public class Demo2 {

public static void main(String[] args) {

String str="Hello world";

String str1=str.substring(0, 5);       //从0下标开始截取5个字符

System.out.println(str1);

}

}

1.5、拆分操作

|-字符串拆分:public String[] split(String regex)

public class Demo2 {

public static void main(String[] args) {

String str="Hello world";

String[] str1=str.split(" ");      //将字符串按“ ”(空格)拆分为字符串数组

for (String string : str1) {

System.out.print(string+",");   //打印拆分后的字符串数组

}

}

}

1.6、查找操作

      |-public int indexOf(int ch, int fromIndex)、public int indexOf(int ch)
       |-此方法返回int整数型,如果查找到了,则返回位置,没有查找到则返回-1;

public class Demo2 {

public static void main(String[] args) {

String str="Hello world";

System.out.println(str.indexOf("l"));     //查找l的位置,如果string中有“l“,则返回其在string中的位置;没有,则返回-1

}

}


1.7、字符串的其他操作

去掉左右空格:public String trim()

取得字符串长度:public int length()

小写转大写:public String toUpperCase(Locale locale)

大写转小写:public String toLowerCase(Locale locale)

操作练习

         判断邮箱地址是否正确;(是否有“@”及“.”符号)

public class Demo3 {

public static void main(String[] args) {

String str="abc@134.com";

if(str.indexOf("@") == -1 && str.indexOf(".")==-1){

System.out.println("你输入的邮箱不合法");

}else{

System.out.println("合法邮箱");

}

}

}

 

String类使用方法的更多相关文章

  1. 《java入门第一季》之类(String类常见方法小叙)

    String类下面的构造方法和一些常见的方法: /* * 字符串:就是由多个字符组成的一串数据.也可以看成是一个字符数组. * 通过查看API,可以知道 * A:字符串字面值"abc&quo ...

  2. C++中string类的方法

    C++ string类的方法 具体每个方法怎么使用,可以参考相应的链接. 总的链接为http://www.cplusplus.com/reference/string/string/(C++参考文档) ...

  3. string类find_first_not_of ()方法

    string类find_first_not_of ()方法 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xfqxj.blog. ...

  4. String类replaceAll方法正则替换深入分析

    作者网址: https://my.oschina.net/shipley/blog/98973 背景:      前几天有人发了一个关于下面问题的贴,对这个有点好奇,故花时间做了点研究.       ...

  5. Java中String类的方法及说明

    String : 字符串类型 一.      String sc_sub = new String(c,3,2);    //      String sb_copy = new String(sb) ...

  6. JDK6与JDK7中String类subString()方法的区别

    1.subString()方法的作用 subString(int beginIndex, int endIndex)方法的返回的是以beginIndex开始到 endIndex-1结束的某个调用字符串 ...

  7. 关于Integer类中parseInt()和valueOf()方法的区别以及int和String类性的转换.以及String类valueOf()方法

    Integer类中的. 关于parseInt()方法的API文档.  返回的是int类型的 关于valueOf()方法的API文档 返回的是Integer类型的. 关于intValue()方法的API ...

  8. 深入分析Java的String类的方法与特点

    字符串是任何编程语言都必须支持的变量类型,有些编程语言是直接提供了原生的变量类型,有些编程语言则使用语法特性以 SDK 的形式提供支持.在Java编程平台中,对字符串的支持使用了后者的形式,就是通过在 ...

  9. java String类 trim() 方法源码分析

    public String trim() {        int arg0 = this.value.length;   //得到此字符串的长度        int arg1 = 0;   //声 ...

  10. 【转载】Java中String类的方法及说明

    转载自:http://www.cnblogs.com/YSO1983/archive/2009/12/07/1618564.html String : 字符串类型 一.      String sc_ ...

随机推荐

  1. [ActiveX]使用VS2010创建MFC ActiveX工程项目

    ActiveX的基本概念 ActiveX控件可以看作是一个极小的服务器应用程序,它不能队列运行,必须嵌入到某个容器程序中,与该容器一起运行.这个容器包括web网页,应用程序窗体等等. ActiveX控 ...

  2. RobHess的SIFT源码分析:综述

    最初的目的是想做全景图像拼接,一开始找了OpenCV中自带的全景拼接的样例,用的是Stitcher类,可以很方便的实现全景拼接,而且效果很好,但是不利于做深入研究. 使用OpenCV中自带的Stitc ...

  3. NodeJS Stream 二:什么是 Stream

    对于大部分有后端经验的的同学来说 Stream 对象是个再合理而常见的对象,但对于前端同学 Stream 并不是那么理所当然,github 上甚至有一篇 9000 多 Star 的文章介绍到底什么是 ...

  4. 关于C/C++中的“auto”

    C/C++ 98标准 C++03标准 意思完全一样:auto被解释为一个自动存储变量的关键字,也就是申明一块临时的变量内存.auto的出现意味着,当前变量的作用域为当前函数或代码段的局部变量,意味着当 ...

  5. Git学习笔记(一)创建版本库并添加文件

    最近从廖雪峰老师的个人网站上学习git,做点笔记. ★★★★★ 先注册自己的username和email,否则会报如下错误: 注册:git config --global user.name &quo ...

  6. 2017年IT互联网圈跑会指南~

    啦啦啦~要放假啦,还有十多天就要过年啦,要走亲访友啦!相信大家也是各种胡吃海喝后,啊咧~腰上好像多了好几圈o(>﹏<)o为了让小伙伴们及时制定年后行程(减膘)计划,活动家特此奉上2017年 ...

  7. 抓包分析SIP消息

    I. 实验环境 SIP服务器: Yate服务器, IP:port=10.15.3.34:5060 SIP客户端: 移植Sipdroid的Android网络电话软件, IP: 10.15.3.73, A ...

  8. 2. SpringMVC 上传文件操作

    1.创建java web项目:SpringMVCUploadDownFile 2.在项目的WebRoot下的WEB-INF的lib包下添加如下jar文件 com.springsource.com.mc ...

  9. 实战ASP.NET访问共享文件夹(含详细操作步骤)

    博客园找找看(http://zzk.cnblogs.com)的索引文件占用空间太大,需要移至另外一台服务器,所以要解决"在ASP.NET中通过共享文件夹访问索引文件"的问题. 假设 ...

  10. NullSafe 的原理

    摘要 NullSafe is a simple category on NSNull that returns nil for unrecognised messages instead of thr ...