String的点点滴滴
一、String 的 equals()到底比较的是什么?equals() 与 == 的区别?
当使用关系运算符==比较两个对象时,是比较两个对象使用的内存地址和内容是否相同,如果两个对象使用的是同一个内存地址,并且内容相同,则结果为true,否则结果为false
public class CompareObjectDemo {
public static void main(String[] args) {
String str1 = new String("hello");
String str2 = new String("hello");
String str3 = str2;
//用==比较
System.out.println("str1 == str2的比较结果为:\n\t" + (str1 == str2));
System.out.println("str2 == str3的比较结果为:\n\t" + (str2 == str3));
//用equals比较
System.out.println("str1.equals(str2)的比较结果为:\n\t" +str1.equals(str2));
System.out.println("str2.equals(str3)的比较结果为:\n\t" +str2.equals(str3));
}
}
public class CompareObjectDemo {
public static void main(String[] args) {
String str1 = "hello";
String str2 = "hello";
String str3 =str2;
//用==比较
System.out.println("str1 == str2的比较结果为:\n\t" + (str1 == str2));
System.out.println("str2 == str3的比较结果为:\n\t" + (str2 == str3));
//用equals比较
System.out.println("str1.equals(str2)的比较结果为:\n\t" +str1.equals(str2));
System.out.println("str2.equals(str3)的比较结果为:\n\t" +str2.equals(str3));
}
}
/**
* String -> other type
*/
public static void testString2OtherType(){
String s1 = "121";
int parseInt = Integer.parseInt(s1);
System.out.println(parseInt); String s2 = "12.1";
double parseDouble = Double.parseDouble(s2);
System.out.println(parseDouble); String s3 = "12.1f";
float parseFloat = Float.parseFloat(s3);
System.out.println(parseFloat); String s4 = "false";
boolean parseBoolean = Boolean.parseBoolean(s4);
System.out.println(parseBoolean); //或者使用如下也可
Integer i = Integer.valueOf(s1);
System.out.println(i); Double d = Double.valueOf(s2);
System.out.println(d); //...
} /**
* other type --> String
*/
public static void otherType2String(){
int i = 38;
String s1 = String.valueOf(i);
System.out.println(s1); float f = 38.38f;
String s2 = String.valueOf(f);
System.out.println(s2); //...
}
四、[很好用] 字符串分割 split
public static void main(String[] args) {
String src = "hello world,I love you";
StringDemo.testStringSplit(src);
}
public static void testStringSplit(String src){
String[] strArray = src.split("\\W");//按照单词来划分,具体的正则表达式可以参考Pattern类,里面有详细介绍
for(String s : strArray){
System.out.println(s);
}
}
结果
hello
world
I
love
you
String的点点滴滴的更多相关文章
- Ⅳspring的点点滴滴--方法和事件
承接上文 方法和事件 .net篇(环境为vs2012+Spring.Core.dll v1.31) public abstract class MethodDemo { protected abstr ...
- Ⅲ.spring的点点滴滴--赋值
承接上文 对象的赋值(调用方式都一样不再阐述) .net篇(环境为vs2012+Spring.Core.dll v1.31) public class PropertyDemo{ public Sys ...
- Ⅱ.spring的点点滴滴--对象
承接上文 对象的各种实例化 .net篇(环境为vs2012+Spring.Core.dll) 修改原来的PersonDao对象为 public class PersonDao : IPersonDao ...
- Ⅰ.Spring的点点滴滴--序章
spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架 .net篇(环境为vs2012+Spring.Core.dll) 新建一个控制台 using Spring.Context; ...
- XI.spring的点点滴滴--IObjectFactoryPostProcessor(工厂后处理器)
承接上文 IObjectFactoryPostProcessor(工厂后处理器)) 前提是实现接口的对象注册给当前容器 直接继承的对象调用这个postProcessBeanFactory方法,参数为工 ...
- Ⅸ.spring的点点滴滴--IObjectFactory与IFactoryObject的杂谈
承接上文 ObjectFactory与IFactoryObject的杂谈 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class parent { pu ...
- Ⅷ.spring的点点滴滴--抽象对象和子对象
承接上文 抽象对象和子对象 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class parent { public string Name { get; ...
- Ⅵ.spring的点点滴滴--自定义类型转换器
承接上文 自定义类型转换器 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class CustomeConverter : TypeConverter{ ...
- Ⅴ.spring的点点滴滴--引用其他对象或类型的成员
承接上文 引用其他对象或类型的成员 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class Person { public string Name { ...
随机推荐
- Ubuntu下Qt编译报错“cannot find -lGL”的解决方案
转自cannot find -lGL Solved the problem by installing the "libglu1-mesa-dev" package. sudo a ...
- SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-004- <s:url>、<s:escapeBody>标签
一.<s:url> <s:url>可以直接生成一个url或url变量,它会在href的基础上加上应用context 1. <a href="<s:url ...
- Pascal Game Development with Jason McMillen
In this much belated episode I talk with Jason McMillen of Pascal Game Development. We discuss the s ...
- Nuget
Install-Package Microsoft.AspNet.WebApi.Cors
- webstore+nodejs
新建一个普通的project. 编写如下代码: var http=require('http'); http.createServer(function(req,res){ res.writeHead ...
- 【Lucene3.6.2入门系列】第03节_简述Lucene中常见的搜索功能
package com.jadyer.lucene; import java.io.File; import java.io.IOException; import java.text.SimpleD ...
- 手势识别官方教程(2)识别常见手势用GestureDetector+手势回调接口/手势抽象类
简介 GestureDetector识别手势. GestureDetector.OnGestureListener是识别手势后的回调接口.GestureDetector.SimpleOnGesture ...
- DP录 (更新)
补补弱项 全面发展.. 从最基础来 sdut1299最长上升子序 #include <iostream> #include<cstdio> #include<cstrin ...
- 一个TextView使用不同的颜色
一个TextView使用不同的颜色: textview中首先需要已经有内容了 public static void ColorFormatTextView(TextView tv,int color, ...
- Orchard中的多语言功能
在Orchard中支持了两种本地化的方法: 1.对Orchard应用程序和模块中的一些文本字符串进行本地化.这个就相当程序本身的多语言支持,大多数的CMS系统都支持这一功能,如:DotNetNuke. ...