java中tostring例子_Java中String类的常用方法示例

String的常用方法:

1. public char charAt(int index)

返回字符串中的index个字符

2. public int length()

返回字符串长度

3. public int indexOf(String str)

返回字符串中出现str的第一个位置

4. public boolean equalsIgnoreCase(String another)

比较字符串与another是否相同(忽略大小写)

5. public String replace(char oldChar, char newChar)

在字符串中用newChar替换oldChar

6. public boolean startsWith(String prefix)

判断字符串是否以prefix开头

7. public boolean endsWith(String suffix)

判断字符串是否以suffix结束

8. public String toUpperCase()

返回该字符串的大写形式

9. public String toLowerCase()

返回该字符串的小写形式

10. public String substring(int beginIndex)

返回该字符串从beginIndex开始到结尾的子串

11. public String substring(int beginIndex,int endIndex)

返回该字符串从beginIndex开始到endIndex结尾的子串

13. public String trim()

去除字符串头尾的空格

示例如下:

961ddebeb323a10fe0623af514929fc1.png

1 packagedg;2 3 4 /**5 *6 *@authorDUGANG7 */8 publicclassStringMethod {9 publicstaticvoidmain(String[] args){10 String str="Hello World!!!";11 System.out.println("示例字符串为:"+str);12 //public char charAt(int index)方法13 System.out.println(str+"中第四个字符为:"+str.charAt(4));14 //public int length()方法15 System.out.println(str+"的字符长度为:"+str.length());16 //public int indexOf(String str)方法17 System.out.println(str+"中第一个“l”出现在字符串的第"+str.indexOf("l")+"位");18 //public boolean equalsIgnoreCase(String another)方法19 System.out.println(str+"与hello world!!!相同:"+str.equalsIgnoreCase("hello world!!!"));20 //public String replace(char oldChar, char newChar)方法21 System.out.println(str+"用‘L’替换‘l’后为:"+str.replace("l","L"));22 //public boolean startsWith(String prefix)方法23 System.out.println(str+"以字母‘H’开头:"+str.startsWith("H"));24 //public boolean endsWith(String suffix)方法25 System.out.println(str+"以‘!’结尾:"+str.endsWith("!"));26 //public String toUpperCase()方法27 System.out.println(str+"转换为大写:"+str.toUpperCase());28 //public String toLowerCase()fangfa29 System.out.println("JAVA转换为小写:"+"JAVA".toLowerCase());30 //public String substring(int beginIndex)31 System.out.println(str+"从第六个字符开始截取子串为:"+str.substring(6));32 //public String trim()方法33 System.out.println("hello"+"去掉开头和结尾的空格为:"+"hello".trim());34 }35 36 }

package dg;

/**

*

* @author DUGANG

*/

public class StringMethod {

public static void main(String[] args){

String str = "Hello World!!!";

System.out.println("示例字符串为:" + str);

//public char charAt(int index)方法

System.out.println(str + "中第四个字符为:" + str.charAt(4));

//public int length()方法

System.out.println(str + "的字符长度为:" + str.length());

//public int indexOf(String str)方法

System.out.println(str + "中第一个“l”出现在字符串的第" + str.indexOf("l") + "位");

//public boolean equalsIgnoreCase(String another)方法

System.out.println(str + "与hello world!!!相同:" + str.equalsIgnoreCase("hello world!!!"));

//public String replace(char oldChar, char newChar)方法

System.out.println(str + "用‘L’替换‘l’后为:" + str.replace("l", "L"));

//public boolean startsWith(String prefix)方法

System.out.println(str + "以字母‘H’开头:" + str.startsWith("H"));

//public boolean endsWith(String suffix)方法

System.out.println(str + "以‘!’结尾:" + str.endsWith("!"));

//public String toUpperCase()方法

System.out.println(str + "转换为大写:" + str.toUpperCase());

//public String toLowerCase()fangfa

System.out.println("JAVA转换为小写:" + "JAVA".toLowerCase());

//public String substring(int beginIndex)

System.out.println(str + "从第六个字符开始截取子串为:" + str.substring(6));

//public String trim()方法

System.out.println(" hello " + "去掉开头和结尾的空格为:" + " hello ".trim());

}

}

结果:

示例字符串为:Hello World!!!

Hello World!!!中第四个字符为:o

Hello World!!!的字符长度为:14

Hello World!!!中第一个“l”出现在字符串的第2位

Hello World!!!与hello world!!!相同:true

Hello World!!!用‘L’替换‘l’后为:HeLLo WorLd!!!

Hello World!!!以字母‘H’开头:true

Hello World!!!以‘!’结尾:true

Hello World!!!转换为大写:HELLO WORLD!!!

JAVA转换为小写:java

Hello World!!!从第六个字符开始截取子串为:World!!!

hello 去掉开头和结尾的空格为:hello

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值