4.28

来源:互联网 发布:傲剑修行升级数据 编辑:程序博客网 时间:2024/06/05 02:20

JAVA

String 类及其重要方法

字符与字符串

字符是JAVA 语言中的一种基本数据类型,即使用单引号括起来的单个字符。字符串常量是用双引号括起来的多个字符

JAVA语言中,字符串对象一旦被创建,它的值就永远不能再改变,


String 类

String()      初始化一个新创建的String对象,它表示一个空字符序列

String(byte[] byte)        构造一个新的String对象,方法是使用平台的默认字符集解码字节的指定数组

String(byte[] byte,String charsetName)      构造一个新的String对象,方法是使用指定的字符集解码指定的字节数组

String(char[] value)       创建一个新的String 对象,表示当前字符数组参数中包含的字符序列

String(StringBuffer buffer)  创建一个新的字符串,包含当前在字符串缓冲区参数中的字符序列


字符串对象的创建

String str="This is a String";

或者是

String str=new String("This is a String");


上述两条语句都是创建一个内容为 This is a String 的字符串对象,但是   不是指向同一个对象的引用,


public class StringTest1 {
        public static void main(String args[]) {
                String s1="aaaa";
                String s2=new String("aaaa");
 
                if(s1==s2) {
                      System.out.println("s1,s2是同一个对象! ");
                } else {
                      System.out.println("s1,s2不是同一个对象! ");
                }
        }
}

该示例首先声明了两个具有相同内容的字符串对象,然后通过if 语句判断这两个对象是否相等:

编译运行后得到:

F:\gz\JAVA\zfc>java StringTest1
s1,s2不是同一个对象!

可以看出比较两个引用返回的是 false, 即 s1 和s2 两个字符串对象虽然都相同,但是没有指向同一个对象。这里产生了两个字符串对象,所以等号只能用于比较两个引用是否指向同一个对象。


如果要比较两个对象的内容是否相同,则应该使用 equals() 方法

public class StringTest2 {
        public static void main(String args[]) {
                String s1="aaaa";
                String s2=new String("aaaa");
 
                if(s1.equals(s2)) {
                      System.out.println("s1,s2中的内容相同! ");
                } else {
                      System.out.println("s1,s2中的内容不相同! ");
                }
                if(s2.equals(s1)) {
                      System.out.println("s1,s2中的内容相同! ");
                } else {
                      System.out.println("s1,s2中的内容不相同! ");
                }
        }
}

首先声明两个具有相同内容的字符串对象,然后通过if 语句判断这两个对象是否相等,不同的是,使用的是 equals()方法而不是等号

编译运行后得到结果:

F:\gz\JAVA\zfc>java StringTest2
s1,s2中的内容相同!
s1,s2中的内容相同!


①equals() 方法只能用于比较对象

②equals() 方法的返回值非 true 即 false.


字符串对象一旦被创建将永远不能被改变:

public class StringTest3 {
        public static void main(String args[]) {
                //创建字符串对象
                String s="StringObject";
                //将所有字母都转换为大写
                s.toUpperCase();
                //打印s中的字符串内容
                System.out.println("s="+s);
        }
}

声明一个字符串对象,并调用对象上的 toUpperCase()方法将字母转换为大写,并打印字符串内容

编译运行结果如下:

F:\gz\JAVA\zfc>java StringTest3
s=StringObject


String 类的主要方法

① startsWith() 和 endsWith()方法

测试字符串是否以指定的前缀开始或结束


②public Boolean regionMatches(int firstStart,String other,int ortherStart,int length)方法

从当前字符串参数firstStart 指定的位置开始,取长度为 length 的一个子串,并将这个子串和参数other 指定的一个子串进行比较。其中 other 指定的子串是从参数 otherStart指定的位置开始,从 other 中取长度为 length 的一个子串,如果相同,就返回 true   否则返回 false


③public int indexOf(String s)方法

字符串调用该方法从当前字符串的头开始检索(从0开始计算)字符串s,并返回首次出现s 的位置,如果没有检索到字符串s ,该方法的返回值是-1


④public String substring(int start,int end)方法

