String s = new String("we are students");
String t = new String("we are students");
Syste.out.println(s);
。输出的是对象的实体,即字符序列we are students
。String tom = new String(s);
char a[] = {'J','a','v','a'};
String s = new String(a);
过程相当于
String s = new String("Java");
String(char a[],int startIndex,int count)
?char a[] = {'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
String s = new String(a,2,4);
相当于
?String s = new String("贰叁肆伍");
正则表达式
public String[] split(String regex)
使用参数指定的正则表达式regex做为分隔标记分解出其中的单词,并将分解出的单词存放在字符串数组中。例如,对于字符串str String str = "1949年10月1日是中华人民共和国成立的日子";
如果准备分解出全部由数字字符组成的单词,就必须用非数字字符串做为分隔标记,因此,可以使用正则表达式:String regex="\\D+";
做为分隔标记分解出str中的单词:String digitWord[]=str.split(regex);
那么,digitWord[0]、digitWord[1]和digitWord[2]就分别是“1949”、“10”和“1”。
scanner调用next()方法依次返回NBA中的单词,如果NBA最后一个单词已被next()方法返回,scanner调用hasNext()将返回false,否则返回true。
对于被解析的字符序列中的数字型的单词,比如618,168.98等,scanner可以用nextInt()或nextDouble()方法来代替next()方法,即scanner可以调用nextInt()或nextDouble()方法将数字型单词转化为int或double数据返回。
如果单词不是数字型单词,scanner调用nextInt()或nextDouble()方法将发生InputMismatchException异常,在处理异常时可以调用next()方法返回该非数字化单词。
Calendar calendar= Calendar.getInstance();
public final void set(int year,int month,int date)
public final void set(int year,int month,int date,int hour,int minute)
public final void set(int year,int month, int date, int hour, int minute,int second)
//其作用是将日历翻到任何一个时间
可以直接使用String类调用format方法对日期进行格式化。
Formatter类的format方法:
format(格式化模式, 日期列表)
按着“格式化模式”返回“日期列表”中所列各个日期中所含数据(年,月,日,小时等数据)的字符串表示。
Java已经将format方法做为了String类的静态方法,因此,程序可以直接使用String类调用format方法对日期进行格式化。
"日期:%ty-%tm-%td"
String s = String.format("%tY年%tm月%td日",new Date(),new Date(),new Date());
那么s
就是"2011年02月10日"
3.格式化同一日期
可以在“格式化模式”中使用“<”
,比如:"%ty-%<tm-%<td"
中的三个格式符将格式化同一日期,即含有“<”
的格式符和它前面的格式符格式同一个日期,例如:String s = String.format("%ty年%<tm月%<td日",new Date());
那么%<tm
和%<td
都格式化new Date()
,因此字符串s
就是:"11年02月10日"
。
如果想用特定地区的星期格式来表示日期中的星期,可以用format的重载方法:
format (Locale locale,格式化模式,日期列表);
其中的参数locale
是一个Locale类的实例,用于表示地域。
Locale类的static常量都是Locale对象,其中US是表示美国的static常量。
Math类在java.lang包中。Math类包含许多用来进行科学计算的类方法,这些方法可以直接通过类名调用。
另外,Math类还有两个静态常量,它们分别是:
E 2.7182828284590452354
和PI 3.14159265358979323846
。
以下是Math类的常用类方法:
public static long abs(double a)
返回a的绝对值。public static double max(double a,double b)
返回a、b的最大值。public static double min(double a,double b)
返回a、b的最小值。public static double random()
产生一个0到1之间的随机数(不包括0和1)。public static double pow(double a,double b)
返回a的b次幂。public static double sqrt(double a)
返回a的平方根。public static double log(double a)
返回a的对数。public static double sin(double a)
返回正弦值。public static double asin(double a)
返回反正弦值。public static double ceil(double a)
返回大于a的最小整数,并将该整数转化为double
型数据。public static double floor(double a)
返回小于a的最大整数,并将该整数转化为double
型数据。public static long round(double a)
返回值是(long)Math.floor(a+5),即所谓a的“四舍五入”后的值。java.math包中的BigInteger类提供任意精度的整数运算。可以使用构造方法:
public BigInteger(String val)
构造一个十进制的BigInteger对象。
以下是BigInteger类的常用类方法:
public BigInteger add(BigInteger val)
返回当前大整数对象与参数指定的大整数对象的和。public BigInteger subtract(BigInteger val)
返回当前大整数对象与参数指定的大整数对象的差。public BigInteger multiply(BigInteger val)
返回当前大整数对象与参数指定的大整数对象的积。public BigInteger divide(BigInteger val)
返回当前大整数对象与参数指定的大整数对象的商。public BigInteger remainder(BigInteger val)
返回当前大整数对象与参数指定的大整数对象的余。public int compareTo(BigInteger val)
返回当前大整数对象与参数指定的大整数的比较结果,返回值是1、-1或0,分别表示当前大整数对象大于、小于或等于参数指定的大整数。public BigInteger pow(int a)
返回当前大整数对象的a次幂。public String toString()
返回当前大整数对象十进制的字符串表示。public String toString(int p)
返回当前大整数对象p进制的字符串表示。Random类的如下构造方法:
public Random();
public Random(long seed);
第一个构造方法使用当前及其实践作为种子创建一个Random对象,第二个构造方法使用参数seed
指定的种子创建一个Random对象。
随机数生成器random调用不带参数的nextInt()
方法:
Random random=new Random();
random.nextInt();
返回一个0至n之间(包括0,但不包括n)的随机数
随机数生成器random调用带参数的nextInt(int m)
方法(参数m必须取正整数值)
Formatter类提供了一个和C语言printf函数类似的format方法:format(格式化模式,值列表)
该方法按着“格式化模式”返回“值列表”的字符串表示。
Java已经将format方法做为了String类的静态方法,因此,程序可以直接使用String类调用format方法对数字进行格式化。
"输出结果%d,%f,%d"
中的%d
和%f
是格式符号。String s = String.format(“%.2f”,3.141592);
那么s
就是“3.14”
.String s=format("%d元%0.3f公斤%d台",888,999.777666,123);
那么,s
就是"888元999.778公斤123台"
。3.格式化顺序:format方法默认按从左到右的顺序使用“格式化模式”中的格式符来格式化“值列表”中对应的值,而“格式化模式”中的普通字符保留原样。
例如,假设int型变量x
和double型变量y
的值分别是888
和3.1415926
,
那么对于String s = format("从左向右:%d,%.3f,%d",x,y,100);
字符串s就是:从左向右:888,3.142,100
- 注1:改变默认的顺序(从左向右)进行格式化,在格式符前面添加索引符号
index$
,例如,1$
表示“值列表”中的第1个,2$
表示“值列表”中的第2个,
对于:String s=String.format(“不是从左向右:%2$.3f,%3$d,%1$d”,x,y,100);
字符串s
就是:不是从左向右:3.142,100,888- 注2:如果准备在“格式化模式”中包含普通的
%
,在编写代码时需要连续键入两个%
,如:
String s=String.format("%d%%",89);
输字符串s是:"89%"
%d
、%o
、%x
和%X
String s=String.format("%8d",59);
s
就是:“ 59"
,其长度(s.length())
为8,即s
在59左面添加了6个空格字符。
String s=String.format(“%-8d”,59);
字符串s
就是:"59 "
,其长度(s.length())
为8。String s=String.format("%5d%5d%8d",59,60,90);
字符串s
就是:“59 60 90”
(长度为18)。注:如果实际数字的宽度大于格式中指定的宽度,就按数字的实际宽度进行格式化。
可以在宽度的前面增加前缀0,表示用数字0(不用空格)来填充宽度左面的富裕部分,例如:String s=String.format("%08d",12);
字符串s
就是:"00000012"
,其长度(s.length())
为8,即s
在12的左面添加了6个数字0。
float
,Float
,double
和Double
%f
,%e(%E)
,%g(%G)
和%a(%A)
格式符可格式化float、Float、double和Double:
%f
将值格式化为十进制浮点数,小数保留6位。%e(%E)
将值格式化为科学记数法的十进制的浮点数(%E在格式化时将其中的指数符号大写,例如5E10)。3.限制小数位数与数据的“宽度”
"%.nf"
可以限制小数的位数,其中的n是保留的小数位数,例如%.3f
将6.1256
格式化为"6.126"
(保留3位小数)。
规定宽度的一般格式为“%mf”
(在数字的左面增加空格),或“%-md”
(在数字的右面增加空格)。
注:如果实际数字的宽度大于格式中指定的宽度,就按数字的实际宽度进行格式化
className
指定的类)相关的Class对象:public static Class forName(String className) throws ClassNotFoundException
??className
指定的类相关的Class对象。如果类在某个包中,className
必须带有包名,例如, className="java.util.Date"
。public Object newInstance() throws Exception
方法就可以得到一个className
类的对象。newInstance()
实例化一个className类的对象时,className类必须有无参数的构造方法。如果希望在键盘输入一行文本,但不想让该文本回显,即不在命令行显示,那么就需要使用java.io
包中的Console类的对象来完成。
首先使用System类调用console()
方法返回一个Console类的一个对象,比如cons
:
Console cons = System.console();
然后,cons
调用readPassword()
方法读取用户在键盘输入的一行文本,并将文本以一个char
数组返回:
char[] passwd = cons.readPassword();
Pattern类和Matcher类在java.util.regex包中。
以下结合具体问题来讲解使用Pattern类和Matcher类的步骤。假设有字符串:
String input = "hello,good morning,this is a good idea"
我们想知道input
从哪个位置开始至哪个位置结束曾出现了字符串good
。
使用Pattern类和Matcher类检索字符串str
中的子字符串的步骤如下:
regex
做参数得到一个称为模式的Pattern类的实例pattern
:String regex = "good";
Pattern pattern = Pattern.compile(regex);
模式对象是对正则表达式的封装。Pattern类调用类方法compile(String regex)
返回一个模式对象,其中的参数regex是一个正则表达式, 称为模式对象使用的模式。
matcher(CharSequence input)
方法返回一个Matcher对象matcher
,称为匹配对象Matcher matcher = pattern.matcher(input);
matcher
可以使用下列方法寻找字符串input
中是否有和模式regex
匹配的子序列(regex
是创建模式对象pattern
时使用的正则表达式)。
public boolean find()
:寻找input
和regex
匹配的下一子序列,如果成功该方法返回true
,否则返回false
。public boolean matches()
:matcher
调用该方法判断input
是否完全和regex
匹配。public boolean lookingAt()
:matcher
调用该方法判断从input
的开始位置是否有和regex
匹配的子序列。public boolean find(int start)
: matcher
调用该方法判断input
从参数start
指定位置开始是否有和regex
匹配的子序列 。public String replaceAll(String replacement)
: matcher
调用该方法可以返回一个字符串,该字符串是通过把input
中与模式regex
匹配的子字符串全部替换为参数replacement
指定的字符串得到的.public String replaceFirst(String replacement)
: matcher
调用该方法可以返回一个字符串,该字符串是通过把input
中第1个与模式regex
匹配的子字符串替换为参数replacement
指定的字符串得到的。原文:https://www.cnblogs.com/wyf20175217/p/10681235.html