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常用方法测试的更多相关文章

  1. Javascript语言精粹之String常用方法分析

    Javascript语言精粹之String常用方法分析 1. String常用方法分析 1.1 String.prototype.slice() slice(start,end)方法复制string的 ...

  2. JAVA之旅(十六)——String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较

    JAVA之旅(十六)--String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较 过节耽误了几天,我们继续JAVA之旅 一.String概述 String时 ...

  3. String常用方法

    1. String StringBuffer StringBuilder的区别: 001.在执行速度方法 StringBuilder > StringBuffer > String 002 ...

  4. JVM内存分配及String常用方法

    一,JVM内存分配和常量池 ​ 在介绍String类之前,先来简单分析一下在JVM中,对内存的使用是如何进行分配的.如下图所示(注意:在jdk1.8之后便没有方法区了): ​ ​ 如上JVM将内存分为 ...

  5. String常用方法解析

    package com.javaBase.string; import java.util.Locale; /** * 〈一句话功能简述〉; * 〈String类中常用的方法整理〉 * * @auth ...

  6. Java中String常用方法总结

    package cn.zhang.Array; /** * String类的一些常用方法 * @author 张涛 * */ public class TestString { public stat ...

  7. Java 中String常用方法

    java中String的常用方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len ...

  8. Java中String常用方法

    java中String的常用方法1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len= ...

  9. 类String 常用方法

    字符串当中的常用方法之比较相关的方法 public boolean equals (object obj):将此字符串与指定的对象进行比较(只有参数是字符串并且内容相同才会返回true) public ...

随机推荐

  1. php页面如何增加下载软件功能

    php <?php $array = parse_ini_file("sample.ini"); ?> <a id="btnDownloadFull&q ...

  2. itellij idea导入web项目并部署到tomcat

    概述 主要分为项目配置和tomcat配置两大步骤. 一.项目配置 打开idea,选择导入项 选择将要打开的项目路径后,继续选择项目的原本类型(后续引导设置会根据原本的项目类型更新成idea的项目),此 ...

  3. 在Eclipse中使用Git提交到远程仓库

  4. socket编程进阶

    1.   动态导入模块 第一种方法(python解释器自己内部用的): 上图是我程序的目录结构 下面代码是动态导入模块3.py的源码: #AUTHOR:FAN lib_dir = __import__ ...

  5. Java 之 List<T> 接口的实现:ArrayList

    Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap└WeakHashMap ArrayLis ...

  6. nodejs cookie管理

    Cookie 管理 我们可以使用中间件向 Node.js 服务器发送 cookie 信息,以下代码输出了客户端发送的 cookie 信息: // express_cookie.js 文件 var ex ...

  7. eclipse中搭建svn开发管理环境

    1.准备好资源 subversive,若是离线安装,已为大家准备好:http://pan.baidu.com/s/1hrbXH9y(本人喜欢离线安装,在线安装就不在此说了,其实只要一种方法简单的方法能 ...

  8. vs2010问题:未能安装xxx包

    打开vs2010新建c++工程,出现问题如图 原因是重复安装了,之前安装的没有删除干净,导致冲突. 如果你的vs2010安装在c盘,解决方法:http://blog.sina.com.cn/s/blo ...

  9. Openbox简单支持平铺

    使用和gnome shell同样的热键定义 rc.xml中 <keybind key="W-Up"> <action name="Maximize&qu ...

  10. PHP开发中的缓存技术汇总

    在PHP开发中,出于对网站服务器负载的考虑,往往需要对页面.数据等内容进行缓存处理,下面就来看看,在PHP开发中有哪些缓存方式吧. 1.页面部分缓存该种方式,是将一个页面中不经常变的部分进行静态缓存, ...