Java/String常用方法及字符串与基本类型的转换

字符串相等判断

1)测试内容相同用s.equals(t)
2)测试除大小写区分外是否相等用equalslgnoreCase方法

package TestTextbook;

/**
 * @author cc
 * @date 2020年3月26日
 */
public class TestString {
	public static void main(String[] args) {
		Equation a = new Equation();
		a.Equation();
		Use b = new Use();
		b.Use();
	}
}

// 测试字符串相等判断
class Equation {
	String a1 = "cc";
	String a2 = "cc";
	String a3 = new String("cc");

	public void Equation() {
		System.out.println(a1 == a2);// true
		System.out.println(a1 == a3);// flase
		System.out.println(a1.equals(a3));// true
		System.out.println("############");
	}
}

// 测试字符串使用
class Use {
	String b1 = "";
	String b2 = "cc is";
	String b3 = "18";

	void Use() {
		System.out.println(b1);
		System.out.println(b2 + b3);// cc is18
		System.out.println(b3 + 19);// 1819
		System.out.println("#############");
	}
}

String常用方法

具体使用在代码里,代码里有黄金屋,代码里有颜如玉。
String类常用的方法有:

  1. String类的下述方法能创建并返回一个新的String对象: concat()、 replace()、substring()、 toLowerCase()、 toUpperCase()、trim()。
  2. 提供查找功能的有关方法: endsWith()、 startsWith()、 indexOf()、lastIndexOf()。
  3. 提供比较功能的方法: equals()、equalsIgnoreCase()、compareTo()。
  4. 其它方法: charAt() 、length()。
package TestTextbook;

public class TestString01 {
	public static void main(String[] args) {
		Length a = new Length();
		a.length();
		CharAt b = new CharAt();
		b.charAt();
		SubString c = new SubString();
		c.subString();
		Compare d = new Compare();
		d.compare();
		Concat e = new Concat();
		e.concat();
		IndexOf f = new IndexOf();
		f.indexOf();
		ToCase g = new ToCase();
		g.toCase();
		Replace h = new Replace();
		h.replace();
		Trim i = new Trim();
		i.trim();
		StatWith j = new StatWith();
		j.statWith();
		RegionMa k = new RegionMa();
		k.regionMa();
		Contains l = new Contains();
		l.contains();
		Split m = new Split();
		m.split();
	}

}

// 测试int length()
// 返回此字符串的长度。长度等于字符串中 16 位 Unicode 字符数。
class Length {
	String a1 = "WANGJIABING";
	String a2 = new String("VIVEICE");

	// int strlengtn = a1.length();
	// int strlength = a2.length();
	void length() {
		System.out.println(a1.length());
		System.out.println(a2.length());
		System.out.println("##############");
	}

}

// 测试char charAt()
// 回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是length()-1。
class CharAt {
	String b1 = new String("WANGJIABING");
	char b2 = b1.charAt(5);

	void charAt() {
		System.out.println(b2);// I
		System.out.println(b1.charAt(5));
		System.out.println("##############");
	}
}
/*
 * 测试用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:
 *  1)public String substring(int beginIndex) 
 *  该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。 
 *  2)public String substring(intbeginIndex, int endIndex)
 *  该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。
 */

class SubString {
	String c1 = new String("WANGJIABING");
	String c2 = c1.substring(2);
	String c3 = c1.substring(2, 5);

	void subString() {
		System.out.println(c2);// NGJIABING
		System.out.println(c3);// NGJ
		System.out.println(c1.substring(2));
		System.out.println(c1.substring(2, 5));
		System.out.println("##############");
	}
}

/*
 * 字符串比较一系列方法
 *  1)public int compareTo(String anotherString)
 * 对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
 * 2)public int compareToIgnore(String anotherString) 与compareTo方法相似,但忽略大小写。
 * 3)public boolean equals(Object anotherObject)
 * 比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
 *  4)public boolean equalsIgnoreCase(String anotherString) 与equals方法相似,但忽略大小写。
 */
class Compare {
	String d1 = new String("VIVEICE");
	String d2 = new String("viveice");
	/*
	 * int a = d1.compareTo(d2); int b = d1.compareToIgnoreCase(d2); boolean c =
	 * d1.equals(d2); boolean d = d1.equalsIgnoreCase(d2);
	 */
	void compare() {
		System.out.println(d1.compareTo(d2));
		System.out.println(d1.compareToIgnoreCase(d2));
		System.out.println(d1.equals(d2));
		System.out.println(d1.equalsIgnoreCase(d2));
		System.out.println("##############");
	}
}

/*
 * 字符串连接 public String concat(String str) 将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。
 */
