首页 > 编程知识 正文

字符串反转java,java通过字符串调用方法

时间:2023-05-04 05:21:57 阅读:15158 作者:764

http://www.Sina.com/http://www.Sina.com /

字符串是常量,一旦创建就无法更改。 这是因为字符串的值存储在方法区域的常量池中,但可以更改引用。 也可以将文字“ab”视为字符串对象。

内存映射如下所示(

这样,可以更好地理解程序运行过程中将字符串常量存储在内存中的情况。字符串是由多个字符组成的一串数据

public int length () :获取字符串的长度。 publiccharcharat [ intindex ] :获取指定索引位置的字符。 公共索引of [ intch ] :返回指定字符在此字符串中出现的第一个索引。 公共索引of (字符串) :返回指定字符串在此字符串中出现的第一个索引。 公共索引of (intch,int fromIndex ) :返回指定字符在此字符串指定位置之后出现的第一个索引。 公共int index of (stringstr,int fromIndex ) :返回指定字符串中第一个出现在指定位置之后的索引。 publicstringsubstring(intstart ) :从指定位置剪切字符串,默认情况下位于末尾。 公共字符串子串(intstart,int end ) :将字符串从指定位置剪切到指定位置。 package org.baidu2; 公共类mytest1{ publicstaticvoidmain (string [ ] args ) { String str='anAdEfg '; system.out.println (字符串类获取功能); //字符串的长度int length=str.length (; //获取指定索引位置的字符charc1=str.charat(0)//指定字符在此字符串中出现的第一个索引intc2=str.indexof(n ); //指定字符串在此字符串中出现的第一个索引intc3=str.indexof('fg ); //返回指定字符在此字符串中从指定位置开始出现的第一个索引。 INTC4=str.indexof('f ',2 ); //返回从指定字符串中指定位置开始出现的第一个索引。 INTC5=str.indexof('fg ',2 ); //从指定位置剪切字符串,默认情况下末尾的stringc6=str.substring(2); //将字符串从指定位置剪切到指定位置的stringc7=str.substring (2,4 ); system.out.println(Length; system.out.println(C1; system.out.println(C2; system.out.println(C3; system.out.println(C4; system.out.println(C5; system.out.println(C6; system.out.println(C7; }运行结果:

7a 1555 adefgad http://www.Sina.com /

publicbooleanequals(objectobj ) :比较字符串内容是否相同,区分大小写publicbooleanequalsignorecase ) :比较字符串内容是否相同, 传递的字符串为3360,用于确定是否包含传递给忽略大小写的publicbooleancontains (stringstr ) :字符串的publicbooleanstartswith (stringstr ) 判断anendswith(stringstr ) :字符串是否以传递的字符串结束公共布尔输入(public boolean isEmpty ) :字符串的内容是否为空字符串“”。 package org.baidu2; 公共类mytest1{ publicstaticvoidmain (string [ ] args ) { String str1='axcde '; String str2='Axcde '; system.out.println (字符串类判断功能); //比较字符串内容是否相同,区分大小写的BooleanB=str1.equals(str2); //比较字符串的内容是

否相同,忽略大小写 boolean b1 = str1.equalsIgnoreCase(str2); //判断字符串中是否包含传递进来的字符串 boolean b2 = str1.contains("cde"); //判断字符串是否以传递进来的字符串开头 boolean b3 = str1.startsWith("ax"); //判断字符串是否以传递进来的字符出结尾 boolean b4 = str2.endsWith("de"); //判断字符串的内容是否为空 boolean b5 = str1.isEmpty(); System.out.println(b1); System.out.println(b2); System.out.println(b3); System.out.println(b4); System.out.println(b5); } }

运行结果为:

String类的判断功能truetruetruetruefalse

3.常见String类的转换功能

public byte[] getBytes(): 把字符串转换为字节数组。public char[] toCharArray(): 把字符串转换为字符数组。public static String valueOf(char[] chs): 把字符数组转成字符串。public static String valueOf(int i): 把int类型的数据转成字符串。(String类的valueOf方法可以把任意类型的数据转成字符串。)public String toLowerCase(): 把字符串转成小写。public String toUpperCase(): 把字符串转成大写。public String concat(String str): 把字符串拼接。 package org.baidu2;public class Mytest1 { public static void main(String[] args) { String str = "anAdEfg"; String str1 = "axcde"; int a=123323; //把字符串转换为字节数组 byte[] bytes = str.getBytes(); for (int i = 0; i < bytes.length; i++) { System.out.print(bytes[i]+" "); } //把字符串转换为字符数组 char[] chars = str.toCharArray(); for (int i = 0; i < chars.length; i++) { System.out.print(chars[i]+" "); } System.out.println(); //把字符数组转换成字符串 String s2 = new String (chars); //把int类型的数据转成字符串 String s3 = Integer.toString(a); //把字符串转换成小写 String s = str.toLowerCase(); //把字符串变成大写 String s1 = str.toUpperCase(); //字符串拼接 String s4 = str.concat(str1); System.out.println(s); System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s4); } }

运行结果为:

97 110 65 100 69 102 103 a n A d E f g anadefgANADEFGanAdEfg123323anAdEfgaxcde

4.常见String类的其他常用功能

public String replace(char old,char new) 将指定字符进行互换public String replace(String old,String new) 将指定字符串进行互换public String trim() 去除两端空格public int compareTo(String str) 会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果,如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果,如果连个字符串一摸一样 返回的就是0public int compareToIgnoreCase(String str) 跟上面一样 只是忽略大小写的比较 package org.baidu2;public class Mytest1 { public static void main(String[] args) { String str = " anAdEfg "; String str1 = "axcde"; //将指定字符进行互换 String s = str.replace('a', 'b'); System.out.println(s); //将指定字符串进行互换 String s1 = str.replace("ab", "qq"); System.out.println(s1); //去除两端空格 String s2 = str.trim(); System.out.println(s2); //通过字典顺序去比较 返回的值是 ASCII 码的差值 调用者减去传入者 int i = "abc".compareTo("ABc"); System.out.println(i); //如果前面几个字母一样会根据两个字符串长度进行减法运算返回该减法的结果 (通过长度去比) int i1 = "abc".compareTo("a"); System.out.println(i1); //忽略大小写的比较 int i2 = "abc".compareToIgnoreCase("ABC"); System.out.println(i2); } }

运行结果为:

bnAdEfg anAdEfg anAdEfg3220

5.常见String类中int与String的相互转换
  int转String有三种方式

num + “”String.valueOf(num)Integer.toString(num)
  String转int有两种方式Integer.parseInt(str)Integer.valueOf(str).intValue()
上代码看一下 package org.westos.demo3;public class mytest6 { public static void main(String[] args) { //int->String int i = 1234567; String s = ""; s = i + "";//会产生两个String对象 System.out.println(s); String s1 = String.valueOf(i);//使用String类的静态方法,只产生一个String对象 System.out.println(s1); String s2 = Integer.toString(i); System.out.println(s2); //String->int String str = "12345"; int i1 = Integer.parseInt(str);//直接使用静态方法,不会产生多余的对象 System.out.println(i1); int i2 = Integer.valueOf(str).intValue(); System.out.println(i2); }}

运行结果为:

1234567123456712345671234512345

5.在学习了字符串以后知道了==和equals()的区别,下面拿代码看一下。

package org.baidu2;public class Mytest1 { public static void main(String[] args) { String s1 = new String("hello"); String s2 = new String("hello"); System.out.println(s1.equals(s2));//判断字符串内容 System.out.println(s1 == s2);//判断字符串引用 }}

运行结果为:

truefalse

7.Java中的charAt()方法
java.lang.String.charAt() 方法返回指定索引处的char值。索引范围是从0到length() - 1。
声明java.lang.String.charAt()方法

public charcharAt(int index)
拿个代码简单了解一下。 package org.baidu2;public class Mytest2 { public static void main(String[] args) { //统计字符串中的小写字母,大写字母数字空格的个数 String str = "1123ahdiASDFGF shaid"; int upper = 0; int lower = 0; int num = 0; int space = 0; for (int i = 0; i < str.length(); i++) { //返回该索引处的char值 char c = str.charAt(i); System.out.print(c+" "); if(c>'a'&&c<'z'){ lower++; } if(c>'A'&&c<'Z'){ upper++; } if(c>'0'&&c<'9'){ num++; } if(c==' '){ space++; } } System.out.println(); System.out.println(lower); System.out.println(upper); System.out.println(num); System.out.println(space); }}

运行结果为:

1 1 2 3 a h d i A S D F G F s h a i d 7544

·

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。