当前位置:首页 > 黑客技术 > 正文内容

string的常用方法

访客3年前 (2022-02-25)黑客技术1030

好程序员Java学习路线分享Java-String常用方法,String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的final类,不能继承。String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间。

好程序员Java培训

二、String类构造方法

1、public String()

无参构造方法,用来创建空字符串的String对象。

String str1 = new String();

String str2 = new String("asdf");

2、public String(String value)

String str2 = new String("asdf");

3、public String(char[] value)

char[] value = {'a','b','c','d'};

String str4 = new String(value);

4、public String(char chars[], int startIndex, int numChars)

char[] value = {'a','b','c','d'};

String str5 = new String(value, 1, 2);

5、public String(byte[] values)

byte[] strb = new byte[]{65,66};

String str6 = new String(strb);

三、String类常用方法

1、public char charAt(int index)

参数

index -- 字符的索引。

返回值

返回指定索引处的字符。

实例

public class Test {

public static void main(String args[]) {

String s = "www ";

char result = s.charAt(1);

System.out.println(result);

}

}

以上程序执行结果为:

w

2、public boolean equals(Object anObject)

参数

anObject -- 与字符串进行比较的对象。

返回值

如果给定对象与字符串相等,则返回 true;否则返回 false。

实例

public class Test {

public static void main(String args[]) {

String Str1 = new String("run");

String Str2 = Str1;

String Str3 = new String("run");

boolean retVal;

retVal = Str1.equals( Str2 );

System.out.println("返回值 = " + retVal );

retVal = Str1.equals( Str3 );

System.out.println("返回值 = " + retVal );

}

}

以上程序执行结果为:

返回值 = true

返回值 = true

3、public boolean endsWith(String suffix)

endsWith() 方法用于测试字符串是否以指定的后缀结束。

参数

suffix -- 指定的后缀。

返回值

如果参数表示的字符序列是此对象表示的字符序列的后缀,则返回 true;否则返回 false。注意,如果参数是空字符串,或者等于此 String 对象(用 equals(Object) 方法确定),则结果为 true。

实例

public class Test {

public static void main(String args[]) {

String Str = new String("runooo");

boolean retVal;

retVal = Str.endsWith( "run" );

System.out.println("返回值 = " + retVal );

retVal = Str.endsWith( "ooo" );

System.out.println("返回值 = " + retVal );

}

}

以上程序执行结果为:

返回值 = false

返回值 = true

4、public boolean equalsIgnoreCase(String anotherString)

equalsIgnoreCase() 方法用于将字符串与指定的对象比较,不考虑大小写。

参数

anObject -- 与字符串进行比较的对象。

返回值

如果给定对象与字符串相等,则返回 true;否则返回 false。

public class Test {

public static void main(String args[]) {

String Str1 = new String("run");

String Str2 = Str1;

String Str3 = new String("run");

String Str4 = new String("RUN");

boolean retVal;

retVal = Str1.equals( Str2 );

System.out.println("返回值 = " + retVal );

retVal = Str3.equals( Str4);

System.out.println("返回值 = " + retVal );

retVal = Str1.equalsIgnoreCase( Str4 );

System.out.println("返回值 = " + retVal );

}

}

以上程序执行结果为:

返回值 = true

返回值 = false

返回值 = true

5、public String replace(char oldChar,char newChar)

replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。

参数

oldChar -- 原字符。

newChar -- 新字符。

返回值

替换后生成的新字符串。

public class Test {

public static void main(String args[]) {

String Str = new String("hello");

System.out.print("返回值 :" );

System.out.println(Str.replace('o', 'T'));

System.out.print("返回值 :" );

System.out.println(Str.replace('l', 'D'));

}

}

以上程序执行结果为:

返回值 :hellT

返回值 :heDDo

6、public String toLowerCase()

toLowerCase() 方法将字符串转换为小写。

参数

返回值

