Java字符串及数组的操作

一、实验目的

  1. 熟悉字符串的书写要求和创建方法;

  2. 掌握字符串的连接、获取等常用操作;

  3. 熟悉正则表达式,能正确构造和运用正则表达式;

  4. 能正确使用字符串生成器。

  5. 熟悉一维数组的结构,能正确创建一维数组,能使用一维数组;

  6. 熟悉二维数组的结构,能正确创建二维数组,能使用二维数组

  7. 熟悉对数组的遍历、填充、排序、复制、查询等常用操作

二、实验内容与要求

Java统计单词个数、提取内容、分割字符串

“A regular expression, regex or regexp (sometimes called a rational expression) is, in theoretical computer science and formal language theory, a sequence of characters that define a search pattern. Usually this pattern is then used by string searching algorithms for "find" or "find and replace" operations on strings, or for input validation.”

1) 编程统计上段文字中的单词个数。

2) 编程提取括号中的内容

3) 统计计算文字中有多少个单词“a”(不区分大小写)

4) 分割单词,用字符串数组保存每个单词,对字符串数组进行排序。

public class shijian21 {
	@SuppressWarnings("unused")
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		String s=new String("A regular expression, regex or regexp (sometimes called a rational expression) is, in theoretical computer science and formal language theory, a sequence of characters that define a search pattern. Usually this pattern is then used by string searching algorithms for \"find\" or \"find and replace\" operations on strings, or for input validation.");
		//1)编程统计上段文字中的单词个数。
		int jishu1=0;
		int jishu2=0;
		int size1 = 0;
		int size2 = 0;
	    int biaozhi1=0;
	    int biaozhi2=0;
	    String[]shuzu = new String[52];
	    int j=0;
		for(int i = 0;i<s.length();i++) {
			if((s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z')) {
				jishu1++;
				while(i<s.length()&&((s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z'))){
					i++;
				}
			}
		}
		System.out.println("单词个数为jishu1="+jishu1);
		//2)编程提取括号中的内容
		for(int i = 0;i<s.length();i++) {
			if(s.charAt(i)=='(') {
				 size1=s.indexOf('(');
			}
			if(s.charAt(i)==')') {
			     size2=s.indexOf(')');
			}
		}
		System.out.print("括号中内容为");
		for(int i = size1+1;i<size2;i++) {
			System.out.print(s.charAt(i));
		}
		//3)统计计算文字中有多少个单词“a”(不区分大小写)
		for(int i = 0;i<s.length();i++) {
			if(s.charAt(i)=='a') {
				 jishu2++;
			}
		}
		System.out.println();
		System.out.println("统计计算文字中有   "+jishu2+" 个单词“a”(不区分大小写)");
		//4)分割单词,用字符串数组保存每个单词,对字符串数组进行排序。
		
		for(int i = 0;i<s.length();i++) {
			if((s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z')) {
				biaozhi1=i;
				while(i<s.length()&&((s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z'))){
					i++;
				}
				biaozhi2=i;
				
				if(biaozhi1!=biaozhi2) {
					shuzu[j]=s.substring(biaozhi1, biaozhi2);
					j++;
				}
			}
		}
		for(j=0;j<jishu1;j++) {
			System.out.println(shuzu[j]);
		}
		//对字符串数组进行排序
		for(int i = 1;i<jishu1;i++) {
			int biaozhi = 0;
	     	for(int j1=1;j1<=jishu1-i;j1++) {
	     		if(shuzu[j1].compareTo(shuzu[biaozhi])<0) {
	     			biaozhi=j1;
	     		}
	     	}
	     	String temp = shuzu[jishu1-i];
	     	shuzu[jishu1-i]=shuzu[biaozhi];
	     	shuzu[biaozhi]=temp;
		} 
		System.out.println("排序为");
		for(j=0;j<jishu1;j++) {
			System.out.println(shuzu[j]);
		}
	}
}

Java提取指定信息、正则表达式使用

“xxx大学的网站地址是http://www.cxxx.edu.cn,中国移动公司南充站首页地址是http://www.10086.cn/index/sc/index_280_817.htm,microsoft公司的中文网站是 https://www.microsoft.com/zh-cn/ 手机号232323,手机号1108976345,手机号2323232,手机号222222332。办公电话是0817-22222”

1) 请编程通过正则表达式将上面文字中的所有网站地址提取出来,保存到字符串数组中。

2) 提取文字中的手机号,判断是否是正确的手机号

public class shijian22 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		String regex1="\\p{Alpha}+\\p{Punct}+\\p{Lower}+(\\.+(\\w{2,20}+\\p{Punct}?)+)+";
		@SuppressWarnings("unused")
		String regex2="(\\p{Digit}+\\p{Punct}*)+";
		String str="xxx大学的网站地址是http://www.cxxx.edu.cn,中国移动公司南充站首页地址是http://www.10086.cn/index/sc/index_280_817.htm,microsoft公司的中文网站是 https://www.microsoft.com/zh-cn/  手机号232323,手机号1108976345,手机号2323232,手机号222222332。办公电话是0817-22222";
		int biaozhi1;
		int biaozhi2;
		int j=0;
		int jishu=0;
		String shuzu[]=new String[10]; 
		for(int i = 0;i<str.length();i++) {
			if((str.charAt(i)>='a'&&str.charAt(i)<='z')||(str.charAt(i)>='0'&&str.charAt(i)<='9')||((str.charAt(i))=='.')||((str.charAt(i))==':')||((str.charAt(i))=='/')||((str.charAt(i))=='_')||((str.charAt(i))=='-')){
				biaozhi1=i;
				while((i<str.length())&&((str.charAt(i)>='a'&&str.charAt(i)<='z')||(str.charAt(i)>='0'&&str.charAt(i)<='9')||((str.charAt(i))=='.')||((str.charAt(i))==':')||((str.charAt(i))=='/')||(str.charAt(i))=='_')||(str.charAt(i))=='-'){
					i++;
				}
				biaozhi2=i;			
				if(biaozhi1!=biaozhi2) {
					shuzu[j]=str.substring(biaozhi1, biaozhi2);
					System.out.println(shuzu[j]);
					j++;
				}
			}
		}
		System.out.println(j);
		System.out.println("合法网络地址");
		for(int z = 0;z<j;z++) {
			if(shuzu[z].matches(regex1)) {
				System.out.println(shuzu[z]+"是一个合法网络地址");
			}
		}
		System.out.println("电话号码");
		for(int z = 0;z<j;z++) {
			if(shuzu[z].matches(regex2)) {
				System.out.println(shuzu[z]+"是一个电话号码");
				for(int n = 0;n<shuzu[z].length();n++) {
					if(shuzu[z].charAt(n)>='0'&&shuzu[z].charAt(n)<='9'){
						jishu++;
						if(jishu==11){
							System.out.println(shuzu[z]+"是一个正确的电话号码");
						}
					}	
				}
				jishu=0;
			}
		}
	}
}

Java操作时间对象

2.编程输出当前日期时间,按如下格式输出:

1)年月日,星期,点分秒

