• 欢迎光临~

Java: Strings

开发技术 开发技术 2022-11-24 次浏览

String Methods:

String txt = "Hello World";
System.out.println(txt.toUpperCase());   // Outputs "HELLO WORLD"
System.out.println(txt.toLowerCase());   // Outputs "hello world"

 

String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate")); // Outputs 7

System.out.println(txt.indexOf("p")); // Outputs 0
System.out.println(txt.indexOf("l")); // Outputs 1

 

String firstName = "John ";
String lastName = "Doe";
System.out.println(firstName.concat(lastName));

 

String x = "10";
String y = "20";
String z = x + y;  // z will be 1020 (a String)

 

String x = "10";
int y = 20;
String z = x + y;  // z will be 1020 (a String)

 

Special Characters

Escape characterResultDescription
' ' Single quote
" " Double quote
\ Backslash
 

 

 

 

CodeResult 
n New Line

String txt = "HellonWorld!";
System.out.println(txt);

// Outputs 

Hello
World!

r Carriage Return

String txt = "HellorWorld!";
System.out.println(txt);

// Outputs 

Hello
World!

t Tab

String txt = "HellotWorld!";
System.out.println(txt);

// Outputs 

Hello    World!

b Backspace

String txt = "Helblo World!";
System.out.println(txt);

// Outputs 

Helo World!

 

程序员灯塔
转载请注明原文链接:Java: Strings
喜欢 (0)
违法和不良信息举报电话:022-22558618 举报邮箱:dljd@tidljd.com