Loading

String的各类方法


返回 我的技术栈(Technology Stack)



Eclipse中如何删除项目和导入项目


  • 删除项目:

    • 选中项目——右键——删除
    • 打钩:从硬盘中直接删除
    • 不打钩:从项目区域中删除
  • 导入项目:

    • 在项目区域右键找到import
    • 找到General,展开
    • 找到existing project into workspace
    • 点击next,然后选择你要导入的项目
    • 注意:这里选择的是项目名称

API(Application Programming Interface)


  • 应用程序编程接口
  • 就是Java提供给我们使用的类,这些类将底层的实现封装了起来

统计不同类型字符个数


  • 要点:char c = s.charAt(i); 获取String字符串中的每一个字符

      public class One {
      
      	public static void main(String[] args) {
      		String s = "ABCDEFabcdef123456/';=-";
      		int big = 0;
      		int small = 0;
      		int num = 0;
      		int other = 0;
      		for(int i = 0;i < s.length(); i ++) {
      			char c = s.charAt(i);    //通过索引获取每一个字符
      			if(c >= 'A' && c<='Z') {
      				big++;
      			}else if(c >= 'a' && c <= 'z') {
      				small++;
      			}else if(c >= '0' && c <= '9') {
      				num ++;
      			}else {
      				other ++;
      			}
      		}
      		System.out.println( s + "字符串中,有大写字母" + big + "个,有小写字母" + small 
      				+ "个,有数字" + num + "个,其他字符" + other + "个" );
      	}
      }
      
      输出结果:ABCDEFabcdef123456/';=-字符串中,有大写字母6个,有小写字母6个,有数字6个,其他字符5个
    

String类的转换功能(必须掌握)


  • byte[] getBytes():把字符串转换为字节数组

      public static void main (String[] args) {
      	String s1 = "abc";
      	byte[] arr = s1.getBytes();
      	for(int i = 0; i < arr.length; i ++) {
      		System.out.println(arr[i] + " ");
      	}
    
      	System.out.println();
      
      	String s2 = "你好你好";    
      	byte[] arr2 = s2.getBytes();     //通过gbk码表将字符串转换成字节数
      	for(int i = 0;i < arr2.length; i ++) {    //编码:把我们看得懂转换成计算机看的懂的
      		System.out.println(arr2[i] + " ");   //gbk码表一个中文代表两个字节
      											//gbk码表特点,中文的第一个字节肯定是负数
      	}
      
      }
    
      输出结果:97 98 99
      		 -60 -29 -70 -61 -60 -29 -70 -61
    
  • char [] toCharArray():把字符串转换为字符数组

      public static void main(String[] args) {
      	String s = "heima";
      	char[] arr = s.toCharArray();   //将字符串转换为字符数组
    
      	for (int i = 0 ; i < arr.length; i ++) {
      		System.out.print(arr[i] + "  ");
      	}
      }	
    
      输出结果:h  e  i  m  a  
    
  • static String valueOf(char[] chs):把字符数组转换成字符串

  • static String valueOf(int i):把int类型的数据转换成字符串

    • 注意:String类的valueOf方法可以把任意类型的数据转换成字符串

      	public static void main(String[] args) {
      		char[] arr = {'a','b','c'};
      		String s = String.valueOf(arr);      
      		System.out.println(s);
      		
      		String s2 = String.valueOf(100);
      		System.out.println(s2 + 100);           //将100转换为字符串
      	}	
      
  • string toLowerCase():把字符串转成小写

  • String toUpperCase():把字符串转成大写

  • String concat(String str):把字符串拼接

      	public static void main(String[] args) {
      		String s1 = "heiMA";
      		String s2 = "chengxuYUAN";
      		String s3 = s1.toLowerCase();
      		String s4 = s2.toUpperCase();
    
      		System.out.println(s3);
      		System.out.println(s4);
    
      		System.out.println(s3 + s3);   //用+拼接字符串更强大,可以用字符串与任意类型相加
      		System.out.println(s3.concat(s4));   //concat方法调用和传入的都必须是字符串
      	}
    
      	输出结果:heima
      			 CHENGXUYUAN
      			 heimaCHENGXUYUAN
      			 heimaCHENGXUYUAN
    

