2020/08/09 QQ1017871939 小白学习字符串

3 篇文章 0 订阅
1 篇文章 0 订阅

小白学习字符串

在这里插入图片描述
在这里插入图片描述

1.字符串,不可变特性

// 字符串演示
public class StringTest1
{
//
public void test3(){

	String s1 = "uplooking";
	// 字符替换的时候,依然是创建新的字符串
	String s2 = s1.replace('u','U');

	System.out.println(s1);
	System.out.println(s2);

}

public void test2(){

	String s1 = "北京";

	// 通过连接字符串,也是不会改变原来的字符串“北京”的
	String s2 = s1+"上海"; // 创建了一个新的字符串,“北京上海”

	System.out.println(s1);

	System.out.println(s2);
}


// java.lang.String lang包下的类直接可以用,无需导包
public void test1(){
	// 
	String s1 = "Hello";// 字面量
	String s2 = "Hello";
	
	// 重新创建了一个字符串,不会在原来的基础上加上: Tom
	s1 = "Hello Tom";

	System.out.println(s1);

	System.out.println(s2);
}

public static void main(String[] args){
	StringTest1 st = new StringTest1();

	// st.test1();
	// st.test2();

	st.test3();
}

};
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

!!!:关于对内存和常量池内的判断。面试题会遇到

public class String2
{
public void func1(){
String s1 =“Hello”; //字面量
String s2 =new String(“Hello”);
String s3 =“HelloWorld”;
//带变量的字符串连接,相当于在堆中new了一个字符串
String s4 =s1+“World”; //HelloWorld
String s5 =“Hello”+“World”;
System.out.println(“s5 :”+s5);
System.out.println(“s4 :”+s4);

	 //用==进行字符比较的时候,比较的是对象的引用(地址)
	  System.out.println(s1==s2);//false
	  
	  System.out.println(s1==s3);//false
	  System.out.println(s3==s4);//false
	  System.out.println(s3==s5);//true  字面量相加等于常量池内所创建的常量
}
  public static void main(String[] args){
	  String2 tc =new String2();
	
	  tc.func1();
  }
};

在这里插入图片描述

public class Mianshi
{

public void func1(){
String s1 ="hello";  //字面量

String s2 =new String("hello");  //新创建的对象

// ==比较的是地址(引用)

System.out.println(s2==s1);

//比较的是String的字符串本身
System.out.println(s2.equals(s1));

//字符串双引号  字符单引号
char[] ch ={'h','e','l','l','o'};

String s3 =new String(ch);

String s4 =new String(ch,1,3);
System.out.println(s3);
System.out.println(s1==s3);
System.out.println(s2==s3);

System.out.println(s4);
}


public static void main(String[] args){
 Mianshi tc =new Mianshi();
 tc.func1();

}

};

在这里插入图片描述

一道模拟公司的面试题
2.字符串:String,定义的时候
public final class Stringextends Objec timplements Serializable, Comparable, CharSequence

final:String类不能被继承
Serializable:String是可以序列化
Comparable:String可以比较
CharSequence:字符序列

// 字符串创建演示
public class StringTest2
{

public void test2(){
	String s1 = "广州";
	String s2 = "甲骨文科技";

	String s3 = "广州甲骨文科技";

	String s4 = s1+"甲骨文科技"; // 字符串连接如果有变量,相当于new了一个字符串 

	String s5 = "广州"+s2;

	String s6 = s1+s2;
	// 字面量字符串连接,不是new字符串,在常量池中查找相同的如果有就使用了
	String s7 = "广州"+"甲骨文科技"; 

	System.out.println(s3 == s4);// false
	System.out.println(s3 == s5);// false
	System.out.println(s3 == s6);// false
	System.out.println(s4 == s5);// false 
	System.out.println(s5 == s6);// false
	System.out.println(s6 == s7);// false

	System.out.println(s3 == s7);// true 

}

// java.lang.String lang包下的类直接可以用,无需导包
public void test1(){
	// 
	String s1 = "Hello";// 字面量


	String s2 = new String("Hello"); // 堆内存中

	String s3 = "HelloWorld";

	// 带变量的字符串连接,相当于在堆中new了一个字符串
	String s4 = s1+"World"; // HelloWorld



	System.out.println(s1);
	System.out.println(s2);
	
	// 用==进行字符串比较的时候,比较的是对象的引用(地址)
	System.out.println(s1 == s2); //false
	
	System.out.println(s3 == s4); // false

}

public static void main(String[] args){
	StringTest2 st = new StringTest2();

	// st.test1();

	st.test2();
	
}

};
在这里插入图片描述

在这里插入图片描述

字符串的单词替换
public class String1
{

//java.lang.String lang 包下的类可以直接使用
 public void func3(){
  String s1 ="uplooking";
  //字符替换的时候,依然是创建新的字符串
  String s2 =s1.replace("u","U");

  System.out.println(s1);
  System.out.println(s2);

 }


  public void func2(){
  String s1 ="上海";
  //通过连接字符串,也是不会改变原来的字符串"北京"的
  String s2 =s1+"北京"; //创建一个新的字符串""
  
  System.out.println(s1);
  System.out.println(s2);

  }
 
