Java StringBuffer输出字符串换行 java中string怎么输出 |
您所在的位置:网站首页 › java中怎么输出字符数组 › Java StringBuffer输出字符串换行 java中string怎么输出 |
一、字符串的自身特性(I) String类$$ 字符串的本质是字符数组,在java中字符串是String类型的对象。String对象的内容、长度一经配置,便不可改变(因为它是final的),对于 str="abc"; str="zxc";并不是改变了"abc"对象的内容,而是另创建了一个对象"zxc",将引用名str参考至新字符串罢了。 $$ String对象上的几个常用方法:1、length(): 取得字符串长度2、equals(): 判断两字符串是否相等3、toLowerCase()4、toUpperCase() $$ 将字符串分解为数值类型:1、static byte Byte.praseByte(String str)2、static short Short.parseShort(String str)3、static int Integer.parseInt(String str)4、static long Long.parseLong(String str)5、static float Float.parseFloat(String str)6、static double Double.parseDouble(String str)如果指定的字符串无法解析为指定的数据类型数值,则会发生NumberFormatException异常。 $$ 取得字符串中字符的方法:1、char charAt(int index):返回指定索引处的字符2、int indexOf(char ch):返回找到的第一个指定字符所在的索引位置3、int indexOf(String str):返回找到的第一个指定子串所在的索引位置4、int lastIndexOf(char ch):返回找到的最后一个指定字符串所在的索引位置5、String substring(int beginIndex):取出从指定索引处到末端的子字符串6、String substring(int beginIndex, int endIndex)7、char[] toCharArray():将字符串转化为字符数组8、endsWith(末端子串):取出字符串中以指定子串结尾的字符串 【实验代码】 public class CharAtStringDemo{ public static void main(String[] args){ String str = "Today is a sunny day."; char ch1 = str.charAt(0); int num1 = str.indexOf('a'); int num2 = str.lastIndexOf('a'); int num3 = str.indexOf("a sunny"); String str1 = str.substring(9); System.out.println("ch1="+ch1); System.out.println("num1="+num1); System.out.println("num2="+num2); System.out.println("num3="+num3); System.out.println("str1="+str1); char[] charArr = str.toCharArray(); System.out.println("Output the contents of charArr: "); for(char ch : charArr) System.out.print(ch); System.out.println(); } } $$ intern()方法API说明: 在intern()方法被调用时,如果池(Pool)中已经包括了相同的String对象(相同与否由equals()方法决定,即所含字符串的内容是否相同),那么会从池中返回该字符串,否则原String对象会被加入池中,并返回这个String对象的引用。 (II) StringBuilder类 JDK5.0开始提供java.lang.StringBuilder,使用这个类会产生默认16个字符长度的对象,用户也可自行指定长度。如果附加的字符超过了可容纳的长度,则StringBuilder对象会自动增加长度以容纳被附加的字符。这对于需频繁进行append操作的情况,比"+"连接符的效率要高得多,因为使用"+"连接字符串实际上是又创建了新的实例,会消耗较多时间和内存资源。 二、对字符串的操作和相关API字符串的分离: 字符串对象.split( ) 三、其他主题:正则式的对比PatternMatcher |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |