String常用方法测试
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常用方法测试的更多相关文章
- Javascript语言精粹之String常用方法分析
Javascript语言精粹之String常用方法分析 1. String常用方法分析 1.1 String.prototype.slice() slice(start,end)方法复制string的 ...
- JAVA之旅(十六)——String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较
JAVA之旅(十六)--String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较 过节耽误了几天,我们继续JAVA之旅 一.String概述 String时 ...
- String常用方法
1. String StringBuffer StringBuilder的区别: 001.在执行速度方法 StringBuilder > StringBuffer > String 002 ...
- JVM内存分配及String常用方法
一,JVM内存分配和常量池 在介绍String类之前,先来简单分析一下在JVM中,对内存的使用是如何进行分配的.如下图所示(注意:在jdk1.8之后便没有方法区了): 如上JVM将内存分为 ...
- String常用方法解析
package com.javaBase.string; import java.util.Locale; /** * 〈一句话功能简述〉; * 〈String类中常用的方法整理〉 * * @auth ...
- Java中String常用方法总结
package cn.zhang.Array; /** * String类的一些常用方法 * @author 张涛 * */ public class TestString { public stat ...
- Java 中String常用方法
java中String的常用方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len ...
- Java中String常用方法
java中String的常用方法1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len= ...
- 类String 常用方法
字符串当中的常用方法之比较相关的方法 public boolean equals (object obj):将此字符串与指定的对象进行比较(只有参数是字符串并且内容相同才会返回true) public ...
随机推荐
- LeetCode Sum of Left Leaves
原题链接在这里:https://leetcode.com/problems/sum-of-left-leaves/ 题目: Find the sum of all left leaves in a g ...
- ios上的 button和input-button为什么不水平居中的
在iphone6plus上的button中文本上不居中,如下图: 造成的原因,是button的padding不为零,造成的,因而设置padding: 0:就可以解决
- Node.js 学习笔记
时间:2016-07-07 提前安装好linux CentOs环境,以及相关工具(git,wget,vim……) 1.安装: 目前打算在CentOs上进行操作,网上有教程是旧的地址,这个是搬家后的新地 ...
- SpringMVC问题- MultipartConfig 配置问题以及解决方式
http://www.cnblogs.com/weilu2/p/springmvc_fileupload_with_servlet_3_0.html
- 可爱的Python_课后习题_CDay−4 可用的首个Python 脚本
读取文件cdays−4-test.txt 内容,去除空行和注释行后,以行为单位进行排序,并将结果输出为cdays−4-result.txt. cdays−4-test.txt的内容 #some wor ...
- 夺命雷公狗----Git---2---基本用法
首先我们来创建一个git仓库,其实命令也是很简单的.. git init 其实在创建完成后,该目录会多了一个.git的文件夹,但该文件夹是隐藏的.... 其实这就是一个git仓库,特别建议不要在这个目 ...
- .net动态类型在处理json数据方面的应用
我们在.net中处理json数据时,经常需要定义相应的类.比如json数据:{ "name" : "hello", "age" : 18 } ...
- 利用wireshark抓包获取cookie信息
以下是一些过滤规则: 1. 百度的cookie: http.cookie matches "BDUSS" 2. 博客园的cookie: http.cookie matches &q ...
- 模拟apache commons dbutils 实现自己的BeanListHandler(回调应用)
首先dbcp相关的jar包和MySQL的驱动包导入到项目中. dbcp.properties配置文件如下,并放到项目根目录下. driverClassName=com.mysql.jdbc.Drive ...
- 自学android半年,已从.net转型成android程序员,分享下这个过程
自学从来都是一件难以坚持的事情,看过太多人三分钟热度之后就颓然放弃,然后告诉下一个要自学的人,自学很难,还是正儿八经去培训机构吧 所以首先你要对安卓开发非常感兴趣,发自内心喜欢安卓系统,日常手机如果是 ...