【达内课程】字符串相关方法

charAt(int index)

作用:得到指定下标位置的单个字符

String s = "abc";
char c = s.charAt(1);
打印c输出结果:b

indexOf(String s)

作用:返回第一次找到的子串位置

String s = "abc abc abc";
int index1 = s.indexOf("bc");//返回值1
int index2 = s.indexOf("bc", 2);//从2位置向后找,返回值5
int index3 = s.lastIndexOf("bc");//从后向前找,返回值9
int index4 = s.indexOf("de");//不存在返回-1

toUpperCase()

作用:将字符串大写并赋值给一个变量

String s = "abc";
s = s.toUpperCase();
打印s输出结果:ABC

toLowerCase()

作用:将字符串小写并赋值给一个变量

String s = "ABC";
s = s.toLowerCase();
打印s输出结果:abc

length()

作用:获取字符长度

String s = "ABC";
int l = s.length();
打印l输出结果:3

startsWith(String s)

作用:判读是不是以传入字符s为前缀

String s = "http://www.baidu.com";
boolean b = s.startsWith("http://");
打印b输出结果:true

endsWith(String suffix)

作用:判断是不是以传入字符s为后缀结尾

String s = "http://www.baidu.com";
boolean b = s.endsWith(".cn");
打印b输出结果:false

equals(Object object)

作用:比较字符串中所包含的内容是否相同

String s1 = "abc";
String s2 = "abc";
boolean b = s1.equals(s2);
打印b输出结果:true
String s1 = "abc";
String s2 = "ABC";
boolean b = s1.equals(s2);
打印b输出结果:false

equalsIgnoreCase(String anotherString)

作用:忽略大小写比较字符串内容是否相同

String s1 = "abc";
String s2 = "ABC";
boolean b = s1.equalsIgnoreCase(s2);
打印b输出结果:true

trim()

作用:去除两端空白字符

String s = " abc d ";
s = s.trim();
打印s输出结果:abc d

replace(char oldChar,char newChar)

作用:将指定的字符替换为新字符

String s = "abc abc abc";
String s2 = s.replace("a","-");
打印s输出结果:abc abc abc (说明不对原字符串进行修改)
打印s输出结果:-bc -bc -bc

subString(int a)

作用:从位置a到结尾截取字符串

String s = "abcdefg";
String s2 = s.substring(3);//返回值"defg"
String s3 = s.substring(3,5);//左闭右开,返回值"de";

String.valueOf(数据)

作用:把任何数据转换成字符串

int i = 123;
Log.d("TTT", String.valueOf(i));
Log不能直接输出数字,可用此方法把数字转换成字符串再输出

compareTo(String anotherString)

作用:按字符编码顺序比较字符串大小

返回值是整型,它是先比较对应字符的大小(ASCII码顺序),如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的长度差值,如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符做比较,以此类推,直至比较的字符或被比较的字符有一方结束。

当前字符串大,返回正数
当前字符串小,返回负数
大小相同,返回0

String str1 = "Strings";
String str2 = "Strings";
String str3 = "Strings123";

int result = str1.compareTo( str2 );
Log.d("TTT", ""+result);//输出:0

result = str2.compareTo( str3 );
Log.d("TTT", ""+result);//输出:-3

result = str3.compareTo( str1 );
Log.d("TTT", ""+result);//输出:3

java中的 compareTo 方法,返回参与比较的前后两个字符串的asc码的差值

String a = "a";
String b = "b";
Log.d("TTT", "" + a.compareTo(b));//输出 -1
String a = "a";
String b = "b";
Log.d("TTT", "" + b.compareTo(a));//输出 1
String s1 = "a";
String s2 = "a";
Log.d("TTT", "" + s1.compareTo(s2));//输出 0

两个字符串首字母不同,则该方法返回首字母的asc码的差值

String a = "abc";
String b = "dfg";
 Log.d("TTT", "" + a.compareTo(b));//输出 -3

参与比较的两个字符串如果首字符相同,则比较下一个字符,直到有不同的为止,返回该不同的字符的asc码差值

String a = "abedfg";
String b = "abc";
Log.d("TTT", "" + a.compareTo(b));//输出 2

两个字符串不一样长,可以参与比较的字符又完全一样,则返回两个字符串的长度差值

String a = "abc";
String b = "abcdefg";
Log.d("TTT", "" + a.compareTo(b));//输出 -4
String a = "abc";
String b = "abcdefg";
Log.d("TTT", "" + b.compareTo(a));//输出 4

目前compareTo项目中的用途是比较版本号的高低

String a = "1.0.0";
String b = "1.0.1";
Log.d("TTT", "" + a.compareTo(b));//输出 -1

compareToIgnoreCase(String str)

作用:忽略大小写与另一字符串按编码顺序比较大小

String str1 = "strings";
String str2 = "Strings";

int result = str1.compareTo( str2 );
Log.d("TTT", ""+result);//输出32

int result2 = str1.compareToIgnoreCase( str2 );
Log.d("TTT", ""+result2);//输出0

综合练习:提取邮件姓名、判断回文、翻转字符串

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提取email名字部分" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="判断是否回文" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="翻转字符串" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#222"
        android:gravity="center"/>
</LinearLayout>

java

public class MainActivity extends AppCompatActivity {
    EditText editText;
    Button button1;
    Button button2;
    Button button3;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initListeners();
    }

    private void initViews() {
        editText = findViewById(R.id.editText);
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        textView = findViewById(R.id.textView);
    }

    private void initListeners() {
        button1.setOnClickListener(view -> f1());
        button2.setOnClickListener(view -> f2());
        button3.setOnClickListener(view -> f3());
    }

    private void f1() {
        String email = editText.getText().toString();
        int index = email.indexOf("@");
        if (index == -1) {
            textView.setText("格式不正确");
            return;
        }
        String name = email.substring(0, index);
        textView.setText(name);
    }

    private void f2() {
        String s = editText.getText().toString();
        /*
         * abcdefedcba
         * i         j
         *  i       j
         * 判断两个下标变量是否相等
         * 如果相等继续比较,直到i<j;i和j没有必要重合
         */
        for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                textView.setText("不是回文");
                return;
            }
            textView.setText("是回文");
        }
    }

    private void f3() {
        String s = editText.getText().toString();
        String temp = "";
        /*
         * abc->cba
         *   i
         *  i
         *i
         * 用下标i反向取到字符放入空字符串
         */
        for (int i = s.length() - 1; i >= 0; i--) {
            temp += s.charAt(i);
        }
        textView.setText(temp);
    }
}

提取邮件姓名 运行结果:
在这里插入图片描述
判断回文 运行结果:

“回文串” 是一个正读和反读都一样的字符串,如“level”或者“noon”等就是回文串
在这里插入图片描述
翻转字符串 运行结果:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值