publicclassMain { publicstaticvoidmain(String[] args)throws UnsupportedEncodingException { // 字面量 创建字符串 Stringstr="PHP is the best language all of the world!"; Stringstr_1=newString("A cup of Java"); byte[] b = str.getBytes("UTF-8"); Stringstr_2=newString(b, "UTF-8"); System.out.println(str_2); } }
一些方法: public String(StringBuffer buffer) public String(StringBuilder buffer) public String substring(int beginIndex, int endIndex) public String toUpperCase() public String toLowerCase() public String trim() public boolean isEmpty() public String concat(String str) public String replace(char oldChar, char newChar) public char charAt(int index) public static String valueOf(double d) 将参数转字符串
…
查找 public int indexOf(int ch) public int indexOf(int ch, int fromIndex) public int indexOf(String str) public int indexOf(String str, int fromIndex) public int lastIndexOf(int ch) public int lastIndexOf(int ch, int endIndex) public int lastIndexOf(String str) public int lastIndexOf(String str, int endIndex)
…
字符串转数组 public char[] toCharArray() public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) public byte[] getBytes() public byte[] getBytes(String charsetName)
…
字符串比较 public boolean equals(String anotherString) public boolean equalsIgnoreCase(String anotherString) public int compareTo(String another) 比较Unicode值 public int compareToIgnoreCase(String anotherString) public boolean startsWith(String prefix) public boolean endsWith(String suffix) public boolean contains(String str)
字符串拆分与组合 public String[] split(String regex) public boolean matches(String regex) public static String join(CharSequence delimiter, CharSequence ...elements)
命令行参数
public static void main(String []args){} public static void main(String ...args){}
用CMD运行的时候指定, 或者在IDEA中可以,
Apply应用后在运行就可以了.
格式输出
public PrintStream printf(String format, Object ...args)
public static String format(String format, Object ...args) public static String format(Locale l, String format, Object ...args)
这两个方法的功能是按照参数指定的格式,将args格式化成字符串返回。此外,在java.io.PrintStream类、java.io.PrintWriter类以及 java.util.Formatter类中都提供了相应的format()方法。它们的不同之处是方法的返回值不同。在各自类中的 format()方法返回各自类的一个对象.
public StringBuilder()
创建一个没有字符的字符串缓冲区,初始容量为16个字符。此时length()方法的值为0,而capacity()方法的值为16 public StringBuilder(int capacity) public StringBuilder(String str)
利用一个已存在的字符串对象 str创建一个字符串缓冲区对象,另外再分配16个字符的缓冲区
StringBuilder类除定义了 length()、charAt()、indexOf()、getChars()等方法外,还提供
了下列常用方法 public int capacity() public void setCharAt(int index,char ch) public StringBuilder append(String str) public StringBuilder insert(int offset, String str) public StringBuilder deleteCharAt(int index) public StringBuilder delete(int start, int end) public StringBuilder replace(int start, int end, String str) public StringBuilder reverse() public String substring(int start) public String substring(int start, int end) public void setLength(int newLength)
更多见文档 https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/StringBuilder.html