 public void func1(){

  String s1 ="Hello";  //字面量
  String s2 ="Hello";

  //重新创建一个字符串
  s1 ="Hello Tom";
  System.out.println(s1);
  System.out.println(s2);
 }

public static void main(String[] args){
String1 tc =new String1();
// tc.func1();
//tc.func2();
tc.func3();
}
};
在这里插入图片描述

在这里插入图片描述

// 字符串常用方法演示
public class StringTest4
{

public void test1(){
	// 
	String s1 = "Hello World";// 字面量

	// 数组中的length是属性,不需要写()
	// String中length是方法,要写()
	System.out.println(s1.length());

	// charAt()
	// char ch = s1.charAt(60); // StringIndexOutOfBoundsException:
	
	 char ch = s1.charAt(6);
	System.out.println(ch);
	// 取子字符串
	// 包头不包尾
	String str = s1.substring(2,10);
	String str1 = s1.substring(2);

	System.out.println(str);
	System.out.println(str1);

	//compareTo() 比较字符串
	String s2 = "abc";
	String s3 = "def";
	System.out.println(s2.compareTo(s3));// 0

	// concat() 连接字符串

	String s4 = s2.concat(s3);

	System.out.println("s2 : "+s2);
	System.out.println("s4 : "+s4);

	//contains(s) 是否包含
	String s5 = "我是韩老师是韩老师是韩老师是韩老师";

	System.out.println(s5.contains("韩老123"));

	// endsWith(s) 是否是以s结束的
	System.out.println(s5.endsWith("张老师"));

	// indexOf(s) 指定子字符串在此字符串中第一次出现处的索引
	System.out.println(s5.indexOf("是韩")); //

	// indexOf(String str,  int fromIndex) fromIndex从什么位置开始搜索
	System.out.println(s5.indexOf("是韩",10)); //

	// isEmpty() 当且仅当 length() 为 0 时返回 true。 
	String s6 = "";
	String s7 = null;
	System.out.println(s5.isEmpty());
	System.out.println(s6.isEmpty());
	// System.out.println(s7.isEmpty()); // NullPointerException

	// lastIndexOf(str)  此字符串中最右边出现处的索引
	System.out.println(s5.lastIndexOf("韩老师"));

	//  boolean  matches(regex) 告知此字符串是否匹配给定的正则表达式
	String s8 = "010-334455660";
	System.out.println(s8.matches("010-\\d+"));// \\d+ 一个或者多个数字
	System.out.println(s8.matches("010-\\d{7,8}"));

	// String replaceAll(String regex,String replacement)  regex正则表达式

	String s9 = "445北京778上海996广州556深圳112";
	String s10 = s9.replaceAll("\\d","");

	System.out.println(s10);

	// String[] split(String regex)
	String s11 = "北京2上海9广州5深圳";
	String[] arrayStr = s11.split("\\d");

	for(String s : arrayStr){
		System.out.println(s);
	}
	// boolean startsWith(String prefix)
	
	// String toLowerCase() 
	// String toUpperCase()
	String s12 = "abcDefHjkl";

	System.out.println(s12.toLowerCase());
	System.out.println(s12.toUpperCase());

	// String trim()忽略前导空白和尾部空白
	String s13 = "  刘备 关羽 张飞   ";
	String s14 = s13.trim();

	System.out.println("-----"+s13+"------");
	System.out.println("-----"+s14+"------");

	//  static String valueOf(char[] data)
	// 一个新分配的字符串,它表示包含在字符数组参数中的相同字符序列
	char[] c = {'张','三','丰'};
	String s15 = String.valueOf(c);

	System.out.println(s15);


}

public static void main(String[] args){
	StringTest4 st = new StringTest4();

	st.test1();

}

};

在这里插入图片描述

public class Huoqu
{
public void func1(){
String str01 =“Hello,welcome to China!”;
//索引位置 012345678…
//获取字符串的长度
int len =str01.length();
System.out.println(“Str01的长度:”+len);
//取某个索引位置上的字符
System.out.println(“索引位置4上的字符:”+str01.charAt(4));
//获取指定位置的子字符串,一直取到结尾
String s1 =str01.substring(4);
//含前面的索引,不包含后面的索引位置的字符串
String s2 =str01.substring(4,10);
System.out.println(“截取字符串s1:”+s1);
System.out.println(“截取字符串s2:”+s2);
//找到指定字符所处的第一个位置的索引
System.out.println(str01.indexOf(‘w’));
//找到指定字符所处的最后一个位置的索引
System.out.println(str01.indexOf(‘a’));

//将字符串全部转换成大写
System.out.println(str01.toUpperCase());
}

public static void main(String[] args)
{
   Huoqu tc =new Huoqu();
   tc.func1();
}

};

在这里插入图片描述

在这里插入图片描述

