码迷,mamicode.com
首页 > 其他好文 > 详细

String常用方法测试

时间:2015-10-21 22:24:10      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:

String.Equals()使用方法

用来比较String两个对象所表示的字符串是否相同

public class StringEquals {

 

    public static void main(String[] args) {

       

        String s1=new String("Hello");

       

        String s2=new String("Hello");

 

       

        System.out.println(s1==s2);  //false

       

        System.out.println(s1.equals(s2));   //true

 

       

        String s3="Hello";

       

        String s4="Hello";

 

         

        System.out.println(s3==s4);       //true

       

        System.out.println(s3.equals(s4));      //true

    }

}

类的对象不能直接用==比较  比较的是两个String对象 不能用来比较对象和常量

对一些string 方法的测试 代码如下:

package demo;

 

//StringMisc.java

//This program demonstrates the length, charAt and getChars

//methods of the String class.

//

//Note: Method getChars requires a starting point

//and ending point in the String. The starting point is the

//actual subscript from which copying starts. The ending point

//is one past the subscript at which the copying ends.

import javax.swing.*;

 

public class Stringmisc {

public static void main( String args[] )

{

   String s1, output,s2,s3;

   char charArray[];

 

   s1 = new String( "hello there" );

   charArray = new char[ 5 ];

 

   // output the string

   output = "s1: " + s1;

 

   // test the length method

   output += "\nLength of s1: " + s1.length();

 

   // loop through the characters in s1 and display reversed

   output += "\nThe string reversed is: ";

 

   for ( int i = s1.length() - 1; i >= 0; i-- )

      output += s1.charAt( i ) + " ";

 

   // copy characters from string into char array

   //四个参数的含义

   //1.被拷贝字符在字串中的起始位置

   //2.被拷贝的最后一个字符在字串中的下标再加1

   //3.目标字符数组

   //4.拷贝的字符放在字符数组中的起始下标

   s1.getChars( 0, 5, charArray, 0 );

   output += "\nThe character array is: ";

  

   for ( int i = 0; i < charArray.length;i++ )

      output += charArray[ i ];

   //test the replace method

   s2 = s1.replace("e","o");         //replace方法完了转换后是返回一个新的对象,如果没有找到需要替换的字符串则返回原对象

   output +="\nThe replace method result is:"+s2;

 

   //test the toUpperCase and tpLowerCase

   s2 = s1.toUpperCase();                        //toUpperCase 是把所有字符串变为大写

   s3 = s1.toLowerCase();                        //toLowerCase 是吧所有字符串变为小写

   output += "\nThe Upper is:"+s2;

   output += "\nThe Lower is:"+s3;

  

   //test The trim                                                //去掉字符串首位的空格

   s2 = "      Hello There ";

   s3 = "Hello There";

   output += "\n Add The blank:"+s3.equals(s2);

   s2 = s2.trim();

   output += "\n after trim:" + s2.equals(s3);

  

   //test the toCharArray                         //把字符串存入一个字符数组中

   char array[] = s1.toCharArray();

   output += "\ntoCharArray:";

   for ( int i = 0; i < array.length;i++ )

               output += array[ i ];

  

   JOptionPane.showMessageDialog( null, output,

      "Demonstrating String Class Constructors",

      JOptionPane.INFORMATION_MESSAGE );

 

   System.exit( 0 );

}

}

 技术分享

String常用方法测试

标签:

原文地址:http://www.cnblogs.com/zczhtml/p/4899094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!