Dane Course - String Correlation Method (Part I)

charAt(int index)

Function: Gets a single character at the specified subscript position
s = "abc";
char c = charAt(2);

toUpperCase()

Capitalize a string and assign it to a variable
s = s.toUpperCase();
print(s);

toLowerCase()

Lowercase the string and assign it to a variable

length()

length

compareTo(String anotherString)

Compare string sizes in character encoding order
The current string is large and returns a positive number
The current string is small and returns a negative number
Same size, return 0
a.compareTo(b)

compareToIgnoreCase(String str)

Ignoring case and comparing size with another string in encoding order

startsWith(String s)

Is s prefixed?
s = "http://www.baidu.com";
boolean b = s.starsWith("http://");

endsWith(String suffix)

Does it end with s suffix?

equals(Object object)

Compare character content

equalsIgnoreCase(String anotherString)

Ignore whether the contents of case comparison strings are the same

indexOf(String s)

Returns the location of the substring found for the first time
s = "abc abc abc";
int index = s.indexOf("bc"); return value 1
int index = s.indexOf("bc, 2);// Look back from position 2, return value 5
int index = s.lastIndexOf("bc"); // Look back and forth, return value 9
No, return - 1

replace(char oldChar,char newChar)

Replace the specified character with a new character
s = "abc abc abc";
String s2 = s.replace("a","-");
System.out.println(s2);//No modification to the original string, return value "-bc-bc-bc"

subString(int a)

Truncate strings from position a to the end
s = "abcdefg";
String s2 = s.subString(3);
System.out.println(s2); //return value "defg";
String s3 = s.subString(3,5); / / left closed and right open, return value "de";

trim()

Remove blank characters at both ends
s = " a bc ";
s = s.trim();
System.out.println(s); //return value "a bc";

getBytes()

Use the platform's default character set to encode strings as byte sequences and store the results in a new byte array

GetBytes (character encoding)

Use the specified character set to encode the string as a byte sequence and store the result in a new byte array

String. valueOf (data)

Converting any data into strings

Chestnut in the above way

String s = " abc abc abc ";
        System.out.println(s);//" abc abc abc "
        System.out.println("2 Location character:"+s.charAt(2));//b
        System.out.println("Capitalize:"+s.toUpperCase());//" ABC ABC ABC "
        System.out.println("Length:"+s.length());//13
        System.out.println("and xyz Compare size:"+s.compareTo(" xyz"));//negative
        System.out.println("Whether or not abc Start:"+s.startsWith(" abc"));//true
        System.out.println("Whether to use bc Ending:"+s.endsWith("bc "));//true
        System.out.println("First bc Starting position:"+s.indexOf("bc"));//2
        System.out.println("From the third location bc: "+s.indexOf("bc",3));//6
        System.out.println("Look backwards and forwards bc: "+s.lastIndexOf("bc"));//10
        System.out.println("Find characters that do not exist:"+s.indexOf("xxxxx"));//-1
        System.out.println("a replace with-: "+s.replace("a", "-"));//" -bc -bc -bc"
        System.out.println("Interception 6 to the end:"+s.substring(6));//"bc abc"
        System.out.println("Intercept[6,10): "+s.substring(6,10));//"bc a"
        System.out.println("Remove the blank characters at both ends:"+s.trim());//"abc abc abc"

Output result

abc abc abc
2 position characters: b
Capitalization: ABC ABC ABC
Length: 13
Compare size with xyz: -23
Does it start with abc:
Does it end with bc:
The starting position of the first bc: 2
Look for bc:6 from the third location
Look for bc:10 from back to front
Find characters that do not exist: -1
Replace a with -:-bc-bc-bc
Interception 6 to end: bc abc
Interception [6,10):bc a
Remove the blank characters at both ends: abc abc abc

Comprehensive exercises

xml

<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="extract email Name part"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Judging whether palindrome or not"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Flip string"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</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 = (EditText)findViewById(R.id.editText);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);
        button3 = (Button)findViewById(R.id.button3);
        textView = (TextView) findViewById(R.id.textView);
    }

    private void initListeners() {
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                f1();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                f2();
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                f3();
            }
        });
    }

    private void f1() {
        String email = editText.getText().toString();
        int index = email.indexOf("@");
        if(index == -1){
            textView.setText("Incorrect format");
            return;
        }
        String name = email.substring(0,index);
        textView.setText(name);
    }
    private void f2() {
        String s = editText.getText().toString();
        /*
        * abcdefedcba
        * i         j
        *  i       j
        * Determine whether two lower variables are equal
        * If equal comparison continues, it is not necessary to know that I < j; I and J coincide.
        * */
        for(int i=0,j=s.length()-1;i<j;i++,j--){
            if(s.charAt(i)!=s.charAt(j)){
                textView.setText("Not palindrome.");
                return;
            }
            textView.setText("It's palindrome.");
        }
    }
    private void f3() {
        String s = editText.getText().toString();
        String temp = "";
        /*
        * abc->cba
        *   i
        *  i
         *i
        * Use subscript i to retrieve characters and place them in empty strings
        * */
        for(int i=s.length()-1;i>=0;i--){
            temp += s.charAt(i);
        }
        textView.setText(temp);
    }

}

Keywords: Android encoding xml Java

Added by dominicd on Fri, 24 May 2019 22:54:12 +0300