字符串对象调用该方法获得一个当前字符串的子串,该子串是从当前字符串的star 处截取到end 处所得到的字符串,但不包括end 处所对应的字符


⑤public String concat(String str)方法

将指定字符串连到调用此方法的字符串对象的结尾


⑥public String replace(char oldChar,Char newChar)方法

返回一个新的字符串,通过用newChar 替换调此 replace()方法的字符串中出现的所有 oldChar而生成的


⑦public int length()方法

返回此字符串的长度,长度等于字符串中的16位Unicode字符数


⑧public boolean matches(String regex)方法

通知此字符串是否匹配给定的正则表达式。此方法调用的 str.matches(regex)形式与 Pattern.matches(regex,str)表达式产生完全相同的结果


⑨public String[] split(String regex)方法

根据给定的字符来拆分此字符串

//以逗号分隔

String s1="St,rin,g0b,je,ct;

//调用split(String regex方法

String[] s2=s1.split(",");

//循环打印

for(String ss:s2) {

        System.out.println(ss);

}

程序的运行结果为:

St

rin

g0b

je

ct


⑩public String toUpperCase()方法

使用默认语言环境的规则,将此字符串中的所有字符都转换成大写


⑪public String toLowerCase()方法

使用默认语音环境的规则,将此字符串中的所有字符都转换为小写


⑫public String trim()方法

返回字符串的副本,忽略前导空白和尾部空白:

String s=" StringObject ";

System.out.println(s.trim());

程序的运行结果为:

StringObject


StringBuffer 类及其方法

StringBuffer 类用于创建和操作动态字符串信息,为该对象分配的内存会自动扩展以容纳新增的文本,从而避免了进行字符串连接操作时产生的垃圾对象

StringBuffer()      构造一个空的StringBuffer对象,初始容量为16个字符

StringBuffer(int capacity)        构造一个指定容量的StringBuffer对象

StringBuffer(String str)      构造一个初始值为  str  的StringBuffer对象

该类位于java.lang 基础包中,所以可以直接在程序中使用。一旦创建了StringBuffer类的对象,就可以使用StringBuffer 类提供的大量方法对字符串进行操作

最常用的为 append()方法

该方法将字符串文本追加到当前StringBuffer对象内容的结尾,但不会产生新的字符串对象


public class StringBufferTest {
        public static void main(String args[]) {
                StringBuffer strBuf=new StringBuffer();
                strBuf.append("67890");
                System.out.println("\""+ strBuf +"\" length: "+
strBuf.length()+",capacity: "+strBuf.capacity());
                strBuf.insert(0,"12345");
                System.out.println("\""+ strBuf +"\" length: "+
strBuf.length()+",capacity: "+strBuf.capacity());
                strBuf.append("1234567");
                System.out.println("\""+ strBuf +"\" length: "+
strBuf.length()+",capacity: "+strBuf.capacity());
        }
}

以上代码声明了一个StringBuffer类的对象,然后调用append()方法并打印StringBuffer类的对象的内同

编译运行后得到结果:

F:\gz\JAVA\zfc>java StringBufferTest
"67890" length: 5,capacity: 16
"1234567890" length: 10,capacity: 16
"12345678901234567" length: 17,capacity: 34


StringBuffer 类的主要方法

通过StringBuffer对象的delete()方法可以对字符串进行截取操作:

public class StringBufferTest1 {
        public static void main(String args[]) {
                String str="this si original string";
                String toDelete=" original";
                System.out.println("Original string: \""+str+"\"");
                System.out.println("String to be deleted: \""+toDelete+"\"");
                int index=str.indexOf(toDelete);
                if(index==-1) {
                      System.out.println("No string matched!");
                } else {
                      StringBuffer bufStr=new StringBuffer(str);
                      bufStr.delete(index,index+toDelete.length() );
                      str=bufStr.toString();
                      System.out.println("New string: \""+str+"\"");
                }
        }
}

编译运行结果为:

F:\gz\JAVA\zfc>java StringBufferTest1
Original string: "this si original string"
String to be deleted: " original"
New string: "this si string"


