java中string类的常用方法举例说明

strbegin-字符串中要复制的第一个字符的索引
strend-字符串中要复制的最后一个字符之后的索引
char[]data-目标数组
offset-目标数据中的起始偏移量

  • void getChars(int strbegin,int strend,char[]data,int offset)
    将这个字符串的字符复制到目的数组
package com;

public class StringDemo {
  public static void main(String args[])
  {
	  String s=new String("计算机学院战胜文学院");
	  char []c=new char[s.length()];
	  s.getChars(7,10,c,0);//7,10是指s这个字符串的起始,0是指放在c中的起始位置
	  System.out.println(c);
	  s.getChars(5,7,c,3);
	  System.out.println(c);
	  s.getChars(0,5,c,5);
	  System.out.println(c);
	  
	  
  }
}

输出结果为:

文学院
文学院战胜
文学院战胜计算机学院
  • int indexOf(int char)
    产生这个字符串中出现给定字符的第一个位置的索引(int char是ASCII码)
public class StringDemo {
  public static void main(String args[])
  {
	  char[] ch= {'a','b','c','h','e','l','l','o'};
      String str=new String(ch);
      str.indexOf(104);
      System.out.println(str.indexOf(104));//h所在下标为3
  }
}

输出结果:

3
  • int indexOf(int ch,int formIndex)
    从给定的索引处开始,产生这个字符串中出现给定字符的第一个位置的索引
public class StringDemo {
  public static void main(String args[])
  {
	  char[] ch= {'a','b','c','h','e','l','l','o'};
      String str=new String(ch);
      str.indexOf(101, 4);
      System.out.println(str.indexOf(101, 4));
  }
}

输出结果:

4
  • int indexOf(String str)
    产生这个字符串中出现给定子字符的第一个位置的索引
 public class StringDemo {
  public static void main(String args[])
  {
	  char[] ch= {'a','b','c','h','e','l','l','o'};
      String str=new String(ch);
      str.indexOf("hell");
      System.out.println(str.indexOf("hell"));//3
   }
}

输出结果:

3
  • int indexOf(String str,int formIndex)
    从给定的索引处开始,产生这个字符串中出现给定子字符的第一个位置的索引
public class StringDemo {
	  public static void main(String args[])
	  {
		  char[] ch= {'a','b','c','h','e','l','l','o'};
	      String str=new String(ch);
	      str.indexOf("che", 0);//等价于str.indexOf("che")
	      System.out.println(str.indexOf("che", 0));//2
	   }
	}

输出结果:

2
  • int length()
    产生这个字符串的长度
public class StringDemo {
	  public static void main(String args[])
	  {
		  String s1="hello,java!";
		  int len1=s1.length();
	      System.out.println("s1长度为"+len1);
	   }
	}

输出结果:

s1长度为11
  • boolean starsWith(String prefix)
    检查这个字符串是否以给定的字符开头
public class StringDemo {
	  public static void main(String args[])
	  {
		  String s1=new String("www.111.com");
	      System.out.println(s1.startsWith("www"));
	   }
	}

输出结果是:

true
  • boolean starsWith(String prefix,int toffset)
    从给定的索引处开头,检查这个字符串是否以给定的前缀开头
public class StringDemo {
	  public static void main(String args[])
	  {
		  String s1=new String("www.111.com");
	      System.out.println(s1.startsWith("111",4));
	   }
	}

输出结果是:

true
  • String substring(int strbegin)
    产生一个新字符串,它是这个字符串的子字符串
public class StringDemo {
	  public static void main(String args[])
	  {
		  String s1=new String("www.111.com");
	      System.out.println(s1.substring(2));
	   }
	}

输出结果是:

w.111.com
  • String substring(int strbegin,int strend)
    产生一个新字符串,它是这个字符串的子字符串,允许指定结尾处的索引
public class StringDemo {
	  public static void main(String args[])
	  {
		  String s1=new String("www.111.com");
	      System.out.println(s1.substring(2,7));
	   }
	}

输出结果是:

w.111
  • char[] toCharArray()
    将这个字符串转化为新的字符数组
public class StringDemo {
	  public static void main(String args[])
	  {
		  String s1=new String("www.111.com");
	      System.out.println(s1.toCharArray());
	   }
	}

输出结果是:

www.111.com
  • String toLowerCase()
    将这个String对象中的所有字符变为小写
public class StringDemo {
	  public static void main(String args[])
	  {
		  String s1=new String("WWW.111.COM");
	      System.out.println(s1.toLowerCase());
	   }
	}

输出结果是:

www.111.com

String toUpperCase()
将这个String字符串中的所有字符变为大写

public class StringDemo {
	  public static void main(String args[])
	  {
		  String s1=new String("www.111.com");
	      System.out.println(s1.toUpperCase());
	   }
	}

输出结果是:

WWW.111.COM
  • static String valueOf(int i)
    将int参数转化为字符串返回。该方法有很多重载方法,用来基本数据类型转化为字符串
public class StringDemo {
	  public static void main(String args[])
	  {
		  int i=10;
		  System.out.println(String.valueOf(i));
	   }
	}

输出结果为:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值