首页 技术 正文
技术 2022年11月14日
0 收藏 753 点赞 5,064 浏览 1941 个字

1.String

String类很常用,很重要。

String不像int或float, 它是参考类型。final类型, 不能被继承,String is a Reference Type,Defined in java.lang package

常用方法:
length()
String greeting = “Hello”;
int n = greeting.length();//is 5
charAt(n)(取某个位置字符)
char first = greeting.charAt(0);
char last = greeting.charAt(4);
substring()(取子字符串)
String s = greeting.substring(0,3);//from 0 inclusive to 3 exclusive
Concatenation(链接)
String a = greeting + “ world!”+ 2009;
Equality(don’t use ==)(测试是否相等)
String s = “Hello”; s.equals(greeting);
“Hello”.equalsIgnoreCase(“hello”);(忽略大小写的测试相等)

例子:
public class Test {
    public static void main(String args[]) {
        String letters = “abcdefghijklabcdefghijkl”;
/*这里讲讲阅读源代码,control点击进入方法*/
        System.out.println(“‘c’在第” + letters.indexOf(‘c’) + “个”);
/* indexOf(int ch, int fromIndex) Returns the index within this string
of the first occurrence of the specified character, starting the
search at the specified index.*/
        System.out.println(“‘a’在第” + letters.indexOf(‘a’, 1) + “个”);
        System.out.println(“‘$’在第” + letters.indexOf(‘$’) + “个”);
        System.out.println(“def在第” + letters.indexOf(“def”) + “个”);
        System.out.println(“‘c’在第” + letters.lastIndexOf(‘c’) + “个”);
        System.out.println(letters.substring(20));// 从第20个到末尾
/*beginIndex – the beginning index, inclusive(包含). endIndex – the ending
index, exclusive(不包含).*/
        System.out.println(letters.substring(3, 6));
    }

public class Test {
    public static void main(String args[]) {
        String s, s1;
        char charArray[] = new char[8];
        s1 = new String(“Hello World!”);
        // s1 = “Hello World!”;
        // 输出String的长度
        System.out.println(s1.length());
        s1=s1.replace(“World”, “mark-to-win”);
        System.out.println(“s1 is “+s1);
        // 使用charAt()翻转字符串
        s = “”;
        for (int i = s1.length() – 1; i >= 0; i–)
            s = s + s1.charAt(i);
        System.out.println(s);

    }
}

String表示字符串常量:一旦创建后不会再做修改和变动的字符
串。之所以采用这种方法是因为实现固定的,不可变的字符串比实现可变的字符串更简单高效。对于那些想得到改变的字符串的情况,有一个叫做
StringBuffer的String类的友类。它的对象包含了在创建之后可被改变的字符串。String类和StringBuffer类都在
java.lang包中定义。

更多内容请见原文,原文转载自:https://blog.csdn.net/qq_44639795/article/details/101773979

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898