java常用类的使用、String类、StringBuffer类、正则表达式、基本数据类型的包装类、Object类、Math类

2022-07-29,,,,

一、String类

常用方法介绍及示例如下,方法描述见注释:

package PackageA; public class String_excess { public static void main(String[] args) { // 初始化训练 char a[]= {'这','是','一','个','字','符','串'}; String s3=new String(a); String s4=new String(a,3,2); String s5; s5=new String(s3); System.out.println(s3); System.out.println(s4); System.out.println(s5); // 返回索引处的字符值  char c1=s3.charAt(3); System.out.println(c1); // 比较两个字符串 int int1=s3.compareTo(s4); System.out.println(int1); int int2=s4.compareTo(s3); System.out.println(int2); // 忽略大小写,比较两个字符串 int int3=s3.compareToIgnoreCase(s4); System.out.println(int3); // 将指定字符串连接到此字符串末尾 String s1=s3.concat(s4); System.out.println(s1); // 判断字符串是否以指定字符串结尾 boolean boolean1=s3.endsWith(s4); System.out.println(boolean1); boolean boolean2=s3.endsWith("串"); System.out.println(boolean2); // 判断字符串是否相等 boolean boolean3=s3.contentEquals(s3); System.out.println(boolean3); // 不考虑大小写,判断是否相等 boolean boolean4=s3.equalsIgnoreCase(s3); System.out.println(boolean4); // 判断字符在字符串中第一次出现的索引 int int4=s3.indexOf("是"); System.out.println(int4); // 与上一个类似,这次是判断字符串 int int5=s3.indexOf(s4); System.out.println(int5); // 返回字符串的长度 int int6=s3.length(); System.out.println(int6); // 代替子串 System.out.println(s3); System.out.println(s4); String s2=s3.replace(s4, "|嘿嘿嘿,我被代替了。"); System.out.println(s2); // 根据给定的正则表达式的匹配,来拆分子串 // 注意:当出现转义字符的时候,需要加\\ String s6="a,bc,de"; String ss1[]=s6.split(","); for(int i=0;i<ss1.length;++i) { System.out.println(ss1[i]); } // 返回子串(类似切分) String s7=s6.substring(3); System.out.println(s7); String s8=s6.substring(3,6); System.out.println(s8); // 大小写转化 String s9="ABCDEFghijkl"; String s10=s9.toLowerCase(); String s11=s9.toUpperCase(); System.out.println(s10); System.out.println(s11); // 返回字符串副本,忽略前后的空格 String s12=" ifhehgie   "; System.out.println(s12); String s13=s12.trim(); System.out.println(s13); // 返回返回参数的字符串表达形式 String s14=String.valueOf(3.414); System.out.println(s14); } } 

二、StringBuffer类

package PackageA; public class Encrypt { // 加密 public static void encrypt(StringBuffer s, int n) { for (int i = 0; i < s.length(); ++i) { char ch = s.charAt(i); char new_ch = ch; if (ch >= 'a' && ch <= 'z') { new_ch = (char) ('a' + (ch - 'a' + n) % 26); } else if (ch >= 'A' && ch <= 'Z') { new_ch = (char) ('A' + (ch - 'A' + n) % 26); } s.setCharAt(i, new_ch); } } // 解密 public static void decrypt(StringBuffer s, int n) { for (int i = 0; i < s.length(); ++i) { char ch = s.charAt(i); char new_ch = ch; if (ch >= 'a' && ch <= 'z') { // 注意在此处加一个26,否则会出错。 new_ch = (char) ('a' + (ch - 'a' - n + 26) % 26); } else if (ch >= 'A' && ch <= 'Z') { new_ch = (char) ('A' + (ch - 'A' - n + 26) % 26); } s.setCharAt(i, new_ch); } } public static void main(String[] args) { StringBuffer s = new StringBuffer("xyZABCdefGH"); System.out.println(s); Encrypt.encrypt(s, 2); System.out.println(s); decrypt(s, 2); System.out.println(s); // 添加操作 s.append('c'); System.out.println(s); // 删除操作,注意,删除的是左闭右开的一个区间 s.delete(2, 3); System.out.println(s); // 插入 s.insert(1, "heihei"); System.out.println(s); // 替换, 注意,依然是左闭右开区间 s.replace(1, 4, "555"); System.out.println(s); // 反转序列 s.reverse(); System.out.println(s); } } 

三、正则表达式

正则表达式真难啊,学了好久,终于初有所出成。

几个不常用的非打印转义字符对应的转义序列

\cx ——匹配由x指明的控制字符。
(例如:\xM匹配一个Ctrl-M或者回车符)
其中,x的值必须为A-Z或a-z之一,否则,将c视为一个转义的c字符。

\f ——换页符

\r ——回车符

\s ——匹配任何空白字符==[\f\n\r\t\v]

\S——匹配任何非空白字符

特殊字符

* ——零次或者多次匹配前子表达式
+——一…
? ——零次或一次…
.——匹配除换行符以外的任意单个字符
^ ——作为定位符时,可以匹配输入字符串开始的位置
$ ——。。。结束的位置
\b ——匹配一个字边界,即字与空格间的位置
\B——匹配非字边界
\d——匹配一个数字字符
\D——匹配一个非数字字符
\w ——匹配包括下换线的任意字符
\W——匹配不包括下换线的任意字符
\xn——例如匹配\x41——‘A’,十六进制转义值必须为确定的两个数字长
\num——对所获取匹配的引用
\可以匹配一到三位八进制数,并获取其转义字符。
<
> 匹配词的开始和结束

练习1

package try_a_package; import java.util.regex.Pattern; import java.util.regex.Matcher; public class F2020_9_28_1 { public static void main(String[] args) { String str="计算机:86.5,大学英语:79,数据结构:80,大学语文:80"; String regex="\\d+.{0,1}\\d+"; Pattern pattern=Pattern.compile(regex); // 注意相关包的导入 Matcher matcher=pattern.matcher(str); double s=0; int count=0; while(matcher.find()) { String t=matcher.group(); s=s+Double.parseDouble(t); ++count; } s=s/count; System.out.println(s); } } 

练习2

package try_a_package; import java.util.regex.Pattern; import java.util.regex.Matcher; public class Regex2 { public static void main(String[] args) { String str="18971612345,15087654321,15623302891,13156007988,qq273983336"; String regex="(13[0-9]|15[0-9]|18[0-9])\\d{8}"; Pattern pattern=Pattern.compile(regex); Matcher matcher=pattern.matcher(str); while(matcher.find()) { String t=matcher.group(); System.out.println(t); } } } 

练习3

package try_a_package; import java.util.regex.Pattern; import java.util.regex.Matcher; public class Regex3 { public static void main(String[] args) { //String str="abc@163.com.cn"; String str="273983336@qq.com"; String regex="^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"; Pattern pattern=Pattern.compile(regex); Matcher matcher=pattern.matcher(str); while(matcher.find()) { String s=matcher.group(); System.out.println(s); } } } 

四、基本数据类型的包装类

package try_a_package; import java.util.Scanner; public class Area_1 { public static void main(String[] args) { String line; Scanner sc=new Scanner(System.in); line=sc.nextLine(); String data[]=line.split(","); double a=Double.parseDouble(data[0]); double b=Double.parseDouble(data[1]); double c=Double.parseDouble(data[2]); double p=(a+b+c)/2; double area=Math.sqrt(p*(p-a)*(p-b)*(p-c)); System.out.println(area); } } 

五、Object类

package try_a_package; public class Sphere1 implements Cloneable { final static double PI=3.14159; private double r; public Sphere1(double r) { this.r=r; } public double getArea() { double s; s=4*PI*r*r; return s; } public void setR(double r) { this.r=r; } public double getVolume() { double v; v=4.0/3*PI*r*r*r; return v; } @Override public String toString() { return "球  r="+r+",s="+getArea()+",v="+getVolume(); } public static void main(String[] args) throws CloneNotSupportedException { Sphere1 b1=new Sphere1(2.0); System.out.println(b1); Sphere1 b2=(Sphere1)b1.clone(); System.out.println(b2); b2.setR(5); System.out.println(b2); System.out.println(b1); Sphere1 b3=new Sphere1(3.0),b4; b4=b3; System.out.println(b3); System.out.println(b4); b4.setR(8); System.out.println(b3); System.out.println(b4); } } 

六、Math类

几个实用但容易忘记的函数

static double atan2(double x,double y):将矩形坐标(x,y)转换成极坐标(r,theta)

static double ceil(double a): (接近负无穷)
static double floor(double a):( 接近正无穷)

static double cosh(double x) :返回x的双曲线余弦

static double exp(double a):

static long round(double a):返回最接近参数值的long
static int round(double a):返回最接近的int

static T signum(T d):返回参数的符号函数

练习

package try_a_package; public class Math_excess { public static void main(String[] args) { // double x=2,y=2; double x=1,y=2; double r,theta; System.out.println(x+" "+y); // 这里先传y再传x System.out.println(Math.atan2(y, x)); System.out.println(Math.PI/4); double a=-1.1; System.out.println(Math.ceil(a)); System.out.println(Math.floor(a)); System.out.println(Math.exp(1)); // cosh(double x):返回x的双曲线余弦 System.out.println(Math.round(1.5)); System.out.println(Math.round(-0.5)); // 估计是四舍五入了。 System.out.println(Math.signum(1.1)); System.out.println(Math.signum(-0.0001)); System.out.println(Math.signum(0)); } } 

本文地址:https://blog.csdn.net/qq_41563270/article/details/108851989

《java常用类的使用、String类、StringBuffer类、正则表达式、基本数据类型的包装类、Object类、Math类.doc》

下载本文的Word格式文档,以方便收藏与打印。