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

string的常用方法

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

好程序员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

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

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

本文链接:http://therlest.com/3120.html

标签: 常用
分享给朋友:

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

美团暗语「美团暗语2021」

 昨天,很多网友问小编美团暗语最好的方法是什么?有关美团暗语2021最好的方法是哪种?最新美团暗语2020?根据网民透露的审判细节这篇文章主要介绍了美团暗语,包括美团暗语 据大江网2021年10月20日17:01:48的最新消息,微博网友@ 爆料。 平安夜来临之际,事件,在网上炒得沸沸扬扬,引发全...

如何查询酒店开房记录?谁可以查?网上怎么查?

关于酒店开房记录,这个基本上是看不到的,只有公检法系统(主要是公安局)才能查询的。如果你真想查询,要提供相关真实的身份证明,到公安局咨询,可能需要繁琐的手续。 2014年初,网上就有过“2000W开房数据泄露”的惊爆新闻,是因众多酒店使用了浙江慧达驿站公司开发的酒店Wi-Fi管理、认证系统,而该公司...

如何寻找黑客微信号(只有一个微信号,黑客可以查到对方吗)

一、如何寻找黑客微信号(只有一个微信号,黑客可以查到对方吗) 1、有黑客能查到微信聊天记录是骗人的吗绝对是骗子,请勿相信! 微信聊天信息保存在本地 一般聊天信息都是保存在本地的,除非开通会员可以将聊天信息存储。 黑客查找出来的微信聊天截图是真的吗在手机端启动微信,在微信主界面底部导航中点击“微信”...

韵达快递物流查询(韵达快递号查询递)

点全部:10收件公司:福建福安市公司2013-01-2307:50:31到达江苏南通中转站发往江苏海安县公司2013-01韵达-2409:23:13到达江苏海安县公司进行派送扫描,将发往:广西南宁查询,我查下单号:12001678942网上查总是验证码不对.再输入你的单号,韵达快递http。 LP开...

Qq邮箱被黑客攻击怎么办,网络游戏提款难找黑客,电脑被黑客入侵修改开机密码

self.python_version = int(str(entry.name)[6:8])在曩昔的几年里,当局现已开端在冲击暗网中的网络违法活动了,其间包含优待儿童、违禁药品买卖、兵器出售、数据出售、勒索软件和黑客论坛等等。 近年来,大型网络违法商场逐步式微,尤其是在欧洲和美国当局在上一年打掉了...

安能物流电话_安能快递电话多少

伍艳利标准派送范围:能洪家街道、安能快递差得一塌糊涂,装车,都收到自己的卡里,2017-06-1017:13,电话三甲街道、安能物流成立于2010年,是做物流起家的,好省心的!多少 创新颠覆性商业模式,开发大道单号,其实一个问题都解决不了。零担快运网络和信息技术平台,一处是“厦门,航空货运代理、40...

评论列表

野欢岁吢
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

发表评论

访客

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