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 ...
随机推荐
- 分布式缓存Memcached---开篇的话
大数据.高并发这是最近一段时间内被IT行业提的最为火热的概念,看过<大数据时代>的同学应该不会陌生大数据的概念,尤其是对于互联网行业来说,大数据是每天都要接触的问题,简单通俗地说,每天得大 ...
- Mongodb在windows下的安装和启动
在windows下安装的参考官方地址:https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/ Mongodb的安装与启 ...
- python 3.5 关于sys问题总结
想把自己写的模组直接调用的时候,使用这个sys.path.append(): 但是总是报错(图1): 解决方案,去掉前面的from public; 解决思路: 1.sys.path.append(&q ...
- html特殊字符
平时写代码很少用到HTML的特殊字符,最常用的可能是 了,但有时在移动端为了节省时间,可能会用这些字符实现某种特殊效果,现整理如下: 使用方法: 这些字符属于unicode字符集,所以,你的文档需要声 ...
- NOI 1.5 44:第n小的质数
---恢复内容开始--- 描述 输入一个正整数n,求第n小的质数. 输入 一个不超过10000的正整数n. 输出 第n小的质数. 样例输入 10 样例输出 29 方法1:合数一定可以表示成一个比它小的 ...
- jquery 选择器(name,属性,元素)大全
jQuery 选择器大体上可分为:基本选择器.层次选择器.过滤选择器.表单选择器. 其中过滤选择器可以分为:简单过滤选择器.内容过滤选择器.可见性过滤选择器.属性过滤选择器.子元素过滤选择器.表单对象 ...
- Access restriction: The type QName is not accessible due to restriction on required library
There's another solution that also works. I found it on this forum: Go to the Build Path settings in ...
- 对IOC和DI以及AOP的理解
为了理解Spring的IoC与DI从网上查了很多资料,作为初学者,下面的描述应该是最详细,最易理解的方式了. 首先想说说IoC(Inversion of Control,控制倒转).这是spring的 ...
- Windows Phone 十八、加速计
加速度传感器 手机的加速度传感器工作时是通过 x.y.z 三个轴的偏移来计算的 在代码基本的 API 主要集中在 Accelerometer 类型中 主要是使用该类型的对象捕获 ReadingChan ...
- ZOJ-1239 Hanoi Tower Troubles Again!
链接:ZOJ1239 Hanoi Tower Troubles Again! Description People stopped moving discs from peg to peg after ...