3.Java基础:String对象的创建和使用

一.常用的创建方式

String s1=”abc“;

String s2=”abc“;

s1==s2    ==> true

解析:s1和s2指向的是同一个字符串池地址

二.不常用的创建方式

String s1=new String(”abc“);

String s2=new String(”abc“);;

s1==s2    ==> false

解析:s1和s2指向的是不同的字符串池地址,new的时候会重新创建一个新的内存空间装载字符串,哪怕之前有一样的

三.String的特点

(1).String对象是不可变的(常量);

(2)类中的每一个看来回修改S听值得方法,其实都是创建了新的String对象(包含修改后的字符串内容);

(3)String的只读特性带来的效率优化可能;

(4)字符串字面值存储与字符串池中,String对象优先指向该字符池,避免反复生成重复的字符串实例;

(5)系统对String的非修改处理效率很高,远远超过另外两个字符串类StringBuffer和StringBuilder(后续讲解

四.String对象的常用方法

1、求字符串长度
public int length()//返回该字符串的长度

1 String str = new String("asdfzxc");
2 int strlength = str.length();//strlength = 7

2、求字符串某一位置字符
public char charAt(int index)//返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是length()-1。

1 String str = new String("asdfzxc");
2 char ch = str.charAt(4);//ch = z

3、提取子串
用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:
1)public String substring(int beginIndex)//该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。
2)public String substring(int beginIndex, int endIndex)//该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。

1 String str1 = new String("asdfzxc");
2 String str2 = str1.substring(2);//str2 = "dfzxc"
3 String str3 = str1.substring(2,5);//str3 = "dfz"

4、字符串比较
1)public int compareTo(String anotherString)//该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
2)public int compareToIgnore(String anotherString)//与compareTo方法相似,但忽略大小写。
3)public boolean equals(Object anotherObject)//比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
4)public boolean equalsIgnoreCase(String anotherString)//与equals方法相似,但忽略大小写。

1 String str1 = new String("abc");
2 String str2 = new String("ABC");
3 int a = str1.compareTo(str2);//a>0
4 int b = str1.compareTo(str2);//b=0
5 boolean c = str1.equals(str2);//c=false
6 boolean d = str1.equalsIgnoreCase(str2);//d=true

5、字符串连接
public String concat(String str)//将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。

1 String str = "aa".concat("bb").concat("cc");
2 相当于String str = "aa"+"bb"+"cc";

6、字符串中单个字符查找
1)public int indexOf(int ch/String str)//用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
2)public int indexOf(int ch/String str, int fromIndex)//改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。
3)public int lastIndexOf(int ch/String str)//该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
4)public int lastIndexOf(int ch/String str, int fromIndex)//该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。

1 String str = "I am a good student";
2 int a = str.indexOf(‘a‘);//a = 2
3 int b = str.indexOf("good");//b = 7
4 int c = str.indexOf("w",2);//c = -1
5 int d = str.lastIndexOf("a");//d = 5
6 int e = str.lastIndexOf("a",3);//e = 2

7、字符串中字符的大小写转换
1)public String toLowerCase()//返回将当前字符串中所有字符转换成小写后的新串
2)public String toUpperCase()//返回将当前字符串中所有字符转换成大写后的新串

1 String str = new String("asDF");
2 String str1 = str.toLowerCase();//str1 = "asdf"
3 String str2 = str.toUpperCase();//str2 = "ASDF"

8、字符串中字符的替换
1)public String replace(char oldChar, char newChar)//用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。
2)public String replaceFirst(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。
3)public String replaceAll(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。

1 String str = "asdzxcasd";
2 String str1 = str.replace(‘a‘,‘g‘);//str1 = "gsdzxcgsd"
3 String str2 = str.replace("asd","fgh");//str2 = "fghzxcfgh"
4 String str3 = str.replaceFirst("asd","fgh");//str3 = "fghzxcasd"
5 String str4 = str.replaceAll("asd","fgh");//str4 = "fghzxcfgh"

9、其他类方法
1)String trim()//截去字符串两端的空格,但对于中间的空格不处理。

1 String str = " a sd ";
2 String str1 = str.trim();
3 int a = str.length();//a = 6
4 int b = str1.length();//b = 4

2)boolean statWith(String prefix)boolean endWith(String suffix)//用来比较当前字符串的起始字符或子字符串prefix和终止字符或子字符串suffix是否和当前字符串相同,重载方法中同时还可以指定比较的开始位置offset。

1 String str = "asdfgh";
2 boolean a = str.statWith("as");//a = true
3 boolean b = str.endWith("gh");//b = true

3)regionMatches(boolean b, int firstStart, String other, int otherStart, int length)//从当前字符串的firstStart位置开始比较,取长度为length的一个子字符串,other字符串从otherStart位置开始,指定另外一个长度为length的字符串,两字符串比较,当b为true时字符串不区分大小写。
4)contains(String str)//判断参数s是否被包含在字符串中,并返回一个布尔类型的值。

1 String str = "student";
2 str.contains("stu");//true
3 str.contains("ok");//false

5)String[] split(String str)//将str作为分隔符进行字符串分解,分解后的字字符串在字符串数组中返回。

1 String str = "asd!qwe|zxc#";
2 String[] str1 = str.split("!|#");//str1[0] = "asd";str1[1] = "qwe";str1[2] = "zxc";