2) 按照美国格式输出日期和星期几

import java.util.Date;
@SuppressWarnings("unused")
public class shijian23 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Date date=new Date();
		String year=String.format("%tY", date);
		String month=String.format("%tm", date);
		String day=String.format("%td", date);
		String xinqi=String.format("%tA", date);
		String dian=String.format("%tH", date);
		String fen=String.format("%tM", date);
		String miao=String.format("%tS", date);
		System.out.println("现在是:"+year+"年"+month+"月"+day+"日"+","+xinqi+","+dian+"点"+fen+"分"+miao+"秒");
		System.out.println("现在是:"+month+"月"+day+"日"+year+"年"+","+xinqi+","+dian+"点"+fen+"分"+miao+"秒");
	}

}

Java存储字符串

从键盘输入10句话,保存到字符串数组中。

import java.util.Scanner;
public class shijian24 {
	
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		@SuppressWarnings("resource")
		int i=0;
		@SuppressWarnings("unused")
		String a[]=new String[10];
		for(i=0;i<10;i++) {
			@SuppressWarnings("resource")
			Scanner scan=new Scanner(System.in);
			System.out.println();
			System.out.print("第"+i+"个=");
			a [i] = scan.next(); 
		}
		for(i=0;i<10;i++) {
			System.out.print(a[i]);
		}
	}
}

Java插入字符串

从键盘输入任意多个整数,然后对输入整数进行降序排序,输入任意一个数,判断该数是否在数组中,如果没有就插入到相应的位置,确保数组有序。

import java.util.Scanner;
public class shijan25{
	
	public static void main(String[] args) {
		
		@SuppressWarnings("unused")
		int j=0;
		// TODO 自动生成的方法存根
		@SuppressWarnings("resource")
		int i=0;
		@SuppressWarnings("unused")
		int temp;
		int biaozhi;
		@SuppressWarnings("unused")
		int b[]=new int[5];
		@SuppressWarnings("unused")
		int a[]=new int[4];
		for(i=0;i<4;i++) {
			@SuppressWarnings("resource")
			Scanner scan=new Scanner(System.in);
			System.out.print("第"+(i+1)+"个=");
			a [i] = scan.nextInt(); 
		}
		for(i=1;i<4;i++) {
			biaozhi=0;
	     	for(j=1;j<=4-i;j++) {
	     		if(a[j]<a[biaozhi]) {
	     			biaozhi=j;
	     		}
	     	}
	     	temp=a[4-i];
	     	a[4-i]=a[biaozhi];
	     	a[biaozhi]=temp;
		}
		for(int z:a) {
			System.out.print(">"+z);
		}
		System.out.println();
		@SuppressWarnings("resource")
		Scanner scan=new Scanner(System.in);
		System.out.println("qingshuru X=");
		int x=scan.nextInt();
		@SuppressWarnings("unused")
		int flag=0;
		for(int y=0;y<4;y++) {
			if(x==a[y]) {
				System.out.print("是在数组中");
				flag=1;
				break;
			}
		}
		if(flag!=1){
			System.out.print("不是在数组中");
			for(int y = 0;y<4;y++) {
				b[y]=a[y];
			}
			b[4]=x;
			for(i=1;i<5;i++) {
				biaozhi=0;
		     	for(j=1;j<=5-i;j++) {
		     		if(b[j]<b[biaozhi]) {
		     			biaozhi=j;
		     		}
		     	}
		     	temp=b[5-i];
		     	b[5-i]=b[biaozhi];
		     	b[biaozhi]=temp;
			}
			for(int z:b) {
				System.out.print(">"+z);
			}
		}
	}
}
posted @ 2026-01-06 21:53  东血  阅读(3)  评论(0)    收藏  举报

载入天数...载入时分秒...