In-depth analysis of some common methods of String in java
This article mainly introduces "in-depth analysis of some common methods of String in java". In daily operation, I believe that many people have doubts about some common methods of String in java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "some common methods of String in java". Next, please follow the editor to study!
Methods in package countio;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;public class MyUtil {/ * * utility classes are accessed statically, so private constructors do not allow creation of objects (absolutely good habit) * / private MyUtil () {throw new AssertionError () } / * * @ param filename filename * @ param word string * @ number of times the return string appears in the file * / public static int countWorInFile (String filename,String word) {int counter=0; FileReader fr = null; BufferedReader br = null; try {fr = new FileReader (filename); br = new BufferedReader (fr) / / character cache input stream (from file-- input-- > program) String line = null; while ((line = br.readLine ())! = null) {int index =-1 / / the string read each time must be greater than or equal to the string hello indexOf you are looking for. Return the position where a specified string value first appears in the string while ((line.length () > = word.length () & & (index = line.indexOf (word)) > = 0)) {counter++; System.out.println (index) / / output 4 4 line = line.substring (index+word.length ()); / / start at 0} catch (Exception e) {e.printStackTrace ();} finally {if (br! = null) {try {br.close () } catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} / / fr.close (); same as above to determine and then close to prevent the resource from being closed before it is established} return counter } public static void main (String [] args) {/ * * File.separator indicates the separator related to the system. Under Linux, it is:\\ * heiwhellodohehellod (there are two hello) * / String filename = "\\ d:\\ hello.txt\"; String filename = File.separator+ "d:" + File.separator+ "hello.txt" + File.separator Int counter = countWorInFile (filename, "hello"); System.out.println ("the number of occurrence of string hello is" + counter);}} some methods of String in java are parsed in depth.
1. Public String (char [] c.
Converts a character array of length length to a string, starting at the subscript begin of the character array c.
Begin and length can be omitted to convert the character array c to a string. Also: character array can be changed to byte array byte [] b.
Char [] c=new char [] {'jungle pencils 6' pas 6 'pas 4' pas pompies 9'}
String s1=new String (c)
String s=new String (cpd2, p3)
System.out.println (S1)
System.out.println (s)
Public char [] toCharArray ().
The string is replaced with a character array.
3. Public char charAt (int subscript).
Returns the character at the specified position in the string.
String s = "jkdfsdf"
Char t=s.charAt (3)
4. Public byte [] getBytes ()
Converts a string to an array of bytes, whose default output is an ASCII value, which can be typed to output bytes by char. String s = "sjdfsdf"
Byte [] b=s.getBytes ()
5. Public String trim ()
Clear the spaces on the left and right ends of the string.
String s = "skkgnsdfsd"
System.out.println (s.trim ())
6. Public int indexOf (String spenint index).
Finds the position of the specified character from the string after the specified position. If you do not specify a location, start from scratch.
String s = "dgdgdg"
Int n=s.indexOf ("t"); / / search from scratch
Int n1=s.indexOf ("d", 3); / / start at location 3
7. Public String substring (int beginindex,int endindex)
Intercepts the specified string from the start position to the end position, without the end character. The end position can be omitted.
String s = "sdgsgghd"
String s1=s.substring (2pr 4)
String s2=s.substring (2)
Public String [] split (String s).
Splits the string by the specified character.
String s = "dfgdhdfgdrhrhgdt"
String ss [] = s.split ("d")
For (int iTuno Bandi)