Pattern类和 matcher 类

①static Pattern compile(String regex)     按照模式编译给出的正则表达式的字符串

②static boolean matches(String regex,CharSequence input)    编译给定的正则表达式,并且对输入的字串以该正则表达式为模开展匹配,该方法适合于该正则表达式只使用一次的情况,也就是只进行一次匹配,因为这种情况下并不需要生成一个Matcher实例

③String[] split(CharSequence nput)     将目标字符串以模式里所包含的正则表达式为模进行分割

Pattern 类是一个正则表达式经编译后的表现模式:

String re="[a-z]";

Pattern pattern=Pattern.compile(re);

对以上三个方法举例说明:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternTest {
        public static void main(String args[]) {//匹配小写字母
                Pattern pattern = Pattern.compile("[a-z]+");
                 String[] arrStr=pattern.split("Matcher,Pattern,String,System");
                 int length=arrStr.length;
                 for(int i=0;i<length;i++) {
                     System.out.println(arrStr[i]);
                }
                System.out.println("======");
               Matcher matcher = pattern.matcher("sun.java.com");
               while (matcher.find()) {
                       System.out.println("Match: "+matcher.group());
                }
        }
}

输出结果如下:

F:\gz\JAVA\zfc>java PatternTest
M
,P
,S
,S
======
Match: sun
Match: java
Match: com



import java.util.regex.*;
public class PatternTest1 {
        public static void main(String args[]) {
                Pattern patt = Pattern.compile("(\\w+)\\s(\\d+)");
                Matcher matcher = patt.matcher("Orang 12");
                matcher.lookingAt();
                System.out.println("Name: " + matcher.group(1));
                System.out.println("Number: " + matcher.group(2));
        }
}

结果:

F:\gz\JAVA\zfc>java PatternTest1
Name: Orang
Number: 12


JAVA 数学运算

java.lang.Math 类

java.lang 包中定义的所有类都是 Java语言的基础,在程序中不用显示的声明导入,

Math类也包含在此包中,它用于执行基本的数学运算。

java.lang 包中也定义了两个数学常量PI 和E:

public static final double E

public static final double PI


public class MathPIE {
        public static void main(String args[]) {
                System.out.println("E = " + Math.E);
                double radians = 0.45 * Math.PI/180;
                System.out.println("cos = " + Math.cos(radians));
        }
}

运行结果:

F:\gz\JAVA\zfc>java MathPIE
E = 2.718281828459045
cos = 0.9999691576447897


Math类中的所有方法都是静态的,因此在使用的时候不必创建实例,而且也不能创建对象,因为其构造函数是私有的,任何类也不能扩展自 Math 类,因为它是最终类

①abs():此方法用于返回某个数字的绝对值,参数可以是 float,double,long.或int.如果是byte或short类型,则会被强制转换成int类型

②ceil():用于返回下一个最大整数,

③floor():返回紧邻的最小整数。作用与ceil()方法相反,返回的是比参数小的整数,而且都是双精度型。

④max():返回两个值中的最大值,只支持 float double long int 类型的数据,不支持 byte   short  类型的数据

⑤min():返回两个值中的最小值

⑥random(): 返回一个随机数,一个在0.0到1.0之间的双精度数

⑦round():返回与某浮点数值最接近的整数值,参数可以为 double 和 folat 两种,并且支持四舍五入

⑧sqrt():返回某数值的平方根。如果该参数是“非数字”类型(NaN),或者小于零,则返回值为NaN

⑨toDegrees():返回用度数表示的角度值

⑩toRadians():返回用弧度表示的角度值

java.math.BigInteger 类
常用方法:
public BigInteger add(BigInteger val)      将两个数相加并返回结果
public BigInteger subtract(BigInteger val)   将两个数相减并返回结果
public BigInteger multiply(BigInteger val)    将两个数相乘并返回结果
public BigInteger divide(BigInteger val)     将两个数相除并返回结果
public int signum()      返回某个数的符号
public BigInteger negate()     有取相反数的功能,则返回其自身的相反数
public BigInteger mod(BigInteger m)      求余数并返回结果
public int compareTo(BigInteger val)    比较两个数