class Concat {
	String e1 = "aa".concat("bb").concat("cc");

	void concat() {
		System.out.println(e1);
		System.out.println("a".concat("b").concat("c"));
		System.out.println("##############");
	}
}

/*
 * 字符串中单个字符查找
 * 1)public int indexOf(int ch/String str)
 * 用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。 
 * 2)public int indexOf(int ch/String str, int fromIndex) 改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。
 * 3)public int lastIndexOf(int ch/String str) 该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
 * 4)public int lastIndexOf(int ch/String str, int fromIndex)
 * 该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。
 */
class IndexOf {
	String f1 = new String("WANGJIABING");
	int f2 = f1.indexOf("A");

	void indexOf() {
		System.out.println(f1.indexOf("A"));// 1
		System.out.println(f1.indexOf("B", 5));// 7
		System.out.println(f1.lastIndexOf("A"));// 6
		System.out.println(f1.lastIndexOf("B", 5));// -1
		System.out.println("##############");
	}

}

/*
 * 字符串中字符的大小写转换 
 * 1)public String toLowerCase()//返回将当前字符串中所有字符转换成小写后的新串 2)public
 * String toUpperCase()//返回将当前字符串中所有字符转换成大写后的新串
 */
class ToCase {
	String g1 = "WANGJIABING";
	String g2 = "wangjiabing";

	void toCase() {
		System.out.println(g1.toLowerCase());//wangjiabing
		System.out.println(g2.toUpperCase());//WANGJIABING
		System.out.println("##############");
	}
}
/*
 * 字符串中字符的替换 
 * 1)public String replace(char oldChar, char newChar)
 * 用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。 
 * 2)public String replaceFirst(String regex, String replacement)
 * 该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。
 * 3)public String replaceAll(String regex, String replacement)
 * 该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。
 */

class Replace{
	String h1 = "VIVEICE";
	String h2 = "WANGJIABING";
	void replace(){
		System.out.println(h1.replace("I", "C"));//VCVECCE
		System.out.println(h2.replaceFirst( "NG","555"));//WA555JIABING
		System.out.println(h2.replaceAll("NG","555"));//WA555JIABI555
		System.out.println("##############");
	}
}
//String trim()截去字符串两端的空格,但对于中间的空格不处理。
class Trim{
	String i1 = " C C ";
	void trim(){
		System.out.println(i1.trim());
		System.out.println("##############");
	}
}
/*
 * boolean statWith(String prefix)或boolean endWith(String suffix)
 * 用来比较当前字符串的起始字符或子字符串prefix和终止字符或子字符串suffix是否和当前字符串相同.
 * 重载方法中同时还可以指定比较的开始位置offset。
 */
class StatWith{
	String j1 = "wang";
	 //boolean a = j1.startsWith("wan");
	 //boolean b = j1.endsWith("gi");
	 void statWith(){
		 System.out.println(j1.startsWith("wan"));//true
		 System.out.println(j1.endsWith("g1"));//false
		 System.out.println(j1.startsWith("an", 1));//true
		 System.out.println("##############");
	 }
}
/*
 * regionMatches(boolean b, int firstStart, String other, int otherStart, int length)
 * 从当前字符串的firstStart位置开始比较,取长度为length的一个子字符串,other字符串从otherStart位置开始,
 * 指定另外一个长度为length的字符串,两字符串比较,当b为true时字符串不区分大小写。
 */
class RegionMa{
	String k1 = "WANGJIABING";
	void regionMa(){
		System.out.println(k1.regionMatches(5, "ANG", 3, 7));//false
		System.out.println("##############");
	}
}
//contains(String str)判断参数s是否被包含在字符串中,并返回一个布尔类型的值。
class Contains{
	String l1 = "viveice";
	void contains(){
		System.out.println(l1.contains("i"));//true
		System.out.println("##############");
	}
}
//String[] split(String str)将str作为分隔符进行字符串分解,分解后的字字符串在字符串数组中返回。
class Split{
	String m1 = "wang,jia,,bing,";
	String[] m2 = m1.split(",");//str1[0] = "asd";str1[1] = "qwe";str1[2] = "zxc";
	void split(){
		System.out.println(m2.length);
		for (String m3:m2){
			System.out.println(m3);
		}
		/*
		 * 输出结果
		 * wang
		 * jia

		 * bing
		 */
		System.out.println("##############");
	}
}

字符串与基本类型的转换

字符串转换为基本类型

int a = Integer.parseInt("12");
float b = Float.parseFloat("12.34");
double c = Double.parseDouble("1.124");

基本类型转换为字符串类型

String a = String.valueOf(12);
String b = String.valueOf(12.34);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值