转换为小写的字符串。

public class Test {

public static void main(String args[]) {

String Str = new String("WWW");

System.out.print("返回值 :" );

System.out.println( Str.toLowerCase() );

}

}

以上程序执行结果为:

返回值 :www

扫描二维码推送至手机访问。

版权声明:本文由黑客接单发布,如需转载请注明出处。

本文链接:https://www.therlest.com/3120.html

标签: 常用
分享给朋友:

“string的常用方法” 的相关文章

天猫双十一活动什么时候开始华流

以前提到双十一那都是光棍才过的节日,而现在双十一摇身一变成了全民购物狂欢节。在双十一期间以淘宝天猫为主的购物平台都会推出各种优惠活动以及满减折扣,可以算得上是全年最便宜的时候了。那么天猫双十一活动什么时候开始呢?下面就跟百思特小编来详细了解一下2020年天猫双十一开始时间吧!...

黑客了解,中国黑客越南网络apt,黑客网站密码破解工具

6.42 · hxxp[://]offlineearthquake[.]com/file//?id=&n=000 进程三:使命履行及实时数据剖析10.61 2019年6月19日,FireEye Endpoint Security设备上收到了缝隙检测警报。 违规应用程序被辨认为Microso...

安宫牛黄丸现在价格 - 天地和堂安宫牛黄丸多少钱

一丸280元左右.装同仁堂安宫牛黄丸,那个年代的这个玩意,中医说法多少,体外培育牛黄。 两丸,字[2000]201号\制定安宫牛黄丸等药品价格的批复,到正规的同仁堂自己品牌的药店购买,价格不一样,希望懂得.天然牛黄天然麝香。北京同仁堂牛黄解毒丸的市场价格在2005年。 绿色木盒这三种包装的价格一样,...

宏观经济学价格效应 「经济学传递效应」

饮水机效应,价格效应可分解为替代效应和收入效应。通货膨胀的经济效应主要有两个方面,中提出了著名的“节约悖论,比如买切糕,1元钱买了一块切糕,2,利息和货币通论,微观经济学中的价格效应中的一个问题。 “挤出”效应,政府购买和转移支付对我们经济的影响,加剧了收入分配的不均等化进一步加剧。微观经济学,即不...

Webshell安全检测篇

0x00 依据流量的检测办法 1.概述 笔者一直在重视webshell的安全剖析,最近就这段时刻的心得体会和咱们做个共享。 webshell一般有三种检测办法: 依据流量方法 依据agent方法(本质是直接剖析webshell文件) 依据日志剖析方法 Webshell的分...

威海海景房房价走势 - 山东威海海景房骗局

我是在乳山银滩买房的,多谢啦!可能是真实情况。 晚上夕阳衬着大海格外美丽,石岛房子要比银滩强的多。骗局倒是谈不上,是一个新兴的旅游区的新城,一般购房者以外地居民多,估计也是房子价格的一部分吧,那收入会更高,我家刚在D区买了房子,环境以及二十多公里的原生态沙滩形成。 但都在下面县级市的镇的郊区.听老妈...

评论列表

野欢岁吢
3年前 (2022-07-28)

ing(String value) String str2 = new String("asdf"); 3、public String(char[] value) char[] value = {'a','b','c','d'}; String str4 = new String(value

痴者寂星
3年前 (2022-07-28)

写的字符串。 public class Test { public static void main(String args[]) { String Str = new String("WWW"); System.out.

拥嬉沐白
3年前 (2022-07-28)

g("hello"); System.out.print("返回值 :" ); System.out.println(Str.replace('o', 'T')); System.out.print("返回值 :" ); Sys

美咩谜兔
3年前 (2022-07-28)

好程序员Java学习路线分享Java-String常用方法,String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。ja

痴妓喜余
3年前 (2022-07-28)

public static void main(String args[]) { String Str = new String("runooo"); boolean retVal; retVal

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。