按要求转换字符案例(链式编程)


  • 要求:把一个字符串的首字母转成大写,其余为小写

      	public static void main(String[] args) {
      		String s = "woaiHEIma";
      		String s2 = s.substring(0,1).toUpperCase().concat(s.substring(1).toLowerCase());
      				//从0到1截取字符串中的w
      		System.out.println(s2);
      	}
      	输出结果:Woaiheima
    
  • 把数组中的数据按照指定格式拼接成一个字符串

      public static void main(String[] args) {
      	int [] arr = {1,2,3};
      	String s = "[";
    
      	for(int i = 0; i < arr.length; i ++) {
      		if(i == arr.length - 1) {
      			s = s + arr[i] + "]";
      		}else {
      			s = s + arr[i] + ",";
      		}
      	}
    
      	System.out.println(s);
      }
      
      输出结果:[1,2,3]
    

String类的其他功能


  • String的替换功能及案例演示

    • String replace(char old,char new)

    • String replace (String old,String new)

      	String s = "heima";
      	String s2 = s.replace('i','o');
      	System.out.println(s2);
      
      	String s3 = s.replace('z','o');  //字符不存在就不替换,原样输出
      	System.out.println(s3);
      
      	String s4 = s.replace("ei","ao");
      	System.out.println(s4);
      
      	输出结果:heoma
      			 heima
      			 haoma
      
  • String的去除字符串两空格及案例演示

    • String trim()

      	String s = "  hei  ma  ";
      	String s2 = s.trim();
      	System.out.println(s2);
      
      	输出结果:hei  ma
      	应用:在用户输入密码的时候,对字符串两端的空格进行处理
      
  • String的按字典顺序比较两个字符串及案例演示

    • int compareTo(String str)

    • int compareToIgnoreCase(String str)

      	String s1 = "abc";
      	String s2 = "bcd";
      
      	int num = s1.compareTo(s2);
      	System.out.println(num);
      
      	输出结果:-1
      	解释:a对应的是97,b对应的是98   97-98=-1
      
      	/*******************************************/
      
      	String s1 = "a";
      	String s2 = "aaaa";
      
      	int num = s1.compareTo(s2);
      	System.out.println(num);
      
      	输出结果:-3
      	解释:如果字符都一样就按照长度来比较
      
      	/*******************************************/
      
      	String s3 = "黑";
      	String s4 = "马";
      	int num2 = s3.compareTo(s4);
      	System.out.println('黑' + 0);   //查找的是Unicode码表值
      	System.out.println('马' + 0);
      	System.out.println(num2);
      
      	输出结果:40657
      			 39532
      			 1125
      	解释:查找Unicode的码表值,40657-39532=1125
      
      	/*******************************************/			
      
      	String s5 = "heima";
      	String s6 = "HEIMA";
      	int num3 = s5.compareTo(s6);
      	System.out.println(num3);
      
      	int num4 = s5.compareToIgnoreCase(s6);
      	System.out.println(num4);
      
      	输出结果:32
      			 0
      

字符反转例题


		import java.util.Scanner;
		public class Test {
			public static void main(String[] args) {
				Scanner sc = new Scanner(System.in);  //创建键盘录入对象
				System.out.println("请输入一个字符串:");
				String line = sc.nextLine();     //将键盘录入的字符串存在line中
				
				char[] arr = line.toCharArray();   //将字符串转换为字符数组

				String s = "";
				for(int i = arr.length - 1;i >= 0; i--) {        //倒着遍历字符数组
					s = s + arr[i]; 		//拼接成字符串
				}
			
				System.out.println(s);
			}
		}

		运行结果:请输入一个字符串:
				 abc
				 cba

统计大串中小串出现的次数


public static void main(String[] args) {
	String max = "woaiheima,heimabutongyubaima,wulunheimahaishibaima,zhaodaogongzuojiushihaoma";   //定义大串
	String min = "heima";   //定义小串

	int count = 0;   //定义计数器变量
	int index = 0;   //定义索引
	//定义循环,判断小串是否在大串中出现
	while((index = max.indexOf(min)) != -1) {
		count++;
		max = max.substring(index + min.length());
	}

	System.out.println(count);
}

输出结果:3

附加:
String s1 = "hello";
String s2 = "hello";
System.out.println(s1 == s2);
输出:true
原理:这样创建出来的String是常量,会把数据放在常量池中。
	当s1创建后,会把hello放在常量池中
	当s2创建后,s2会现在常量池中寻找是否有hello这个数据,有就直接指向它,没有就写入新的数据(没有地址)
		
String s1 = new String ("my String");
String s2 = new String ("my String");
System.out.println(s1 == s2);
输出:false
原理:这样相当于创建了两个对象,对象在堆里,所以两者指向的对象都不一样,即地址不一样。

posted @ 2021-03-27 23:27  言非  阅读(89)  评论(0编辑  收藏  举报