public class Fanzhuan
{
public void func1(){
//String是引用数据类型
String str =“aerhefnweufhg”;
//首先把字符串转换成字符类型的数组
char[] ch =str.toCharArray();
//反转字符串
int len =ch.length;

for (int i=0;i<ch.length ; i++)
{

  ch[i] =str.charAt(len-1-i);

}
System.out.println(new String(ch));
}
public void func2(){

}
public static void main(String[] args)
{
Fanzhuan tc =new Fanzhuan();
tc.func1();
}
};
在这里插入图片描述

在这里插入图片描述

import java.util.StringTokenizer;
public class Mianshi5
{
public void func1(){
String str =“北京,上海,深圳,武汉,长沙,重庆,”;
//两个参数:第一个要分割的字符串,第二个指定分隔符
StringTokenizer st =new StringTokenizer(str,",");
while(st.hasMoreTokens()){
String s =st.nextToken();
System.out.println(s);
}
}
public static void main(String[] args)
{
Mianshi5 tc =new Mianshi5();
tc.func1();
}
};

在这里插入图片描述

字符串和数组的相互转换
public class Mianshi4
{

public void func1(){
//
String s1 ="hello";  //字面量

char[] a =s1.toCharArray();
for (char b :a )
{
	System.out.print(b);
}
//第二种方法把数组转换字符串
String s2 =new String(a);
System.out.print(s2);

//String s2 =String.valueOf(a);
//System.out.print(s2);
}

public void func2(){
//字符串转换数组,数组转换字符串
String s1 =“hello”; //字面量
//把字符串放进数组里面
char[] a =s1.toCharArray();
for (char b :a )
{
System.out.print(b);
}
//static String valueOf(char[] data)
//一个新分配的字符串,它表示包含在字符数组参数
String s16 =String.valueOf(a);
System.out.println(s16);

}

public static void main(String[] args){
 Mianshi4 tc =new Mianshi4();
// tc.func1();
  tc.func2();

}

};
在这里插入图片描述

在这里插入图片描述

练习一:接下来通过一个案例来熟悉二维数组的使用。

例如要统计一个公司三个销售小组中每个小组的总销售额以及整个公司的销售额。

如下所示:
第一小组销售额为{11, 12}万元
第二小组销售额为{21, 22, 23}万元
第三小组销售额为{31, 32, 33, 34}万元。

public class Zuoye1
{
		int [][] b ={
		{11,12},
		{21,22,23},
		{31,32,33,34}
		 };

    public void func3(){
		//计算整个公司的销售额
             int sum1 =0;
			 int pin1 =0;  //计算整个公司的平均销售额
			 for(int i=0;i<b.length;i++){
				//内循环遍历的是列
				int sum =0; //计算每个小组的总销售额
				int pin =0; //计算每个小组平均销售额
				for (int j=0;j<b[i].length;j++ )
				{
                    
					sum +=b[i][j];

				}
				    pin +=(sum/b[i].length);
				    System.out.println("第"+(i+1)+"小组总销售额 ="+sum+"万元"+"\t");
					System.out.println("第"+(i+1)+"小组平均销售额 =" +pin+"万元");
				sum1 +=sum;
				pin1 +=pin;

			 }
		 System.out.println("整个公司的销售额 ="+sum1+"万元"+"\t");
		 System.out.println("整个公司的平均销售额 ="+(pin1/b.length)+"万元"+"\t");
		

		}

	public static void main(String[] args){
		 Zuoye1 tc = new Zuoye1(); 
		 tc.func3();
	}
};

在这里插入图片描述

练习二:请将文字“学而不思则罔,思而不学则殆”逆序打印到控制台。
public class Daoxu
{

/*
用string倒序输出
利用string类的tocharArray(),在倒序输出数组的方法

*/
public void func1(String str){

  char[] chr =str.toCharArray();
  System.out.print("方法一:");
  for (int i=chr.length-1;i>=0 ;i-- )
  {
	  System.out.print(chr[i]);

  }
      System.out.println("\t");

}
//利用stringBuff类

public void func2(String str){

 StringBuffer buffer =new  StringBuffer(str);
 System.out.println("方法二:"+buffer.reverse());

}

public static void main(String[] args){
   Daoxu tc =new Daoxu();
   String str ="学而不思则罔,思而不学则殆";
   System.out.println("原文字:"+str);
   tc.func1(str);
   tc.func2(str);

}

};
在这里插入图片描述

练习三:定义一个String类型数组,打印出字符串的长度,并且输出每个值。
输出每个值是遍历
public class Zuoye2
{

public void func1(){

/* 定义一个长度为4的String类型数组,
包含如下元素:”12ab”,”java”,”45Cd”,”Server78”;*/
String[] arr = {“12ab”,“java”,“45Cd”,“Server78”};
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]+ “\t”+arr[i]+“的字符串长度=”+arr[i].length());
}
//打印出字符串的长度

 //把String数组内的元素拼接
 StringBuilder stringBuilder = new StringBuilder();
            for(int i = 0; i < arr.length; i++)
            {
                stringBuilder.append(arr[i]);
            }
	System.out.println(stringBuilder);
    System.out.print("整个String数组的长度为"+stringBuilder.length());
  
}


public static void main(String[] args){
 Zuoye2 tc =new Zuoye2();

  tc.func1();

}

};
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值