继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java基础之String类主要方法

Whaleson
关注TA
已关注
手记 4
粉丝 1
获赞 199

API:application program interface(应用程序接口)
特点:public final class String
字段也叫类的成员变量(也叫属性)
String不能继承,因为有final关键字。

char c='a';//单引号(一个字符用两个字节存储)
char  0=48;A=65;a=97;
String s1="abc";//(abc为String s 的对象)双引号直接引用的叫做String类的常量对象(在堆里的常量区(放字符串常量或者静态的东西));
//s1为引用;
String s2="abc";//常量对象在常量区寻找,如果值存在直接引用,不存在再创建。

String的构造:
String s3=new String("abc");
现在常量区创建值,然后赋值引用给s3;(有两个对象)
【方法】
endsWith(String suffix) boolean类型
测试此字符串是否以指定的后缀结束

charAt(int index) char类型
返回指定索引处的 char 值。
startsWith(String suffix) boolean类型
测试此字符串是否以指定的后缀结束。
equals(Object anObject) boolean类型
将此字符串与指定的对象比较(比较的是内容)
equalsIgnoreCase(String anotherString) boolean类型
将此 String 与另一个 String 比较,不考虑大小写。
indexOf(int ch) int类型
返回指定字符在此字符串中第一次出现处的索引。(不存在时返回-1)
indexOf(String str) int类型
返回指定子字符串在此字符串中第一次出现处的索引。
indexOf(String str, int fromIndex) int类型
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
isEmpty() boolean类型
当且仅当 length() 为 0 时返回 true。
String s=null;只有引用没对象,所以点不出来;NullPointerException
int lastIndexOf(int ch)
返回指定字符在此字符串中最后一次出现处的索引。
int lastIndexOf(int ch, int fromIndex)
返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
int lastIndexOf(String str)
返回指定子字符串在此字符串中最右边出现处的索引。

        String s="abc,bcd,bce";
        String s1="Hello";
        System.out.println(s.indexOf("b"));
        System.out.println(s.charAt(0));
        System.out.println(s.indexOf("b",3));
        System.out.println(s1.replace("Hello", "World"));

String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
oldChar没变,返回了一个新的字符串

String s="abc,bcd,bce";
String [] s3=s.split(",");

String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串。
String[] split(String regex, int limit)
根据匹配给定的正则表达式来拆分此字符串
String s="abc,bcd,bce";
String s6=s.substring(1);//从下标1开始截取
String s6=s.substring(1,4);//从下标1开始截取,从4结束。
String substring(int beginIndex) 截取
返回一个新的字符串,它是此字符串的一个子字符串。
char[] toCharArray()
将此字符串转换为一个新的字符数组。

String toLowerCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
String toLowerCase(Locale locale)
使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。
String toUpperCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
String toUpperCase(Locale locale)
使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。
String trim()
返回字符串的副本,忽略前导空白和尾部空白
一般用System.out.println(""+10);
static String valueOf(boolean b)
返回 boolean 参数的字符串表示形式。
static String valueOf(char c)
返回 char 参数的字符串表示形式。
static String valueOf(char[] data)
返回 char 数组参数的字符串表示形式。
static String valueOf(char[] data, int offset, int count)
返回 char 数组参数的特定子数组的字符串表示形式。
static String valueOf(double d)
返回 double 参数的字符串表示形式。
static String valueOf(float f)
返回 float 参数的字符串表示形式。
static String valueOf(int i)
返回 int 参数的字符串表示形式。
static String valueOf(long l)
返回 long 参数的字符串表示形式。
static String valueOf(Object obj)
返回 Object 参数的字符串表示形式。
ISO8859-1(西欧码) GB2312(国标码)GBK Big5(台湾){所有的字母和数字一样}

UTF-8(统一标准)
编码: 把字符串变成数字的过程

        String c="Obama";
        byte b []=c.getBytes("utf-8");
        System.out.println(Arrays.toString(b));

解码要用字符串构造

        byte f[] ={97,98,99,100};
        String ff=new String(f,"utf-8");
        System.out.println(ff);

byte[] getBytes()
使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
byte[] getBytes(Charset charset)
使用给定的 charset 将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。
void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
已过时。该方法无法将字符正确转换为字节。从 JDK 1.1 起,完成该转换的首选方法是通过 getBytes() 方法,该方法使用平台的默认字符集。
byte[] getBytes(String charsetName)
使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此字符串复制到目标字符数组。
格式化:
static String format(Locale l, String format, Object... args)
使用指定的语言环境、格式字符串和参数返回一个格式化字符串。
static String format(String format, Object... args)
使用指定的格式字符串和参数返回一个格式化字符串。

打开App,阅读手记
24人推荐
发表评论
随时随地看视频慕课网APP