Java从入门到精通——基础篇之Java 常用类

一、   String类

(一)        方法

a)    String(String original):创建一个String对象为original的拷贝

b)   String(char[] value):用一个字符数组创建一个String对象

c)    String(char[] value,int offset,int cout):用一个字符数组从offset项开始的count个字符序列创建一个String对象

d)   Public char charAt(int index):返回字符串中第index个字符

e)    public int length():返回字符串长度

f)     public int indexOf(String str):返回字符串中出现str的第一个位置

g)   public int indexOf(String str,int fromIndex):返回自趺床中从fromIndex开始出现str的第一个位置

h)   public boolean equalsIgnoreCase(String another):比较字符串与another是否一样

i)      public String replace(char oldChar, char newChar):在字符串中用newChar字符替换oldChar字符

j)      public boolean startsWith(String prefix):判断字符串是否以prefix字符串开头

k)    public boolean endsWith(String suffix):判断字符串是否以suffix字符串结尾

l)      public String toUpperCase():返回一个字符串为该字符串的大写形式

m) public String toLowerCase():返回一个字符串为该字符串的小写形式

n)   public String substring(int beginIndex):返回字符串从beginIndex开始到结尾的字符串

o)   public String substring(int beginIndex,int endIndex):返回该字符串从beginIndex开始到endIndex结尾的字符串

p)   public String trim():返回将该字符串去掉开头和结尾空格后的字符串

(二)        实例

 

1.public class Test{  
 public static void main(String[] args){  
 String s1="hello";  
 String s2="world";  
 String s3="hello";  
 System.out.println(s1==s3);  
 s1=new String("hello");  
 s2=new String("hello");  
 System.out.println(s1==s2);  
 System.out.println(s1.equals(s2));  
 char c[]={'s','u','n',' ','j','a','v','a'};  
 String s4=new String(c);  
 String s5=new String(c,4,4);  
 System.out.println(s4);  
 System.out.println(s5);  
 }  
 }  
 

效果:

2. public class Test2{  
 public static void main(String[] args){  
 String s1="sun java";  
 String s2="Sun Java";  
 System.out.println(s1.charAt(1));  
 System.out.println(s2.length());  
 System.out.println(s1.indexOf("java"));  
 System.out.println(s1.indexOf("Java"));  
 System.out.println(s1.equals(s2));  
 System.out.println(s1.equalsIgnoreCase(s2));  
 String s="我是程序员,我在学习java";  
 String sr=s.replace('我','你');  
 System.out.println(sr);  
 }  
 }  
 

效果:



3. public class Test3{  
 public static void main(String[] args){  
 String s="Welcome to Java World!";  
 String s1=" sun java ";  
 System.out.println(s.startsWith("Welcome"));  
 System.out.println(s.endsWith("World"));  
 String sL=s.toLowerCase();  
 String sU=s.toUpperCase();  
 System.out.println(sL);  
 System.out.println(sU);  
 String subS=s.substring(11);  
 System.out.println(subS);  
 String sp=s1.trim();  
 System.out.println(sp);  
 }  
 }  
 


效果:

一、   StringBuffer类

(一)方法

a)StringBuffer():创建一个不包含字符序列的“StringBuffer”对象

b)StringBuffer(String str):创建一个StringBuffer对象,包含与String对象str相同的字符串序列。

(二)实例

public class Test{  
 public static void main(String[] args){  
 Integer i=new Integer(100);  
 Double d=new Double("123.456");  
 int j=i.intValue()+d.intValue();  
 float f=i.floatValue()+d.floatValue();  
 System.out.println(j);  
 System.out.println(f);  
 double pi=Double.parseDouble("3.1415926");  
 //转换为Double类型的数  
 double r=Double.valueOf("2.0").doubleValue();  
 double s=pi*r*r;  
 System.out.println(s);  
 try{  
 int k=Integer.parseInt("1.25");  
 }catch(NumberFormatException e){  
 System.out.println("数据格式不对!");  
 }  
 System.out.println(Integer.toBinaryString(123)+"B");//二进制  
 System.out.println(Integer.toHexString(123)+"H");//十六进制  
 System.out.println(Integer.toOctalString(123)+"0");  
 }  
 }  
 


效果:

一、Math类

(一) 

abs绝对值

aqrt平方根

pow(double a,double b)a的b次幂

log自然对数

exp e为底的指数

二、File类

(一)方法

public File(String pathname):以pathname为路径创建File对象。

Public File(String parent,String child)以parent为父路径,child为子路径创建File对象

 

原文地址:https://www.cnblogs.com/iplus/p/4490431.html