欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java常用类-String

程序员文章站 2024-01-28 20:21:10
Java常用类String篇代码演示:public class StringTest {@Testpublic void test2() {//通过字面量定义的方式:此时的s1和s2的数据JavaEE声明在方法区中的字符串常量池中String s1 = "JavaEE";String s2 = "JavaEE";//通过new + 构造器的方式:此时的s3和s4保存的地址值,是数据在堆空间中开辟空间以后对应的地址值String s3 = new Stri...

String篇

Java常用类-StringJava常用类-String
代码演示:

public class StringTest {
	
	@Test
	public void test2() {
		//通过字面量定义的方式:此时的s1和s2的数据JavaEE声明在方法区中的字符串常量池中
		String s1 = "JavaEE";
		String s2 = "JavaEE";
		//通过new + 构造器的方式:此时的s3和s4保存的地址值,是数据在堆空间中开辟空间以后对应的地址值
		String s3 = new String(original: "javaEE");
		String s4 = new String(original: "javaEE");

		System.out,println(s1 == s2); //true
		System.out,println(s1 == s3); //false
		System.out,println(s1 == s4); //false
		System.out,println(s3 == s4); //false

图解:
Java常用类-String
Java常用类-StringJava常用类-String

Java常用类-String

复杂题型:
Java常用类-String
Java常用类-String
常见面试题:

public class StringTest {
	String str = new String(original: "good");
	char[] ch = {'t','e','s','t'};

	public void change(String str, char ch[]) {
		str = "test ok";
		ch[0] = 'b';
	}

	public static void main(String[] args) {
		StringTest ex = new StringTest();
		ex.change(ex.str, ex.ch);
		System.out.println(ex.str);  //good
		System.out.println(ex.ch);   //best
	}
}

常用方法:
Java常用类-String
Java常用类-String

Java常用类-String
String与byte数组之间的转换

@Test
public void test() {
	String str1 = "abc123中国";
	byte[] bytes = str1.getBytes();   //使用默认的字符集
	System.out.println(Arrays.toString(bytes));

	byte[] gbks = str1.getBytes("gbk");  //使用gbk字符集进行编码
	System.out.println(Arrays.toString(gbks));

	System.out.println("***********");

	String str2 = new String(bytes);   //使用默认的字符集,进行解码
	System.out.println(str2);

	String str3 = new String(gbks, "gbk");
}

Java常用类-String

String与char[ ]之间的转换

@Test
public void test() {
	String str1 = "abc123";

	char[] charArray = str1.toCharArray();
	for(int i = 0 ; i < charArray.length; i++) {
		System.out.println(charArray[i]);
	}

	char[] arr = new char[]{'h','e','l','l','o'};
	String str2 = new String(arr);
	System.out.println(str2);
}

Java常用类-String

String与基本数据类型,包装类之间转换

@Test
public void test1() {
	String str1 = "123";
	//int num = (int)str1;(错误的)

	int num = Integer.parseInt(str1);
	String str2 = String.valueOf(num);  // "123"
	String str3 = num + "";

	System.out.println(str1 == str3);   //false

常见练习题

//反转
//方式一:字符数组
public String reverse(String str, int startIndex, int endIndex) {

	if(str != null && str.length() != 0) {
		char[] arr = str.toCharArray();
		for(int x = startIndex , y = endIndex; x < y; x++, y--) {
			char temp = arr[x];
			arr[x] = arr[y];
			arr[y] = temp;
		}
		return new String(arr);
	}
	
	return null;
}

//方式二:使用String 的拼接
public String reverse1(String str, int startIndex, int endIndex) {
	if (str != null) {
		String reverseStr = str.substring(beginIndex:0, startIndex);
		for(int i = endIndex; i >= startIndex; i--) {
			reverseStr += str.charAt(i);
		}
		reverseStr += str.substring(endIndex + 1);
	
		return reverseStr;
	}
	return null;
}

//方式三:使用StringBuffer/StringBuilder替换String
public String reverse2(String str, int startIndex, int endIndex) {
	if (str != null) {
		StringBuilder builder = new StringBuilder(str.length());

		builder.append(str.substring(beginIndex:0, startIndex));
		for(int i = endIndex; i >= startIndex; i--) {
			builder.append(str.charAt(i));
		}
		builder.append(str.substring(endIndex + 1));

		return builder.toString();
	}
	return null;
}
//获取“ab”在“abkkacajfanficaaifababa”中出现的次数
public int getCount(String mainStr, String subStr) {
	int mainLength = mainStr.length();
	int subLength = subStr.length();
	int count = 0;
	int index = 0;
	if(mainLength >= subLength) {
		/*
		//方式一
		while((index = mainStr.indexOf(subStr)) != -1) {
			count++;
			mainStr = mainStr.substring(index + subStr.length());
		}
		*/

		//方式二:对方式一的改进
		while((index = mainStr.indexOf(subStr, index)) != -1) {
			count++;
			index += subLength;
		}
		return count;
	} else {
		return 0;
	}
}

//str1 = "fbakavnaiiaaiofaw"; str2 = "fafag"返回两个串中包含最大的子串
//提示:将短的那个串进行长度依次递减的子串与较长的串比较

public String getMaxSameString(String str1, String str2) {
	String maxStr = (str1.length() >= str2.length() ? str1 : str2;
	String minStr = (Str1.length() < str2.length() ? str1 : str2;
	int length = minStr.length();

	for(int i = 0; i < length; i++) {
		for(int x = 0,y = length - i;y <= length; x++, y++) {
			String subStr = minStr.substring(x, y);
			if(maxStr.contains(subStr) {
				return subStr;
			}
		}
	}
	return null;
}

//改进,如果存在多个相同子串
public String getMaxSameString(String str1, String str2) {
	String maxStr = (str1.length() >= str2.length() ? str1 : str2;
	String minStr = (Str1.length() < str2.length() ? str1 : str2;
	StringBuffer sBuffer = new StringBuffer();
	int length = minStr.length();

	for(int i = 0; i < length; i++) {
		for(int x = 0,y = length - i;y <= length; x++, y++) {
			String subStr = minStr.substring(x, y);
			if(maxStr.contains(subStr) {
				sBuffer.append(subStr + ",");
			}
		}
		if(sBuffer.length() != 0) {
			break;
		}
	}
	String[] split = sBuffer.toString().replaceAll(regex:",$", replacement: "").split(regex:"\\,");
	
	return split;
}

本文地址:https://blog.csdn.net/weixin_45990065/article/details/110183647

相关标签: Java 字符串