import java.math.*;
public class BigIntegerTest {
        public static void main(String args[]) {
                BigInteger bi1=new BigInteger("112233");
                BigInteger bi2=new BigInteger("445566");
                //加法
                System.out.println("bi1+bi2= "+bi1.add(bi2));
                //减法
                System.out.println("bi1-bi2= "+bi2.subtract(bi1));
                //乘法
                System.out.println("bi1*bi2= "+bi1.multiply(bi2));
                //除法
                System.out.println("bi1/bi2= "+bi2.divide(bi1));
                //求余
                System.out.println("bi1%bi2= "+bi2.mod(bi1));
                //判断bi3的符号
                BigInteger bi3=bi1.subtract(bi2);
                if(bi3.signum()==1) {
                      System.out.println("bi3为整数! ");
                } else {
                      System.out.println("bi3为负数! ");
                }
        }
}
运行结果为:
F:\gz\JAVA\zfc>java BigIntegerTest
bi1+bi2= 557799
bi1-bi2= 333333
bi1*bi2= 50007208878
bi1/bi2= 3
bi1%bi2= 108867
bi3为负数!

java.math.BigDecimal 类
常用构造函数
BigDecimal(BigInteger)       把一个BigInteger值构造成 BigDecimal对象
BigDecimal(double)        把一个double 值构造成BigDecimal对象

常用方法
public BigDecimal add(BigDecimal augend)       将两个数相加并返回结果
public BigDecimal subtract(BigDecimal b)         将两个数相减并返回结果
public BigDecimal multiply(BigDecimal m)        将两个数相乘并返回结果
public BigDecimal divide(BigDcimal divisor)      将两个数相除并返回结果
public int signum()          返回某个数的符号
public BigDecimal negate()         有取相反数的功能,返回其自身的相反数
public int compareTo(BigDecimal val)        比较两个数


import java.math.*;
public class BigDecimalTest {
        public static void main(String args[]) {
                BigDecimal bd1=new BigDecimal("8.8");
                BigDecimal bd2=new BigDecimal("0.6");
                //加法
                System.out.println("bd1+bd2= "+bd1.add(bd2));
                //减法
                System.out.println("bd1-bd2= "+bd2.subtract(bd1));
                //乘法
                System.out.println("bd1*bd2= "+bd1.multiply(bd2));
                //除法
                System.out.println("bd1/bd2= "+bd2.divide(bd1,BigDecimal.ROUND_HALF_DOWN));
                bd2=new BigDecimal("8.80");
                if(bd1.compareTo(bd2)==0) {
                       System.out.println("bd1和bd2相等! ");
                }
        }
}

编译运行后得到一下结果:
F:\gz\JAVA\zfc>java BigDecimalTest
bd1+bd2= 9.4
bd1-bd2= -8.2
bd1*bd2= 5.28
bd1/bd2= 0.1
bd1和bd2相等!




LINUX
Apache   服务器管理命令
apachectl: Apache httpd 服务器控制接口,帮助管理员控制 Apache httpd 后台守护进程的功能

ab: Apache httpd 服务器性能测试工具,描绘当前所安装的Apache 的执行性能,主要显示每秒可以处理多少个请求

httpd: Apache 超文本传输协议服务器

htpasswd: 管理用于基本认证的用户文件

dbmmanage  管理dbm 用法的用户认证文件

htdigest  管理用于摘要认证的用户文件

htcacheclean  清理磁盘缓冲区

日志管理命令
logresolve  解析Apache 日志中的IP地址为主机名

rotatelogs   滚动Apache 日志的管道日志程序


DNS 服务器管理命令
named    域名服务器管理命令

rndc: DNS服务器控制,用于系统管理员控制Bind的运行,替换了Bind8 中的ndc 命令工具

named-checkcong :  检查DNS 配置

named-checkzone:   检查区域文件的合法性

dig:  发送域名查询信息包到域名服务器

nslookup:  交互式查询名称服务器,用于验证与 DNS 名称服务器的通信

host:  使用域名服务器查询主机名字

0 0