作业:Java String类的基本用法

博客内容:介绍String类如何判断是否相等,字符串的子串,如何连接字符串等等,并给出相应的代码;

Java String类的介绍以及常用方法与相应代码

一、简单介绍
String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间。
字符串声明形式为:

String stringName;

字符串创建形式为:

stringName = new String(字符串常量);
stringName = 字符串常量;

虽然两个语句都是返回一个String对象的引用,但是两者的处理方式是不一样的。对于第一种,是将该对象的引用返回给用户。而第二种,是通过String的equels方法查找是对象池中是否存放有该String对象,判断是否有该String对象,然后进行相应的创建工作或将其引用返回给用户同时将该引用添加至strings pool中的工作。

二、常用方法以及相应的代码
1、字符串比较判断
(1)、对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
形式:

public int compareTo(String anotherString)

(2)、与compareTo方法相似,但忽略大小写。
形式:

public int compareToIgnore(String anotherString)

(3)、比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
形式:

 public boolean equals(Object anotherObject)

(4)、与equals方法相似,但忽略大小写。
形式:

public boolean equalsIgnoreCase(String anotherString) 

实例:

package zuoye;

public class Compare {
    public static void main(String[] args) {

        String str1 = new String("abc");//创建字符串
        String str2 = new String("ABC");//创建字符串
        int a = str1.compareTo(str2);//情况(1)a>0
        int b = str1.compareToIgnoreCase(str2);//情况(2)b=0
        boolean c = str1.equals(str2);//情况(3)c=false
        boolean d = str1.equalsIgnoreCase(str2);//情况(4)d=true
        System.out.println(str1.equals(str2));
        System.out.println(str1.equals(str2));
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }
}
/*运行结果
false
false
32
0
false
true
*/

2、提取字符串子串用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:
(1)、该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。
形式:

public String substring(int beginIndex)

(2)、该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。
形式:

public String substring(int beginIndex, int endIndex)

实例:

package zuoye;

public class Compare {
    public static void main(String[] args) {

        String str1 = new String("abcdefg");//创建
        String str2 = str1.substring(2);//情况(1)str2 = "cdefg"
        String str3 = str1.substring(2,5);//情况(2)str3 = "cde"
        System.out.println(str2);
        System.out.println(str3);

    }
}

/*运行结果
cdefg
cde
*/

3、字符串连接
将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。
形式:

public String concat(String str)

实例:

package zuoye;

public class Compare {
    public static void main(String[] args) {

        String str1 = "aa".concat("bb").concat("cc");
//相当于
        String str2 = "aa"+"bb"+"cc";

        System.out.println(str1);
        System.out.println(str2);

    }
}

/*运行结果:
aabbcc
aabbcc
*/

4、求字符串长度
返回该字符串的长度
形式:

public int length()

实例:

package zuoye;

public class Compare {
    public static void main(String[] args) {

        String str = new String("abcdefg");
        int strlength = str.length();//strlength = 7


        System.out.println(str);
        System.out.println(strlength );

    }
}

/*
运行结果:
abcdefg
7
*/

5、求字符串某一位置字符返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个length()-1。序列的第一个 char 值位于索引 0 处,第二个位于索引 1 处,依此类推,这类似于数组索引。
形式:

public char charAt(int index)

实例:

package zuoye;

public class Compare {
    public static void main(String[] args) {

        String str = new String("abcdefg");
        char ch = str.charAt(4);//ch = z


        System.out.println(str);
        System.out.println(ch );

    }
}

/*
运行结果:
abcdefg
e
*/

6、字符串中单个字符查找
(1)、用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
形式:

public int indexOf(int ch/String str)

(2)、改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。
形式:

public int indexOf(int ch/String str, int fromIndex)

(3)、该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
形式:

public int lastIndexOf(int ch/String str)

(4)、该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。
形式:

public int lastIndexOf(int ch/String str, int fromIndex)

实例:

package zuoye;

