📄 stringcompare.java
字号:
// StringCompare
// This program demonstrates the methods equals,
// equalsIgnoreCase, compareTo, and regionMatches
// of the String class.
import javax.swing.JOptionPane;
public class StringCompare {
public static void main( String args[] )
{
String s1, s2, s3, s4, output;
s1 = new String( "hello" );
s2 = new String( "good bye" );
s3 = new String( "Happy Birthday" );
s4 = new String( "happy birthday" );
output = "s1 = " + s1 + "\ns2 = " + s2 +
"\ns3 = " + s3 + "\ns4 = " + s4 + "\n\n";
//test for equality
//equals用于比较两对象的内容是否相等.
//自定义对象时需要重载此方法
if ( s1.equals( "hello" ) )
output += "s1 equals \"hello\"\n";
else
output += "s1 does not equal \"hello\"\n";
// test for equality with ==
//比对引用.只有两个引用指向同一个对象,则结果为true
if ( s1 == "hello" )
output += "s1 == \"hello\"\n";
else
output += "s1 does not == \"hello\"\n";
// test for equality--ignore case
if ( s3.equalsIgnoreCase( s4 ) )
output += "s3 equals s4\n";
else
output += "s3 does not equal s4\n";
// test compareTo
//使用字典法进行比较,返回0表两字串相等,小于返回负值,大于返回正值
output +=
"\ns1.compareTo( s2 ) is " + s1.compareTo( s2 ) +
"\ns2.compareTo( s1 ) is " + s2.compareTo( s1 ) +
"\ns1.compareTo( s1 ) is " + s1.compareTo( s1 ) +
"\ns3.compareTo( s4 ) is " + s3.compareTo( s4 ) +
"\ns4.compareTo( s3 ) is " + s4.compareTo( s3 ) +
"\n\n";
// test regionMatches (case sensitive)
//比较两字串中的某一部分是否相等
//参数含义如下:
//1.调用这个方法的字串中的起始下标
//2.要比较的字串
//3.要比较的字串的起始下标
//4.要比较的字串比较的字符长度
if ( s3.regionMatches( 0, s4, 0, 5 ) )
output += "First 5 characters of s3 and s4 match\n";
else
output +=
"First 5 characters of s3 and s4 do not match\n";
// test regionMatches (ignore case)
if ( s3.regionMatches( true, 0, s4, 0, 5 ) )
output += "First 5 characters of s3 and s4 match";
else
output +=
"First 5 characters of s3 and s4 do not match";
JOptionPane.showMessageDialog( null, output,
"Demonstrating String Class Constructors",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
/**************************************************************************
* (C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall. *
* All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -