一、length()
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s;
s = sc.nextLine();
System.out.println(s.length());
}
}
二、split()
作用:以某种形式(如空格” “)分割字符串,形成字符串数组,例如字符串s = “Hello String abc”在split()之后,会变成字符串数组str,str[0] =Hello 、str[1] = String、str[2] = abc;
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s = sc.nextLine();
String str[] = s.split(" ");
for(int i = 0 ; i < str.length ; i ++) System.out.printf("%s ",str[i]);
}
}
简化代码:(输入的时候就用上split(“ ”),简化代码)
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s[] = sc.next().split(" ");
for(int i = 0 ; i < s.length ; i ++) System.out.printf("%s ",s[i]);
}
}
三、indexOf( )、lastIndexOf( )
没有出现过的话,这些函数的返回值都是-1
s.indexOf('l')
作用:查找字符l在字符串s中第一次出现的下标
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s = "Hello World";
System.out.println(s.indexOf('l'));
}
}
s.indexOf("llo")
作用:查找子串"llo"的首字母在字符串s中第一次出现的下标
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s = "Hello World";
System.out.println(s.indexOf("llo"));
}
}
3、s.lastIndexOf('l')
作用:查找字符l在字符串s中最后一次出现的下标
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s = "Hello World";
System.out.println(s.lastIndexOf('l'));
}
}
4、s.lastIndexOf("or")
作用:查找字符串"or"在字符串s中最后一次出现的首元素的下标
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s = "Hello Wororld";
System.out.println(s.lastIndexOf("or"));
}
}
四、equals( )
作用:比较两个字符串是否相等
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s1 = "Hello World";
String s2 = "Hello World";
if(s1.equals(s2) == true) System.out.println("相等");
else System.out.println("不相等");
}
}
五、compareTo( )
作用:比较两个字符串大小(字典序比较)
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s1 = "Hello World";
String s2 = "Hello World";
if(s1.compareTo(s2) < 0) System.out.println("s1 < s2");
else if(s1.compareTo(s2) == 0) System.out.println("s1 == s2");
else System.out.println("s1 > s2");
}
}
六、startsWith( )、endsWith( )
1、s1.startsWith(s2)
作用:判断s1是否以前缀s2开头
是返回true,不是返回false
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s1 = "Hello World";
String s2 = "Hel";
if(s1.startsWith(s2) == true) System.out.println("s2是s1的前缀");
else System.out.println("s2不是s1的前缀");
}
}
2、s1.endsWith(s2)
作用:判断s1是否以后缀s2结尾
是返回true,不是返回false
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s1 = "Hello World";
String s2 = "rld";
if(s1.endsWith(s2) == true) System.out.println("s2是s1的后缀");
else System.out.println("s2不是s1的后缀");
}
}
七、toLowerCase()、toUpperCase()
s.toLowerCase()
作用:将字符串s的所有大写字母转换成小写字母,并返回该串
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s = "Hello World";
s = s.toLowerCase();
System.out.println(s);
}
}
2、s.toUpperCase()
作用:将字符串s的所有小写字母转换成大写字母,并返回该串
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s = "Hello World";
s = s.toUpperCase();
System.out.println(s);
}
}
八、replace(char oldChar, char newChar)
作用:替换字符或者字符串
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s1 = "Hello World";
String s2 = "Hello World";
s1 = s1.replace('H','a');
s2 = s2.replace("He","ab");
System.out.println(s1);
System.out.println(s2);
}
}
九、substring(int beginIndex, int endIndex)
作用:返回[beginIndex, endIndex)中的子串(注意细节,右边开区间,注意大一位)
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s = "Hello World";
s = s.substring(0,4);
System.out.println(s);
}
}
十、toCharArray()
作用:将字符串转化成字符数组(注意,字符串转成字符数组char a[]才能使用增强for循环)
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
String s = "Hello World";
char a[] = s.toCharArray();
for(int x:a) System.out.printf("%c ",x);
}
}
十一、StringBuilder、StringBuffer
String虽然可以添加是不能被修改,如果打算修改字符串,可以使用StringBuilder和StringBuffer。
StringBuffer线程安全,速度较慢;StringBuilder线程不安全,速度较快。
1、StringBuilder的append( ):(字符串拼接)----在原串操作,不需要返回字串
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
StringBuilder s = new StringBuilder("Hello");
s.append(" World");
System.out.println(s);
}
}
StringBuilder的setCharAt( ); ----在原串操作,不需要返回字串
setCharAt(i,'a')作用:将字符串中i位置上的字符改成字符'a';
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
StringBuilder s = new StringBuilder("Hello");
for(int i = 0 ; i < s.length() ; i ++) s.setCharAt(i, (char)(s.charAt(i) + 1));
System.out.println(s);
}
}
StringBuilder的reverse()----在原串操作,不需要返回字串
作用:翻转字符串
package 做题;
import java.lang.reflect.Array;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Scanner;
import javax.naming.StringRefAddr;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int N = 10;
StringBuilder s = new StringBuilder("Hello World");
s.reverse();
System.out.println(s);
}
}