public class Compare {
    public static void main(String[] args) {

        String str = "I am a good student";
        int a = str.indexOf('a');//a = 2
        int b = str.indexOf("good");//b = 7
        int c = str.indexOf("w",2);//c = -1
        int d = str.lastIndexOf("a");//d = 5
        int e = str.lastIndexOf("a",3);//e = 2
        
        System.out.println(str);
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);

    }
}

/*运行结果:
I am a good student
2
7
-1
5
2

*/

7、字符串中字符的大小写转换
(1)、返回将当前字符串中所有字符转换成小写后的新字符串
形式:

public String toLowerCase()

(2)、返回将当前字符串中所有字符转换成大写后的新字符串
形式:

public String toUpperCase()

实例:

package zuoye;

public class Compare {
    public static void main(String[] args) {

        String str = new String("abCD");
        String str1 = str.toLowerCase();//str1 = "abcd"
        String str2 = str.toUpperCase();//str2 = "ABCD"
        
        System.out.println(str);
        System.out.println(str1);
        System.out.println(str2);


    }
}


/*运行结果:
abCD
abcd
ABCD
*/

8、字符串中字符的替换
(1)、用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。
形式:

public String replace(char oldChar, char newChar)

(2)、该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。
形式:

public String replaceFirst(String regex, String replacement)

(3)、该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。
形式:

public String replaceAll(String regex, String replacement)

实例:

package zuoye;

public class Compare {
    public static void main(String[] args) {

        String str = "asdzxcasd";
        String str1 = str.replace('a','g');//str1 = "gsdzxcgsd"
        String str2 = str.replace("asd","fgh");//str2 = "fghzxcfgh"
        String str3 = str.replaceFirst("asd","fgh");//str3 = "fghzxcasd"
        String str4 = str.replaceAll("asd","fgh");//str4 = "fghzxcfgh"


        System.out.println(str);
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println(str4);


    }
}

/*运行结果:
asdzxcasd
gsdzxcgsd
fghzxcfgh
fghzxcasd
fghzxcfgh
*/

9、截去字符串两端的空格,但对于中间的空格不处理。返回字符串的副本,忽略前导空白和尾部空白
形式:

public String trim()

实例:

package zuoye;

public class Compare {
    public static void main(String[] args) {

        String str = " a sd ";
        String str1 = str.trim();
        int a = str.length();//a = 6
        int b = str1.length();//b = 4

        System.out.println(str);
        System.out.println(str1);
        System.out.println(a);
        System.out.println(b);


    }
}

/*
运行结果:
a sd 
a sd
6
4

*/

10、用来比较当前字符串的起始字符或子字符串prefix和终止字符或子字符串suffix是否和当前字符串相同,重载方法中同时还可以指定比较的开始位置offset。
形式:

public boolean statWith(String prefix)public boolean endWith(String suffix)

实例:

package zuoye;

public class Compare {
    public static void main(String[] args) {

        String str = "asdfgh";
        boolean a = str.startsWith("as");//a = true
        boolean b = str.endsWith("gh");//b = true


        System.out.println(str);
        System.out.println(a);
        System.out.println(b);


    }
}
/*运行结果:
asdfgh
true
true

*/

11、从当前字符串的firstStart位置开始比较,取长度为length的一个子字符串,other字符串从otherStart位置开始,指定另外一个长度为length的字符串,两字符串比较,当b为true时字符串不区分大小写。
形式:

public regionMatches(boolean b, int firstStart, String other, int otherStart, int length)

12、判断参数s是否被包含在字符串中,并返回一个布尔类型的值。
形式:

public contains(String str)

实例:

package zuoye;

public class Compare {
    public static void main(String[] args) {

        String str = "student";
        str.contains("stu");//true
        str.contains("ok");//false
        
        System.out.println(str.contains("stu"));
        System.out.println(str.contains("ok"));


    }
}
/*运行结果:
true
false

*/

13、将str作为分隔符进行字符串分解,分解后的字字符串在字符串数组中返回。
形式:

public String[] split(String str)

举例:

package zuoye;

public class Compare {
    public static void main(String[] args) {

        String str = "asd!qwe|zxc#";
        String[] str1 = str.split("!|#");//str1[0] = "asd";str1[1] = "qwe";str1[2] = "zxc";


        System.out.println(str);
        System.out.println(str1[0]);
        System.out.println(str1[1]);
        System.out.println(str1[2]);


    }
}
/*运行结果:
asd!qwe|zxc#
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
asd
 at zuoye.Compare.main(Compare.java:13)
qwe|zxc

*/

14、测试字符串是否以str结尾
形式:

public boolean endsWith(String str)

15、将字符串转换成字符数组返回
形式:

public char[] getBytes

16、将指定的字符串转成制服数组返回
形式:

public char[] getBytes(String str)

17、测试字符串是否以str开始
形式:

 public boolean startsWith(String str)

18、将字符串转换成字符数组
形式:

public char[] toCharArray

19、将指定内容用字符串表示
(1)、将布尔方法b的内容用字符串表示
形式:

public String valueOf(Boolean b) 

(2)、将字符ch的内容用字符串表示
形式:

public String valueOf(char ch)  

(3)、将数字index的内容用字符串表示
形式:

public String valueOf(int index)   

(4)、将长整数字l的内容用字符串表示
形式:

public String valueOf(long l) 

20、根据给定正则表达式的匹配拆分此字符串。

形式:

public String[] split(String regex)

21、字符串转换为基本类型
java.lang包中有Byte、Short、Integer、Float、Double类的调用方法:
形式:

public static byte parseByte(String s)//情况1
public static short parseShort(String s)//情况2
public static short parseInt(String s)//情况3
public static long parseLong(String s)//情况4
public static float parseFloat(String s)//情况5
public static double parseDouble(String s)//情况6 

代码说明:

 int n = Integer.parseInt("12");
 float f = Float.parseFloat("12.34"); 
 double d = Double.parseDouble("1.124"); 

22、基本类型转换为字符串类型

形式:

public static String valueOf(char data[])//情况1
public static String valueOf(char data[], int offset, int count)//情况2
public static String valueOf(boolean b)//情况3
public static String valueOf(char c)//情况4
public static String valueOf(int i)//情况5
public static String valueOf(long l)//情况6
public static String valueOf(float f)//情况7
public static String valueOf(double d)//情况8 

代码说明:

String s1 = String.valueOf(12);
String s1 = String.valueOf(12.34);

23、进制转换

举例:

public Long.toBinaryString(long l)
public Long.toOctalString(long l)
public Long.toHexString(long l)
public Long.toString(long l, int p)//p作为任意进制
  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
引用:Educoder头歌实践平台 中 Java 数据结构之图 共4关 金币共2300 的那个实践项目答案由于图论问题渗透整个计算机科学,图算法对于计算机学科至关重要。成百上千的计算问题最后都可以归约为图论问题。引用:第一关public class HelloWorld{ /********* Begin *********/ public static void main(String[] args) { System.out.println("姓名:张三"); System.out.println("年龄:25"); System.out.println("职业:JAVA高级工程师"); System.out.println("薪资:15K"); } /********* End *********/ } 引用:第五关package chapter2; /********* Begin *********/ public class HelloWorld { String userName = "张三"; public static void main(String[] args){ System.out.println("hello eduCoder"); } } /********* End *********/。 根据提供的引用内容,我无法直接提供你所需的头歌实训作业答案。但是,我可以给你一些关于Java的一般知识,以帮助你完成头歌实训作业。 在Java中,你可以使用`public static void main(String[] args)`方法作为程序的起点。在这个方法中,你可以编写你想要执行的代码。例如,你可以使用`System.out.println`来输出一些文本。 如果你想定义一个变量,你可以使用`String`关键字,并指定变量的名称和初始值。例如,`String userName = "张三";`表示定义了一个名为`userName`的字符串变量,并将其初始化为"张三"。 希望这些信息对你有帮助。如果你有其他关于Java的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值