(1)int length():返回字符串的长度,例如:

String s1="hello";
System.out.println(s1.length());//显示为5

(2)char charAt(int index):获取字符串中指定位置的字符,index的取值范围是0~字符串长度-1,例如:

String s1="hello world",
System.out.println(s1.charAt(6));//显示结果为w

(3)int compareTo(String another):按Unicode码值逐字符比较两个字符串的大小。如果源串较小就返回一个小于0的值;两串相等返回0;否则返回大于0的值,例如:

System.out.println("hello".compareTo("Hello"));//显示-4
System.out.println("hello".compareTo("hello"));//显示0
System.out.println("Hello".compareTo("hello"));//显示1

(4)String concat(Striing str):把字符串str附加在当前字符串末尾,例如:

String str ="hello";
String str2="world";
System.out.println(str);//显示hello
System.out.println(Str2);//显示helloworld

(5)equals(Object obj)和equalsIgnoreCase(String str):判断两个字符串对象的内容是否相同。两者区别在于,equals()方法区分字母大小写,equalsIgnoreCase()不区分,例如:

String str1="hello";
String str2="Hello"
System.out.println(str1.equals(str2));//显示true
System.out.println(str1.equalsIgnoreCase(str2));//显示false

(6)int indexOf(int ch):返回指定字符ch在此字符串中第一次出现的位置。

int indexOf(String str):返回指定字符串str在此字符串中第一次出现的位置。

int lastIndexOf(int ch):返回指定字符ch在此字符串中最后一次出现的位置。

int lastIndexOf(String str):返回指定子字符串str在此字符串中最后一次出现的位置。

String str1="hello world";
System.out.pritnln(s1.indexOf("l"));//显示2
System.out.println(s1.indexOf("world"));//显示6
System.out.println(s1.lastIndexOf("l"));//显示9
System.out.println(s1.lastIndexOf("world"));//显示6

(7)String toUpperCase():将字符串转换为大写。

String toLowCase():将字符串转换成小写。例如:

String s1="Welcome to Java world";
System.out.println(s1.toUpperCase());//显示WELCOME TO JAVA WORLD
System.out.println(s1.toLowerCase());//显示welcome to java world

(8)String substring(int beginIndex):返回一个新字符串,改子字符串从指定索引处的字符开始,直到此字符串末尾。

String substring(int beginIndex,int endIndex):返回一个新字符串,改子字符串从指定的beginIndex处开始,直到索引endIndex-1处的字符。例如:

String s1="Welcome to Java world";
System.out.println(s4.substring(11));//显示Java world
System.out.println(s1.substring(11,15));//显示Java

(9)static String valueOf():把基本数据类型转换为String类型,例如:

int i=123;
String s1=String.valueOf(i);
System.out.println(s1);//显示字符串123

(10)String[] split(String regex):将一个字符串按照指定的分隔符分隔,返回分隔符后的字符串数组。

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

  1. Delphi中TStringList类常用属性方法详解

    TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 先把要讨论的几个属性列出来: 1.CommaText 2.Delim ...

  2. File类常用的方法与字节流类方法简介

    File类常用的方法 获取功能的方法 public String getAbsolutePath() :返回此File的绝对路径名字符串. public String getPath() :将此Fil ...

  3. Java之String类常用API

    目录 Java之String类常用API char chatAt(int index) int length() char[] toCharArray() String(char value[]) S ...

  4. String类常用的API

    String类常用的API 字符串内容的比较: 注意: 不能使用 == 去比较两个字符串的内容.原理:比较的是字符串的地址. (如果两个字符串都是使用""进行赋值,那么他们都是放在 ...

  5. 关于JAVA的String类的一些方法

    一.得到字符串对象的有关信息 1.通过调用length()方法得到String的长度. String str=”This is a String”; int len =str.length(); 2. ...

  6. C++ string 类的 find 方法实例详解

    1.C++ 中 string 类的 find 方法列表 size_type std::basic_string::find(const basic_string &__str, size_ty ...

  7. java.lang.String 类的所有方法

    java.lang.String 类的所有方法 方法摘要 char charAt(int index) 返回指定索引处的 char 值. int codePointAt(int index) 返回指定 ...

  8. Java中String类的format方法使用总结

    可参考: http://www.cnblogs.com/fsjohnhuang/p/4094777.html http://kgd1120.iteye.com/blog/1293633 String类 ...

  9. String类的split方法以及StringTokenizer

    split方法可以根据指定的表达式regex将一个字符串分割成一个子字符串数组. 它的参数有两种形式,也即:split(String regex)和split(String regex, int li ...

随机推荐

  1. java.lang.NoSuchMethodError: org.apache.poi.ss.usermodel.CellStyle.setVerticalAlignment(Lorg/apache/poi/ss/usermodel/VerticalAlignment;)V

    项目里引入了两个不同的 POI 版本 ,可能是版本冲突引起的. 但是奇怪的是 用Eclipse在本地就失败,在公共测试 环境就是OK的,同事用的 edea 编译器也是OK的. Caused by: j ...

  2. 【week1错题集】

    day9[2.f] # day9 题2.f ''' 有如下文件,t1.txt,里面的内容为: 葫芦娃,葫芦娃, 一根藤上七个瓜 风吹雨打,都不怕, 啦啦啦啦. 以r模式打开文件,从‘风吹雨打..... ...

  3. Python基础-异常

    异常捕获 常见异常类型 Exception:顶级异常类,大部分异常类都是它的子类.SyntaxError:语法错误TypeError:类型错误ValueError:值错误NameError:找不到名称 ...

  4. js原型、原型链

    之前有说过继承,在js中没有类,所以在new的后面,放的是构造函数,在构造函数中有一个属性prototype,js的继承全靠它. 在js中对象的类型有很多,常见的就是普通对象,和函数对象,在对象中都会 ...

  5. 阿里云OSS服务器的使用

    关于文件上传,我们一般使用OSS服务器.大致为两种上传方式: 详情官网参考:https://help.aliyun.com/document_detail/31927.html?spm=a2c4g.1 ...

  6. python自带函数

    callable() #是否可以被执行,是否可以被调用 chr() #返回整数i对应的ASCII字符.与ord()作用相反.参数x:取值范围[0, 255]之间的正数. ord() #参数是一个asc ...

  7. 看完这篇,再也不怕被问到 AsyncTask 的原理了

    本文很多资料基于Google Developer官方对AsyncTask的最新介绍. AsyncTask 是什么 AsyncTask is designed to be a helper class ...

  8. three.js 制作一个三维的推箱子游戏

    今天郭先生发现大家更喜欢看我发的three.js小作品,今天我就发一个3d版本推箱子的游戏,其实webGL有很多框架,three.js并不合适做游戏引擎,但是可以尝试一些小游戏.在线案例请点击博客原文 ...

  9. excel文件双击打开空白

    excel文件双击打开之后进入软件,没有去读文件 一.现象描述 打开现象如下所示,只有excel模板,看不到excel中的表格模板. 二.想要打开文件 (1)在软件的文件--->打开--> ...

  10. 学习python的几个资料网站

    菜鸟教程 https://www.runoob.com/python3/python3-tutorial.html https://www.runoob.com/python/python-tutor ...