Java基础学习笔记之六(1)__常用类String

String 类

  Strings are constant; their values cannot be changed after they are created.
  String 代表不可变的字符序列。

备注:在内存中的位置

String str = "abc";

  1.引用str放在栈中
  2.字符串常量放在常量池(字符串常量池)中,第一次创建放入池中,第二次使用直接把引用指向池中已有的数据。

  3.new String("xxx")创建的 数据放在堆中,每次new都会在堆中创建一块内存存放数据。

常用构造

1.String()
  Initializes a newly created String object so that it represents an empty character sequence.
  初始化一个新创建的 String 对象,它表示一个空字符序列。
2.String(byte[] bytes) 
  Constructs a new String by decoding the specified array of bytes using the platform's default charset.
  构造一个新的 String,方法是使用平台的默认字符集解码字节的指定数组。
3.String(byte[] bytes, String charsetName)  
  Constructs a new String by decoding the specified array of bytes using the specified charset.
  构造一个新的 String,方法是使用指定的字符集解码指定的字节数组。
4.String(char[] value)
  Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.
  分配一个新的 String,它表示当前字符数组参数中包含的字符序列。
5.String(String original) 
  Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.
  初始化一个新创建的 String 对象,表示一个与该参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的一个副本。

常用方法

  1.public char charAt(int index);
  Returns the char value at the specified index.
  返回指定索引处的 char 值
  
  2.public int length();
  Returns the length of this string.
  返回此字符串的长度。
  
  3.public int indexof(String str);
  Returns the index within this string of 
the first occurrence of the specified substring.
  返回第一次出现的指定子字符串在此字符串中的索引。
  
  4.public int indexof(String str,int fromIndex);
  Returns the index within this string of the first occurrence 
of the specified substring, starting at the specified index.  
  从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引
  
  5.public boolean equalsIgnoreCase(String another);
  Compares this String to another String, ignoring case considerations.
  将此 String 与另一个 String 进行比较,不考虑大小写。
  
  6.public String replace(char oldChar,char newChar)
  Returns a new string resulting from replacing all occurrences of oldChar 
in this string with newChar
  返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的
----------------------------------------------------
  7.public boolean startWith(String prefix);
  Tests if this string starts with the specified prefix.
  测试此字符串是否以指定的前缀开始。
  
  8.public boolean endsWith(String suffix);
  Tests if this string ends with the specified suffix.
  测试此字符串是否以指定的后缀结束。
  
  9.public String toUpperCase();
  Converts all of the characters in this String to 
upper case using the rules of the default locale.
  使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
  
  10.public String toLowerCase();
  Converts all of the characters in this String to 
lower case using the rules of the default locale.
  用默认语言环境的规则将此 String 中的所有字符都转换为小写。
----------------------------------------------------   
  11.public String substring(int beginIndex);
  Returns a new string that is a substring of this string.
  返回一个新的字符串,它是此字符串的一个子字符串。
  (返回该字符串从beginIndex开始到结尾的子字符串)
  
  12.public String trim();
  Returns a copy of the string, with leading 
and trailing whitespace omitted.

  返回字符串的副本,忽略前导空白和尾部空白。

----------------------------------------------------   

  13.public static String valueOf(...);
  Returns the string representation of the Object argument.
  返回 Object 参数的字符串表示形式。
  
  14.public String[] split(String regex);
  Splits this string around matches of the given regular expression.

  根据给定的正则表达式的匹配来拆分此字符串。

参考文章:1.官方API  http://docs.oracle.com/javase/6/docs/api/

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值