源码
public String(char[] value)实例
char[] value ={"a","b","c","d"}; String str = new String(value); //相当于String str = new String("abcd")源码
public String(char chars[], int x, int n)实例
char[] array = {'a','b','c','d','e'}; String str1 = new String(array,2,3); //cd源码
public boolean startsWith(String prefix) { return startsWith(prefix, 0); }实例
String str1 = "abcde"; System.out.println(str1.startsWith("ab")); //true源码
public boolean endsWith(String suffix) //是否以指定后缀结束源码
public int length()实例
String str1 = "abcde"; System.out.println(str1.length()); //5源码
public char charAt(int index) { if ((index < 0) || (index >= value.length)) { throw new StringIndexOutOfBoundsException(index); } return value[index]; }实例
String str1 = "abcde"; char ch = str1.charAt(4); //e源码
public String substring(int beginIndex) //从beginIndex位置开始直到此字符串末尾的所有字符实例
String str1 = "abcde"; System.out.println(str1.substring(1)); //bcde源码
public String substring(int beginIndex, int endIndex) //从beginIndex处开始,到 endIndex-1处的所有字符实例
String str1 = "abcde"; System.out.println(str1.substring(1,4)); //bcd源码
public boolean equals(Object anObject)实例
String str1 = "abcde"; System.out.println(str1.equals("abcde"));//true源码
public int compareTo(String anotherString) //该方法是对字符串内容按字典顺序进行大小比较, //通过返回的整数值指明当前字符串与参数字符串的大小关系。 //若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。实例
String str1 = "abcde"; System.out.println(str1.compareTo("abcd")); //1 //==运算符用于比较对象equals和compareTo方法用于比较字符串的内容
源码
public int indexOf(String str) //返回指定字符串在此字符串中第一次出现处的索引 public int indexOf(String str, int fromIndex) //从指定索引开始查找指定字符串 public int lastIndexOf(String str) //返回指定字符串在此字符串中最后一次出现处的索引,从字符串的末尾位置向前查找 public int lastIndexOf(String str, int fromIndex) //与第二种方法类似,区别于该方法从fromIndex位置向前查找。源码
public String concat(String str) //将指定字符串连接到此字符串的结尾实例
s1 = s.concat("abc");源码
public String toLowerCase() //返回将当前字符串中所有字符转换成小写后的新字符串 public String toUpperCase() //返回将当前字符串中所有字符转换成大写后的新字符串源码
public String replace(char oldChar, char newChar) 用字符newChar替换当前字符串中oldChar字符 public String replace(CharSequence target, CharSequence replacement) //将字符串中的指定字符序列target替换为replacement源码
public String[] split(String regex) //根据参数regex将原来的字符串分割为若干个子字符串"abcdef" ===> "cdefab"
import java.util.Scanner; public class Demo6 { public static String reverse(String str, int begin, int end) { char[] ch = str.toCharArray(); char tmp; while (begin < end) { tmp = ch[begin]; ch[begin] = ch[end]; ch[end] = tmp; begin++; end--; } return String.copyValueOf(ch);//将ch转化为字符串 } public static void leftRoadString(String str, int n) { if (str == null || n < 0 || n > str.length()) { return; } //abcdef int left = 0; int leftend = n - 1; int right = n; int rightend = str.length() - 1; str = reverse(str, left, leftend);//bacdef str = reverse(str, right, rightend);//bafedc str = reverse(str, left, rightend);//cdefab System.out.println(str); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入字符串:"); String str = scanner.nextLine(); System.out.print("请输入旋转的位置:"); int n = scanner.nextInt(); leftRoadString(str, n); } }"Here is " ===> " is Here"
import java.util.Scanner; public class Demo6 { public static void reverse(char[] ch, int begin, int end) { char tmp; while (begin < end) { tmp = ch[begin]; ch[begin] = ch[end]; ch[end] = tmp; begin++; end--; } } //here is tulun public static String reverseSentence(String str) { if (str == null) { return null; } char[] ch = str.toCharArray(); reverse(ch, 0, ch.length - 1); int i = 0;//单词的开始 int j = 0;//单词的结束 while (i < str.length()) { if (ch[i] == ' ') { i++; j++; } else if (j == ch.length || ch[j] == ' ') { reverse(ch, i, --j); i = ++j; } else { j++; } } return String.copyValueOf(ch); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入字符串:"); String str = scanner.nextLine(); System.out.println(reverseSentence(str)); } }