⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 5.txt

📁 本书属于《开发专家之 Sun ONE》系列丛书
💻 TXT
字号:
例程5-1
class CompareString 
{
public static void main(String[] args) 
{
String str = "this is a test";
//不相同,r1为负值
int r1 = str.compareTo("this is a test and more");
//不相同,r2为负值,原因是’n’<’a’
int r2 = str.compareTo("this is not a test");
//相同,r3为零
int r3 = str.compareTo("this is a test");
//不相同,r4为正值,原因是't' > 'n'
int r4 = str.compareTo("no, this is not a test");
//不相同,r5为正值,原因是str长度小
int r5 = str.compareTo("this");
//类型转换错误
//int r6 = str.compareTo(new Integer(10));
}
}



例程5-2
class CompareStringIgnoreCase
{
    public static void main(String[] args) 
{
             int n1="abc".compareToIgnoreCase("Abc");
      System.out.println(n1);
      int n2="This".compareToIgnoreCase("tHe");
      System.out.println(n2);
      int n3="JAVE".compareToIgnoreCase("javeline");
      System.out.println(n3);
      int n4= "Java".compareToIgnoreCase("Javits");
      System.out.println(n4);
    }
}



例程5-3
class StringArray
{
    public static void main(String[] args) 
{
    //定义字符串数组
        String[] array = new String[]{"This is a test", 
"Theo Washer",
                                "the case is closed", 
"tHe aLtErNaTiNg StRiNg"};
        System.out.println(“排序之前:”+”\n”);
        //打印出排序之前的字符串数组
for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
        System.out.println(“排序之后:”+”\n”);
        //调用Arrays对象的方法对字符串数组进行排序
        java.util.Arrays.sort(array, String.CASE_INSENSITIVE_ORDER);
        //打印出排序之后的字符串数组
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
}



例程5-4
public class ReverseString 
{
    public static void main(String args[])
    {
			String strSource = new String(“I love Java”);
           String strDest = reverseIt ( strSource );
           System.out.println(strDest);
    }
public static String reverseIt(String source) 
{
          int i, len = source.length();
          StringBuffer dest = new StringBuffer(len);
          for (i = (len - 1); i >= 0; i--)
            dest.append(source.charAt(i));
          return dest.toString();
     }
}



例程5-5
import java.util.Vector;
class insertStringBuffer
{
public static void main(String[] args) 
{
//定义各种常用类型的数据变量
int inum = 512;
long lnum = Long.MAX_VALUE;
double dnum = 123.123e54;
float fnum = 3.1243f;
char sep = ' ';
Object obj1 = null;
Object obj2 = new Vector();
char[] charArray = {'a', 'b', 'c'};
StringBuffer buf = new StringBuffer(100);
//向字符串缓冲区中插入内容
buf.insert(0, inum).insert(0, sep).insert(0, lnum).insert(0, sep);
buf.insert(0, dnum).insert(0, sep).insert(0, dnum).insert(0, sep);
buf.insert(0, obj1).insert(0, sep).insert(0, obj2);
buf.insert(0, sep).insert(0, charArray);
buf.insert(0, charArray, 1, 1);
//打印结果
System.out.println(buf.toString());
}
}



例程5-6
// deleteStringBuffer.java
class deleteStringBuffer
{
    public static void main(String[] args) 
{
    //生成字符串缓冲区
        StringBuffer buf = new StringBuffer("I Love Java");
        //删除字符串缓冲区的第6-10个字符
        buf.delete(0,6);
        System.out.println(buf.toString());
    }
}



例程5-7
// deleteCharAtStringBuffer.java
class deleteCharAtStringBuffer 
{
    public static void main(String[] args) 
{
   //生成字符串缓冲区
	StringBuffer buf = new StringBuffer("I Love Java !");
   //调用deleteCharAt方法
	    buf.deleteCharAt(1);
buf.deleteCharAt(6);
	    buf.deleteCharAt(11);
	    System.out.println(buf.toString()); 
    }
}



例程5-8
class  replaceStringBuffer
{
    public static void main(String[] args) 
{
//创建字符串缓冲区
	StringBuffer buf = new StringBuffer("The C++ Is Wonderful");
   //调用replace方法
	    buf.replace(4, 7, "Java");
       //打印结果
	    System.out.println(buf.toString()); 
   }
}



例程5-9
class subStringBuffer
{
    public static void main(String[] args) 
{
//创建字符串缓冲区
	  StringBuffer buf = new StringBuffer("The Java Class Libraries");
      //调用subString方法
	  System.out.println(buf.substring(4, 8)); 
      //调用subString方法
	  System.out.println(buf.substring(9));
    }
}



例程5-10
//DefaultToken.java
import java.util.*;
public class DefaultToken
{
   public static void main(String args[])
   {
      //构造StringTokenizer对象
	StringTokenizer st = new StringTokenizer("this is a test String");
  	//在字符串中匹配默认的分隔符
        while(st.hasMoreTokens())
        {
            //打印当前分隔符和下一分隔符之间的内容
		     System.out.println(st.nextToken());
      }
   }
}



例程5-11
//UserToken.java
import java.util.*;
public class UserToken
{
   public static void main(String args[])
   {
      //构造待解析的字符串
      String parseString = new String(“this=is=a=test=String”);
      //构造StringTokenizer对象,设置以‘=’为分隔符
StringTokenizer st = new StringTokenizer(parseString,”=”);
//在字符串中匹配分隔符
      While(st.hasMoreTokens())
         {
            //打印当前分隔符和下一分隔符之间的内容
		     System.out.println(st.nextToken());
         }
   }
}



例程5-12
public class objetc2String_1
{
  public static void main(String args[])
  {
int nInt = 10;
float fFloat = 3.14f;
double dDouble = 3.1415926;

    //转换为整型
Integer obj1 = new Integer(nInt);
//转换为浮点数类型
Float obj2= new Float(fFloat); 
//转换为双精度类型
Double obj3 = new Double(dDouble);

    //分别调用toString方法转换为字符串
String strString1 = obj1.toString();
System.out.println(strString1);
String strString2 = obj2.toString();
System.out.println(strString1);
String strString3 = obj3.toString();
System.out.println(strString1);
  }
}



例程5-13
public class objetc2String_2
{
  public static void main(String args[])
  {
int nInt = 10;
float fFloat = 3.14f;
double dDouble = 3.1415926;

       //分别调用valueOf静态方法
String strString1 = String.valueOf(nInt);
System.out.println(strString1);
String strString2 = String.valueOf(fFloat);
System.out.println(strString1);
String strString3 = String.valueOf(dDouble);
System.out.println(strString1);
  }
}



例程5-14
public class String2Object
{
  public static void main(String args[])
  {
char[] cArray;
int nInt;
float fFloat;
double dDouble;
       //生成相应的数据类型
       String strString = new String(“I love Java”);
       String strInteger = new String(“314”);
       String strFloat = new String(“3.14”);
       String strDouble = new String(“3.1416”);
//分别调用各类中的静态方法
cArray = strString.toCharArray();
System.out.println(cArray);
nInt = Integer.parseInt(strInteger);
System.out.println(nInt);
fFloat = Float.parseFloat(strFloat);
System.out.println(fFloat);
dDouble = Double.parseDouble(strDouble);
System.out.println(dDouble);
  }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -