📅  最后修改于: 2020-03-25 01:20:12             🧑  作者: Mango
字符串是字符序列。在Java中,String对象是不可变的,这意味着一个常量,并且一旦创建就不能更改。
创建一个字符串
有两种在Java中创建字符串的方法:
String s = “芒果文档";
String s = new String (“芒果文档");
构造器
byte[] b_arr = {71, 101, 101, 107, 115};
String s_byte =new String(b_arr); //芒果
byte[] b_arr = {71, 101, 101, 107, 115};
Charset cs = Charset.defaultCharset();
String s_byte_char = new String(b_arr, cs); //芒果
byte[] b_arr = {71, 101, 101, 107, 115};
String s = new String(b_arr, "US-ASCII"); //芒果
byte[] b_arr = {71, 101, 101, 107, 115};
String s = new String(b_arr, 1, 3); // eek
byte[] b_arr = {71, 101, 101, 107, 115};
Charset cs = Charset.defaultCharset();
String s = new String(b_arr, 1, 3, cs); // eek
byte[] b_arr = {71, 101, 101, 107, 115};
String s = new String(b_arr, 1, 4, "US-ASCII"); // eeks
char char_arr[] = {'G', 'e', 'e', 'k', 's'};
String s = new String(char_arr); //芒果
char char_arr[] = {'G', 'e', 'e', 'k', 's'};
String s = new String(char_arr , 1, 3); //eek
int[] uni_code = {71, 101, 101, 107, 115};
String s = new String(uni_code, 1, 3); //eek
StringBuffer s_buffer = new StringBuffer("芒果");
String s = new String(s_buffer); //芒果
StringBuilder s_builder = new StringBuilder("芒果");
String s = new String(s_builder); //芒果
字符串方法
"芒果文档".length(); // 返回4
"芒果文档".charAt(3); // 返回 ‘档’
"芒果文档".substring(3); // 返回 “档"
"芒果文档".substring(2, 4); // 返回 “文档"
String s1 = "芒果";
String s2 = "文档";
String output = s1.concat(s2); // 返回 “芒果文档"
String s = "Learn Share Learn";
int output = s.indexOf(“Share"); // 返回 6
String s = "Learn Share Learn";
int output = s.indexOf("ea",3);// 返回 13
String s = "Learn Share Learn";
int output = s.lastIndexOf("a"); // 返回 14
Boolean out = “芒果".equals(“芒果"); // 返回 true
Boolean out = “芒果".equals(“文档"); // returns false
Boolean out= “芒果".equalsIgnoreCase(“芒果"); // returns true
Boolean out = “a".equalsIgnoreCase(“A"); // returns true
int out = s1.compareTo(s2); // 比较s2和s2
This returns difference s1-s2. If :
out < 0 // s1先于s2
out = 0 // s1 = s2.
out > 0 // s1后于s2.
int out = s1.compareToIgnoreCase(s2);
// 比较s2和s2
This returns difference s1-s2. If :
out < 0 // s1先于s2
out = 0 // s1 = s2.
out > 0 // s1后于s2.
注意:在这种情况下,它将不考虑字母的大小写(它将忽略大写还是小写)。
String word1 = “HeLLo";
String word3 = word1.toLowerCase(); // 返回 “hello"
String word1 = “HeLLo";
String word2 = word1.toUpperCase(); // 返回 “HELLO"
String word1 = “ Learn Share Learn “;
String word2 = word1.trim(); // 返回 “Learn Share Learn"
String s1 = “芒果文档“;
String s2 = “芒果文档".replace(‘果’ ,’芒’); // 返回 “芒芒文档"
用来说明所有字符串方法的程序:
import java.io.*;
import java.util.*;
class Test
{
public static void main (String[] args)
{
String s= "GeeksforGeeks";
// or String s= new String ("GeeksforGeeks");
// Returns the number of characters in the String.
System.out.println("String length = " + s.length());
// Returns the character at ith index.
System.out.println("Character at 3rd position = "
+ s.charAt(3));
// Return the substring from the ith index character
// to end of string
System.out.println("Substring " + s.substring(3));
// Returns the substring from i to j-1 index.
System.out.println("Substring = " + s.substring(2,5));
// Concatenates string2 to the end of string1.
String s1 = "Geeks";
String s2 = "forGeeks";
System.out.println("Concatenated string = " +
s1.concat(s2));
// Returns the index within the string
// of the first occurrence of the specified string.
String s4 = "Learn Share Learn";
System.out.println("Index of Share " +
s4.indexOf("Share"));
// Returns the index within the string of the
// first occurrence of the specified string,
// starting at the specified index.
System.out.println("Index of a = " +
s4.indexOf('a',3));
// Checking equality of Strings
Boolean out = "Geeks".equals("geeks");
System.out.println("Checking Equality " + out);
out = "Geeks".equals("Geeks");
System.out.println("Checking Equality " + out);
out = "Geeks".equalsIgnoreCase("gEeks ");
System.out.println("Checking Equality " + out);
int out1 = s1.compareTo(s2);
System.out.println("If s1 = s2 " + out);
// Converting cases
String word1 = "GeeKyMe";
System.out.println("Changing to lower Case " +
word1.toLowerCase());
// Converting cases
String word2 = "GeekyME";
System.out.println("Changing to UPPER Case " +
word1.toUpperCase());
// Trimming the word
String word4 = " Learn Share Learn ";
System.out.println("Trim the word " + word4.trim());
// Replacing characters
String str1 = "feeksforfeeks";
System.out.println("Original String " + str1);
String str2 = "feeksforfeeks".replace('f' ,'g') ;
System.out.println("Replaced f with g -> " + str2);
}
}
输出:
String length = 13
Character at 3rd position = k
Substring ksforGeeks
Substring = eks
Concatenated string = GeeksforGeeks
Index of Share 6
Index of a = 8
Checking Equality false
Checking Equality true
Checking Equality false
If s1 = s2 false
Changing to lower Case geekyme
Changing to UPPER Case GEEKYME
Trim the word Learn Share Learn
Original String feeksforfeeks
Replaced f with g -> geeksgorgeeks