Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Java Learning: elegant string     - innodb storage engine backup tool --Xtrabackup (Database)

- How to run Docker client in Windows operating system (Linux)

- Java Database Programming JDBC configuration (Programming)

- Use Visual Studio Code Development TypeScript (Linux)

- How to use the command line ftp upload and download files (Linux)

- Linux environment has been running Tomcat how to deploy the new Tomcat (Server)

- MongoDB query timeout exception SocketTimeoutException (Database)

- How do I use Linux development environment (Linux)

- Linux maximum number of threads and limit the number of queries the current thread (Linux)

- Git Installation and Configuration (Network Agent settings) (Linux)

- Ceph cluster disk is no workaround for the remaining space (Server)

- Connect to the Oracle Database Help class (Database)

- C ++ Supplements - Virtual Function Principle (Programming)

- Some security configuration of Linux systems (Linux)

- Compare Several MySQL environmental issues (Database)

- XenServer virtual machines installed in dual-card configuration (Server)

- Easy to get hidden administrator account (Linux)

- Nginx-1.9.7 TCP reverse proxy (Server)

- The Rabbitmq installation under CentOS 6.4 (Linux)

- Workspace Go language and environment variables GOPATH (Linux)

  Java Learning: elegant string
  Add Date : 2018-11-21      

Whether we believe that the use of Java language to develop projects or for data analysis and processing, to be applied to the string and associated processing methods. The society has everywhere and string associated shadow: logs, documents, books and so on. Since we can not do without a string, then to get to know about it.

How to construct a string?

In the Java language, there are two ways to create a string object: First, the amount of direct string

1 String message = new String ( "Welcome to Java");
2 String message2 = "Welcome to Java";
Second, through an array of characters.

1 char [] array = { 'G', 'o', 'o', 'd', '', 'D', 'a', 'y'};
2 String message3 = new String (array);
Note: String variables are stored is a reference to a String object, the value of the String object is stored in the string. So strictly speaking, the term String variable, String objects and string values are different, we usually just for simplicity, are collectively referred to as a string only.

Immutable? So pull the string!

String objects are immutable, its contents can not be changed. For example, the following two lines of code:

1 String s = "Java";
2 s = "html";
The first statement creates a String object contents as "Java" and will assign its reference to s. The second statement creates a new String object content "html", and will assign it to s. So, after the implementation of these two statements, two String objects have not changed, and the contents of which have not changed, just the last point to the variable s new String object.

String comparison

Once you see the comparison of the word, we think of "==." However, the operator == can only detect strings String1 and String2 point to the same object, but it does not tell us whether they are the same content. So Java determine which strings are the same, we should use the method equals or compareTo methods.

1 String s1 = new String ( "Welcome to Java");
2 String s2 = "Welcome to Java";
3 String s3 = "Welcome to C ++";
4 String s4 = "welcome TO java";
5 System.out.println (s1.equals (s2));
6 System.out.println (s1.equals (s3));
7 System.out.println (s1.compareTo (s2));

NOTE: When comparing two strings are equal, when the method can be ignored equalsIgnoreCase and compareToIgnoreCase capitalization and compare.

System.out.println (s1.equalsIgnoreCase (s4));
System.out.println (s2.compareToIgnoreCase (s4));

String length, characters, and a combination of strings

length is a method of the String class, but it is an attribute array object, so we need to get the number of characters in the string s, it is necessary to use s.length (). Methods s.charAt (index) can be used to extract a particular character string s, where the subscript index in the range of 0 to s.length () - between 1.

In addition, we can use the concat method to connect two strings. As shown in the following code, the strings s1 and s2 connected to form a s3.

1 String ss = "BaiYi";
2 String ss2 = "ShaoNian";
3 String ss3 = ss.concat (ss2);
4 System.out.println (ss3);

Of course, like we usually prefer a more simple way, that is the direct use of the plus sign (+) to connect two or more strings to form a new string.

How to get a substring of it?

As already mentioned we can get through any string method charAt a single character, but it's a substring should get how to do it? We use the substring method.

1 System.out.println ( "Welcome to Java" .substring (0,11) + "C ++");
String conversion, replacement and separation methods, learn ing ...

toLowerCase: Returns all of the characters are converted to lowercase after the new string

toUpperCase: Returns all characters are converted to uppercase after a new string

Returns a new string to remove whitespace characters at both ends after: trim

replace: Back with a new substring replace this string and it matches all characters the new string

replaceFirst: Returns the substring with a new string to replace the new string after string and it matches the first child

replaceAll: Back with a new substring replaced by the new string after string this string and it matches all sub

split: Returns an array of strings separated by a delimiter substring posed

 1 System.out.println ( "Welcome TO" .toLowerCase ());
 2 System.out.println ( "Welcome to" .toUpperCase ());
 3 System.out.println ( "Welcome to" .trim ());
 4 System.out.println ( "Welcome to" .replace ( 'e', 'A'));
 5 System.out.println ( "Welcome to" .replaceFirst ( "e", "ABC"));
 6 System.out.println ( "Welcome to" .replaceAll ( "e", "AB"));
 . 8 String [] str = "Java # HTML # C ++" split ( "#", 0);
 9 for (int i = 0; i < str.length; i ++) {
10 System.out.println (str [i] + "");
. 12 String [] str2 = "Java # HTML & C ++" split ( "[# &]", 0);
13 for (int i = 0; i < str2.length; i ++) {
14 System.out.println (str2 [i] + "");
- Differences Shell scripting languages and compiled languages (Programming)
- To resolve Ubuntu 14.04 Unity Desktop Environment Login freeze problem (Linux)
- CentOS Linux Optimization and real production environment (Linux)
- MySQL tmpdir parameter modification (Database)
- Docker commonly used commands Description (Linux)
- OpenSSH server configuration file for each Common Definition (Server)
- Several reasons MySQL garbled (Database)
- Linux system security knowledge (Linux)
- CentOS 6.5 installation VNCServer implement graphical access (Server)
- How to clear the v $ archived_log view expiration information (Database)
- Ubuntu 15.10 under Python + Apache + CGI fully configured (Server)
- Install FFmpeg compiling from source in Mac OS X environment (Linux)
- Talking about modern programming language syntax and standard library tightly bound phenomenon (Programming)
- Linux support exFAT and NTFS (Linux)
- Linux, C programming language library file handling and preparation of Makefile (Programming)
- Golang environment configuration recommendations (Linux)
- Linux operation and maintenance of the actual file system, file links (Linux)
- How to build Memcached Docker container (Server)
- Five useful commands to manage file types and system time in linux (Linux)
- Distributed transaction management Spring declarative transactions (Programming)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.