当前位置:网站首页>Common methods of string class
Common methods of string class
2022-06-29 20:49:00 【zhengmayusi】
About String Construction method of class
- String s = new String(”“);
- String s = ”“; The most commonly used
- String s = new String (char Array )
- String s = new String ( char Array , Start subscript , length )
- String s = new String(byte Array )
- String s = new String(byte Array , Start subscript , length )
Code as follows :
// Here we only master the common construction methods
// One of the most common ways to create string objects
String s1 = "hello world!";
// s1 It stores a memory address , It is supposed to output an address
// But output a string , explain String The class has been rewritten toString Method
System.out.println(s1);
// Only the most common construction methods are mastered here
byte[] bytes = {
97,98,99}; // 97 yes a,98 yes b,99 yes c(byte Arrays do these conversions automatically )
String s2 = new String(bytes); // Pass a byte Array
// As I said before : When outputting a reference , Automatically called toString() Method
// Default Object Words , The memory address of the object will be automatically output
// Through the output results, we come to a conclusion : String The class has been rewritten toString() Method
// If you output a string object , The output is not the memory address of the object , It's the string itself
System.out.println(s2); // abc
// String( Byte array , The starting position of the subscript of the array element , length )
// take byte Part of the array is converted into a string
String s3 = new String(bytes,1,2);
System.out.println(s3); // bc
// take char All arrays are converted into strings
char[] chars = {
' I ',' yes ',' in ',' countries ',' people '};
String s4 = new String(chars);
System.out.println(s4);
// take char Convert a part of the array into a string
String s5 = new String(chars,2,3);
System.out.println(s5);
String s6 = new String("hello world!");
System.out.println(s6);
About String Common methods of class
String Class is often used in business , It is recommended to master , But you can also query API Document to solve !
public class StringTest05 {
public static void main(String[] args) {
// String Common methods in classes
// charAt(int index) // Put back the char value
char c = " Chinese ".charAt(1); // " Chinese " Is a string string object . As long as it's an object "."
System.out.println(c); // countries
// 2 ( understand ) .int compareTo(String anotherString)
// Compare... In dictionary order
int result = "abc".compareTo("abc");
System.out.println(result); //0( be equal to 0) coherence
int result2 = "abcd".compareTo("abce");
System.out.println(result2); //-1( Less than 0) Small in front and big in back
int result3 = "abce".compareTo("abcd");
System.out.println(result3); //1( Less than 0) Big in front and small in back
// Compare the first letter of the string with the first letter of the following string . There is no comparison between victory and defeat .
System.out.println("xyz".compareTo("yxz")); // -1
// 3 ( master ) .boolean contains(CharSequence s)
System.out.println("hello world".contains("hello"));
// 4( master ) boolean endsWith()
// Determine whether the current string ends with a string
System.out.println("test.txt".endsWith(".java")); // false
System.out.println("test.txt".endsWith("xt")); // true
// 5( master ) .boolean equals(Object object)
// To compare two strings, you must use equals Method , Out of commission "=="
// The underlying comparison principle is to take out characters one by one for comparison
// equals Methods can only tell if they are equal
// compareTo The method can also tell who is big and who is small
System.out.println("abc".equals("abc"));
// 6.equalsIgnoreCase(String anotherString) Judge whether two strings are equal, ignore case
System.out.println("Abc".equalsIgnoreCase("abc"));
// 7.( master ).byte[] getBytes()
// Convert a string object into a byte array
byte[] bytes = "abcdef".getBytes();
for (byte s:
bytes) {
System.out.println(s);
}
// 8.indexOf(int ch)
// Returns the index in the string where the specified character first appears .
System.out.println("helloworld".indexOf("world"));
// 9. Determine whether a string is empty .boolean isEmpty() The underlying source code should call the string length
System.out.println(" ".isEmpty());
// 10.int length()
// Interview questions : The length of the judgment array is different from that of the judgment string
// Determine the length of the array is length attribute , Determine the length of the string is length() Method
System.out.println("abc".length());
// 11.lastIndexOf(int ch) Returns the index of the last occurrence of a substring in the current string ( Subscript )
System.out.println("hellohelloworldwor;ld".lastIndexOf("wor"));
// 12.replace(CharSequence target, CharSequence replacement)
// Use... In the current string replacement Replace... In the current string target
// String The parent interface of is : CharSequence
String newString = "http://www.baidu.com".replace("http://", "https://");
System.out.println(newString);
// 13.String[] split(String regex)
// Split string
String[] split = "1980-10-11".split("-"); // With - Split string for delimiter
for (String s:
split) {
System.out.println(s);
}
// 14.startsWith(String prefix)
// Determine whether the current string starts with a string
System.out.println("hello world".startsWith("hello")); // true
System.out.println("hello world".startsWith("hellh")); // false
// 15.substring(int beginIndex) // The parameter is the starting subscript
// Intercepting string
System.out.println("https://www.baidu.com".substring(7)); // From index to 7 Start intercepting the string at the position of
// 16.String substring(int beginIndex,int endIndex) Start and end subscripts
// Intercepting string
System.out.println("https://www.baidu.com".substring(8,15)); // Left closed right away
// 17.char[] toCharArray()
// Converts a string to char Array
char[] chars = " I'm Chinese ".toCharArray();
for (char s1:chars){
System.out.println(s1);
}
// 18.String toLowerCase()
// Convert string to lowercase
System.out.println("HELLO WORLD".toLowerCase());
// 19.String toUpperCase()
// Convert strings to uppercase
System.out.println("hello world".toLowerCase());
// 20.String trim()
// Remove strings Before and after The blank of
System.out.println(" hello world ".trim());
// .String Only one method in is static , Unwanted new object
// 21. This method is called valueOf effect : take ” The string “ convert to " character string "
String s1 = String.valueOf(true);
System.out.println(s1);
String s2 = String.valueOf(new Customer());
System.out.println(s2); // No rewriting toString The object memory address is printed before the , Because of the call toString The method is the parent class Object Class toString Method
// Look at the println() Method source code
System.out.println(100);
System.out.println(3.14);
System.out.println(true);
Object o = new Object();
// You can see from the source code that : Why do I output a reference , Would call toString() Method
// Essentially System.out.println() This method In essence, the target is converted into a string and then output
System.out.println(o);
}
}
class Customer{
// No rewriting toString Method Would call Object Class toString Method What you print is the address
@Override
public String toString() {
return "Customer{}";
}
}
边栏推荐
- 计算成像前沿进展
- 管理人员应具备的基本素质
- Mysql Json 数据类型&函数
- 60 days of remote office experience sharing | community essay solicitation
- 「运维有小邓」审核并分析文件和文件夹访问权限
- How to call RFC function of ABAP on premises system directly in SAP BTP ABAP programming environment
- 注解
- Nutch2.1在Windows平台上使用Eclipse debug 存储在MySQL的搭建过程
- String类的常用方法
- Nutch2.1 distributed fetching
猜你喜欢

mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总

High energy live broadcast, a gathering of celebrities! We invite you to explore bizdevops.

Win10 sets automatic dial-up networking task to realize automatic reconnection after startup and disconnection

分析影响导电滑环传输信号的因素

WIN10设置自动拨号联网任务,实现开机、断网自动重连

Mysql Json 数据类型&函数

STL教程6-deque、stack、queue、list容器

解释PBR纹理贴图(texture-maps)

String字符串的存储原理

18. `bs object Node name next_ sibling` previous_ Sibling get sibling node
随机推荐
Defense cornerstone in attack and defense drill -- all-round monitoring
Win10 sets automatic dial-up networking task to realize automatic reconnection after startup and disconnection
Exercise 8 Chapter 8 Verilog finite state machine design -4 Verilog quartus Modelsim
时钟树综合(CTS)
Oracle保留字查询
. NETCORE unified authentication authorization learning - first authorization (2)
「运维有小邓」实时监控用户登录操作
How do I audit Active Directory User account changes?
Golang基础学习
Set up your own website (12)
CorelDRAW2022全新版V24.1.0.360更新
使用Gunicorn部署web.py应用
tmux设置
Navigation exercises [microcomputer principles] [exercises]
2021 CCPC 哈尔滨 J. Local Minimum (思维题)
《强化学习周刊》第51期:PAC、ILQL、RRL&无模型强化学习集成于微电网络格控制:综述与启示
「运维有小邓」日志分析工具使用越来越频繁的原因
mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总
WIN10设置自动拨号联网任务,实现开机、断网自动重连
Curl download example