Java Learning-Classroom summary

Source: Internet
Author: User

First, the string comparison method

1) ' = = ' address value comparison
2) equals () method content Comparison

Two instantiation methods of the String class
1) String str= "Hello";
2) string Str=new string ("Hello");

Three
String s= "AA" + "BB" + "CC";
String s= "AABBCC";//Compiler optimizations

Iv. common methods of the string class

1. Publicstring (char[] value) Description: Converts all character array contents into strings

2. Publicstring (char[] value,int offset,int count) Description: Converts a partial character array to a string, offset represents the starting point, and count indicates the length to be manipulated

3. Publicchar charAt (int index) Description: Gets the character at the specified index position

Classroom Programming:

Package Com.cqvie.ceshi;

public class Ceshi {

public static void Main (string[] args) {
Char[] Ch=new char[] {' A ', ' B ', ' C ', ' d ', ' e ', ' f '}; Defining strings
String Str1=new string (ch);
System.out.println (STR1);
String Str2=new string (ch,2,3);
System.out.println (STR2);
Char Str3=str1.charat (3);
System.out.println (STR3);
}
}

Running Result: abcdef

Cde

D

4. Publicint Length () Description: Get string length

Classroom Programming:

Package Com.cqvie.chuan;

public class Chuan {

public static void Main (string[] args) {
String s= "World"; Defining strings
int Str=s.length (); S.length return value for shaping, saved in Str
System.out.println (str);
}
}

Run Result: 5

5. Publicint indexOf (string str) Description: Finds the position of the specified string from scratch, cannot find return-1

Classroom Programming:

Package Com.cqvie.chuan;

public class Chazhao {

public static void Main (string[] args) {
String str= "The weather is fine today";
int S=str.indexof ("weather");
System.out.println (s);

}
}

Run Result: 2

6. Publicint IndexOf (string str,int fromIndex) Description: The position of the string being searched backwards by the specified position, no return-1 found

Classroom Programming:

Package Com.cqvie.chuan;

public class ChaZhao1 {

public static void Main (string[] args) {
String str= "Yesterday Today Tomorrow"; Defining strings
int S=str.indexof ("Day", 1);
System.out.println (s);
int S1=str.indexof ("Day", s+1);
System.out.println (S1);
}

}

Run Result: 1

3

Classroom programming: Querying the position of the 5th "thing" in a string

Package Com.cqvie.chuan;

public class ChaZhao2 {

public static void Main (string[] args) {
String s= "The affairs of the Family matter everything cares about";
int p=-1;
int n=5;
String key= "thing";
for (int i=0;i<n;i++)
{
P=s.indexof (key, p+1);
}
SYSTEM.OUT.PRINTLN (P);
}
}

Run Result: 8

Java Learning-Classroom summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.