好程序员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
Atlassian解决了其Jira Seraph软件中的一个严重漏洞,该漏洞编号为CVE-2022-0540(CVSS 评分 9.9),未经身份验证的攻击者可以利用该漏洞绕过身份验证。威胁参与者可以通过向易受攻击的软件发送特制的HTTP 请求来触发漏洞。 该漏洞会对Jira软件多版本产生影响,比如...
对于IT管理员和网络安全团队来说,勒索软件攻击是一场与时间赛跑的关键比赛,以检测和控制损害,同时抢救公司的数据资产的剩余部分。但是,当这种事件发生时,有多少反应时间呢?似乎不是很多。正如对10个候选勒索软件的测试所揭示的那样,一种名叫LockBit的勒索软件效率惊人,在四分钟内就加密了一台Windo...
在俄乌冲突爆发后的一个月时间里,许多组织机构纷纷颁布了针对俄罗斯的制裁。但是对于开源社区来说,将制裁的范围无限扩大,显然并不是声援乌克兰的最佳途径。开放源码倡议(Open Source Initiative)组织指出:尽管大多数软件开发者只是在运行时显示反战或亲乌克兰的信息,但还是有个别项目的维护者...
欧盟准备在周五公布一项具有里程碑意义的法律,该法律将迫使大型科技公司更积极地监管其平台的非法内容,这是监管机构遏制大型科技集团权力的最新举措。 据四位知情人士透露,《数字服务法》(DSA)将禁止根据用户的宗教信仰、性别或性取向对用户进行分类和内容定位。DSA是一个立法方案,首次为大型科技公司如何保证...
据称,一个黑客组织泄露了微软37GB的源代码,这些代码与包括Bing和Cortana在内的数百个项目有关,这是一系列重大网络安全事件中的最新一起。Lapsus$黑客组织在周一晚上公开发布了一个9GB的压缩文件。据称,该7zip档案包含了从微软获得的250多个内部项目。 据称这些数据来自微软的Az...
近段时间,一轮新的 BitRAT 恶意软件活动正在加速传播。手段是利用非官方的微软许可证激活器,来激活盗版 Windows 操作系统。Bleeping Computer 指出,BitRAT 是一款功能强大的远程访问木马。在网络犯罪论坛和暗网市场上,它正以 20 美元的买断价,向网络犯罪分子们兜售。...