五、字符串与基本类型的转换
1、字符串转换为基本类型
java.lang包中有Byte、Short、Integer、Float、Double类的调用方法:
1)public static byte parseByte(String s)
2)public static short parseShort(String s)
3)public static short parseInt(String s)
4)public static long parseLong(String s)
5)public static float parseFloat(String s)
6)public static double parseDouble(String s)
例如:

1 int n = Integer.parseInt("12");
2 float f = Float.parseFloat("12.34");
3 double d = Double.parseDouble("1.124");

2、进制转换
使用Long类中的方法得到整数之间的各种进制转换的方法:
Long.toBinaryString(long l)
Long.toOctalString(long l)
Long.toHexString(long l)
Long.toString(long l, int p)//p作为任意进制

3、基本类型转换为字符串类型
String类中提供了String valueOf()放法,用作基本类型转换为字符串类型。
1)static String valueOf(char data[])
2)static String valueOf(char data[], int offset, int count)
3)static String valueOf(boolean b)
4)static String valueOf(char c)
5)static String valueOf(int i)
6)static String valueOf(long l)
7)static String valueOf(float f)
8)static String valueOf(double d)
例如:

1 String s1 = String.valueOf(12);
2 String s1 = String.valueOf(12.34);

原文地址:https://www.cnblogs.com/lzfsuifeng/p/9527190.html

时间: 2024-08-27 04:47:14

3.Java基础:String对象的创建和使用的相关文章

JAVA String对象的创建

String对象的创建是比较特殊的,普通对象创建都是在堆中创建,String对象确不一定,下面看一段代码 [java] view plaincopy public class StringTest1 { public static void main(String[] args) throws Exception{ String a = "abc"; String b = "abc"; String c = "ab"; String d = ne

J2SE基础:2.对象的创建与使用

1:参数传递的值传递与引用传递 A:值传递:基本数据类型传递都是值传递 B:引用传递(地址传递):对象数据类型都是引用传递. 2:类变量与成员变量(实例变量,对象变量) 类变量:通过类名调用,类变量被所有的实例共享. final static int MAX = 20;//Java中定义常量 对象变量:通过对象调用(对象必须new出来). 3:类方法与成员方法(实例方法,对象方法) 类方法:通过类名调用,在类方法中不能使用this关键字. 因为this代表当前对象. 成员方法:通过对象调用(对象

分析Java的String对象

对于Java中的String对象,个人觉得每个程序员都会思考过.学习过.研究过这个对象,因为他是面试官们的最爱.如:String s = new String("abc");,创建了几个对象.这种问题反复出现在程序员面试的过程中.下面我们对应着一些代码片段以及其的执行结果,来深入分析Java的String对象. 首先我们要注意的是String对象的处理在JDK6和JDK7中的处理是不同的.下面通过代码来分析String的三个主要方面知识. 1.常量池的存在 先看看下面代码以及执行结果:

Java中String对象的不可变性

首先看一个程序 package reverse; public class Reverse { public static void main(String[] args) { String c1=new String("abc"); String c2=new String("abc"); String c3=c1; System.out.println("c1==c2:"+ (c1==c2)); System.out.println(&quo

java中String对象和String变量

java中String对象和String变量 (2011-12-27 20:40:27) 转载▼ 标签: it 最近在论坛上看到关于String s = new String("XYZ") + new String("XYZ");到底创建几个对象的讨论,觉得比较有意思,在此总结一下. 在JAVA中除了8种基本类型之外,其他的都是类对象及其引用.所以 "XYZ"在JAVA中是一个String对象,对于String类对象来说它的对象值是不能修改的,也

Java基础String的方法

Java基础String的方法 字符串类型写法格式如下: 格式一: String 变量名称; 变量名称=赋值(自定义或传入的变量值); 格式二: String 变量名称=赋值(自定义或传入的变量值);在输出时任何数据类型与字符串进行拼接,结果一般是字符串 1 public class StringFunc { 2 3 public static void main(String[] args){ 4 //字符串拼接 5 String str1; 6 str1 = "hello"; 7

Java系列2 --- 你真的知道Java的String对象么?

?在上一篇中说道这篇文章会说java的动态绑定机制,由于这个知识点放在继承中讲会比较合适,说以在这篇文章中先来详细的说说String对象吧. ?只要学过Java的同学,我们都知道Java一共有8中基本类型,但是在Java中最常用的String类型却不属于这8中基本类型中.他是Java.lang包中的一个类.但是String对象在引用传递中JVM的处理却与其他对象不同. ?在正式开始来讲这个String对象的时候我们首先来简单的说明下Java中的值传递和引用传递.正如很多Java说熟知的那样,Ja

Java基础-String、StringBuffer、StringBuilder

看下面这段代码: public class Main { public static void main(String[] args) { String string = ""; for(int i=0;i<10000;i++){ string += "hello"; } } } 这句 string += "hello";的过程相当于将原有的string变量指向的对象内容取出与"hello"作字符串相加操作再存进另一个新

java基础知识-对象和类

前言: 因为要准备Java面试,所有将java基础知识点重新复习一遍,主要笔记来源于菜鸟教程和java核心技术的书籍中,也有一些博客上的资料(这些只供我个人学习使用) Java 对象和类 对象:对象是类的一个实例,有状态和行为.例如,一条狗是一个对象,它的状态有:颜色.名字.品种:行为有:摇尾巴.叫.吃等. 类:类是一个模板,它描述一类对象的行为和状态. 下图中男孩女孩为类,而具体的每个人为该类的对象: 1.Java中的对象 现在让我们深入了解什么是对象.看看周围真实的世界,会发现身边有很多对象