1024programmer Java Common methods of organizing strings in Java programming

Common methods of organizing strings in Java programming

Characters
Generally, when we deal with characters, we use the primitive data type char.

Example

 char ch = 'a';

 // Unicode for uppercase Greek omega character
 char uniChar = '\u039A';

 // an array of chars
 char[] charArray ={ 'a', 'b', 'c', 'd', 'e' };

 

However, during development, we will encounter situations where we need to use objects instead of primitive data types. In order to meet this demand. Java provides a wrapper class Character for the primitive data type char.

The Character class provides a set of useful classes (for example: static classes) for manipulating characters. You can create a Character object with the help of the Character constructor.

 Character ch = new Character('a');
 

The Java compiler will also be able to create a Character object for you under certain circumstances. For example: if you transfer a raw char to a method of an expected object, the compiler will automatically convert the char to a Character for you. If the conversion is done in the opposite direction, this feature is called autoboxing or unboxing.

Example

 // Here following primitive char 'a'
 // is boxed into the Character object ch
 Character ch = 'a';

 // Here primitive 'x' is boxed for method test,
 // return is unboxed to char 'c'

 char c = test('x');

 

Escape sequences
The character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler.

The newline character (\n) is often used in the System.out.println() statement to break the string after it is printed.

The following table shows Java escape sequences:

Escape sequence Description
\t Inserts a tag into the text.
\b Inserts a backspace into the text.
\n Inserts a newline character into the text.
\r Inserts a carriage return into the text.
\f Inserts a page break into the text.
\’ Inserts a single quote character into the text.
\\ Inserts a backslash character into the text.

When an escape sequence encounters a print statement, the compiler interprets it accordingly.

Example

If you want to put quotes inside quotes, you must use the escape sequence, \” , to quote internally:

 public class Test {

  public static void main(String args[]) {
   System.out.println("She said \"Hello!\" to me.");
  }
 }

 

This will produce the following results:

She said “Hello!” to me.
Character method
The following list is the important instance methods implemented by all subclasses of the Character class:

SN Method description
1 isLetter()
Determine whether the specific char value is a letter
2 isDigit()
Determine whether the specific char value is a number
3 isWhitespace()
Determine whether the specific char value is a space
4 isUpperCase()
Determine whether the specific char value is an uppercase letter
5 isLowerCase()
Determine whether the specific char value is a lowercase letter
6 toUpperCase()
Returns the uppercase version of the specified character value
7 toLowerCase()
Returns the lowercase form of the specified character value
8 toString()
Returns a String object representing the specified character value, that is, a character string

String
A
string, which is widely used in Java programming, is a sequence of characters. In the Java programming language, strings are objects.

The Java platform provides the String class to create and manipulate strings.

Create string
The most direct way to create a string is to write like this:

String greeting = “Hello world!”;
When you create a string, the compiler creates a String object with its value in this case, like: “Hello world!’.

Any other object can be created from a String object by using the new keyword and through the constructor. The String class has 11 constructors that provide initial values ​​using different types of strings, such as a character array.

 public class StringDemo{

public static void main(String args[]){
char[] helloArray = { 'h', 'e', ​​'l', 'l', 'o', '.'};
String helloString = new String(helloArray);
S�This String decodes a byte sequence using the specified character set and stores the result into a new byte array. 14 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copies characters from this string to the target character array. 15 int hashCode()
Returns a hash code for this string. 16 int indexOf(int ch)
Returns the index of the first occurrence of the specified character in this string. 17 int indexOf(int ch, int fromIndex)
Returns the index where the specified character appears for the first time in this string, and starts searching at the specified index. 18 int indexOf(String str)
Returns the index of the first occurrence of the specified substring in this string. 19 int indexOf(String str,int fromIndex)
Returns the index of the first occurrence of the specified substring in this string, starting at the specified index. 20 String intern()
Returns the normalized representation of a string object. 21 int lastIndexOf(int ch)
Returns the index of the last occurrence of the specified character in this string. 22 int lastIndexOf(int ch, int fromIndex)
Returns the index of the last occurrence of the specified character in this string, searching backwards starting from the specified index. 23 int lastIndexOf(String str)
Returns the index of the rightmost occurrence of the specified substring in this string. 24 int lastIndexOf(String str, int fromIndex)
Returns the last occurrence of the specified substring in this string at index, searching backwards from the beginning of the specified index. 25 int length()
Returns the length of this string. 26 boolean matches(String regex)
Determines whether this string matches the given regular expression. 27 boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
Tests the area equality of two strings. 28 boolean regionMatches(int toffset, String other, int ooffset, int len)
Tests that the regions of both strings are equal. 29 String replace(char oldChar, char newChar)
Returns a new string produced by replacing all occurrences of oldChar in this string with newChar. 30 String replaceAll(String regex, String replacement
Replaces every substring in this string that matches the given regular expression with the given replacement. 31 String replaceFirst(String regex, String replacement)
Replaces the first substring of this string matching the given regular expression given. 32 String[] split(String regex)
Splits this string around the given regular expression match. 33 String[] split(String regex, int limit)
Splits this string around the given regular expression match. 34 boolean startsWith(String prefix)
Tests whether this string begins with the specified prefix. 35 boolean startsWith(String prefix, int toffset)
Tests whether this string starts with the specified prefix starting at the specified index. 36 CharSequence subSequence(int beginIndex, int endIndex)
Returns a new character sequence that is a subsequence of this sequence. 37 String substring(int beginIndex)
Returns a new string that is a substring of this string. 38 String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string. 39 char[] toCharArray()
This string is converted into a new character array. 40 String toLowerCase()
Lowercase all characters in this string using the default locale's rules. 41 String toLowerCase(Locale locale)
Lowercase all characters in this string using the given Locale's rules. 42 String toString()
This object (which is already a string!) returns the string form (here itself). 43 String toUpperCase()
Converts all characters in this String to uppercase using the rules of the default locale. 44 String toUpperCase(Locale locale)
Converts all characters in this String to uppercase using the rules of the given Locale. 45 String trim()
Returns a copy of the string with leading and trailing whitespace removed. 46 static String valueOf(primitive data type x)
Returns the string representation of the passed data type argument.

d>

String toString()
This object (which is already a string!) returns the string form (here itself). 43 String toUpperCase()
Converts all characters in this String to uppercase using the rules of the default locale. 44 String toUpperCase(Locale locale)
Converts all characters in this String to uppercase using the rules of the given Locale. 45 String trim()
Returns a copy of the string with leading and trailing whitespace removed. 46 static String valueOf(primitive data type x)
Returns the string representation of the passed data type argument.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/703494

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索