《 java 程序设计之网络编程 》

Click here to load reader

Upload: ringo

Post on 24-Jan-2016

145 views

Category:

Documents


0 download

DESCRIPTION

《 Java 程序设计之网络编程 》. 教学课件. 重庆大学计算机学院 电子邮件: [email protected]. 第 6 章 字符串处理. 主要内容. 本章将介绍 Java 语言中字符串的处理技术。主要涉及在程序运行初始化后不能改变的字符串类 String 和字符串内容可以动态改变的类 StringBuffer ,以及用于进行字符串词法分析类 StringTokenizer 。同时还将介绍字符串和其它数据类型间的转换。 - PowerPoint PPT Presentation

TRANSCRIPT

  • Java

    [email protected]

  • 6

  • JavaStringStringBufferStringTokenizer C/C++\0JavaJava

  • 6.1 String

    6.2 StringBuffer

    6.3 StringTokenizer

    6.4

  • 6.1 String6.1.1

    6.1.2 String

  • 6.1.1

  • 6.1.1

    String sStrings=new String(We are students)s= We are students

    String s=new String("We are students");String s="We are students";

  • 6.1.1

    1String char cDem0l[]={'2','3','4','5'}; char cDem02[]={'1','2','3','4','5'}; String strDem01=new String(cDem0l); String strDem02=new String(cDem02,1,4); System.out.println(strDem01+ strDem01 ); 23452 byte cDem0l[]={66,67,68}; byte cDem02[]={65,66,67,68}; String strDem01=new String(cDem0l); String strDem02=new String(cDem02,1,3); "BCD"

  • 6.1.2 String

    Stringlength( )charAt( )indexOf( )lastIndexOf( )getChars( )getBytes( )toCharArray( )

  • 6.1.2 String

    1String length()length()public int length() 16-bit Unicode :String s= "we are students",tom= "";int n1,n2,n3;n1=s.length(); // n1 15n2=tom.length(); // n25n3=.length(); //n34

  • 6.1.2 String

    2equals()equalsIgnoreCase()startsWith(),endsWith()regionMatches()compareTo(),compareToIgnoreCase()(1)equalsequalsIgnoreCase Stringequals() public boolean equals(String s) s String tom=new String( "we are students"); String boy=new String( "We are students"); String jerry= new String("we are students"); tom.equals(boy)false,tom.equals(jerry)true.

  • 6.1.2 String

    StringequalsIgnoreCase () public boolean equalsIgnoreCase(String s)s , String tom =new String(ABC), Jerry=new String(abc); tom.equalsIgnoreCase(Jerry)true(2) startsWithendsWith public boolean srartsWith(String s),s,String tom= "220302620629021",jerry= "21079670924022";tom.startsWith("220")true jerry.startsWith("220")false

  • 6.1.2 String

    public boolean endsWith(String s) ,s, String tom= "220302620629021", jerry= "21079670924022"; tom.endsWith("021")true jerry.endsWith("021")false.

  • 6.1.2 String

    6-1 20042004005814101public class StringStart{ public static void main(String args[]){ String john="200400581",start="2004"; if((john.startsWith(start)) && (tom.endsWith("1"))) System.out.println("2004"); else System.out.println("2004"); }}2004

  • 6.1.2 String

    (3) regionMatchespublic boolean regionMatches(int firstStart,String other,int ortherStart,int length)public boolean regionMatches(boolean b,int firstStart,String other,int ortherStart,int length)firstStart,length,other other othertStart ,otherlengthtrue,false0

  • 6.1.2 String

    (4) compareTo,compareToIgnoreCase StringcompareTocompareToIgnoreCase public int compareTo(String s) public int compareToIgnoreCase(String s)compareTo,s s ,0 s,s, String str= "abcde" str.compareTo("boy") //0 str.compareTo(aba) //0 str.compareTo(abcde) //0 compareToIgnoreCase(String s),

  • 6.1.2 String

    6-2 public class SortStrs{ public static void main(String args[]){ String a[]={"Java","Basic","C++","Fortran","SmallTalk"}; for(int i=0;i

  • 6.1.2 String

    3 public int indexOf(int ch) public int indexOf(int ch,int fromIndex) public int indexOf(String str) public int indexOf(String str,int fromIndex) fromIndex,-1 String strSource="I love Java" int nPosition nPosition=strSource.indexOf(v) // nPosition4 nPosition=strSource.indexOf(a,9) // nPosition11 nPosition=strSource.indexOf("love") // nPosition2 nPosition=strSource.indexOf("love",0) // nPosition2

  • 6.1.2 String

    String public int lastIndexOf(int ch) public int lastIndexOf(int ch,int fromlndex) public int lastIndexOf(String str) public int lastIndexOf(String str,int fromIndex)fromIndex,-1

  • 6.1.2 String

    5 String public String replace(char oldChar,char newChar)s,newChar s oldChar public String replaceAll(String old ,String new)s,news old public String trim()s trim(),s String s= "I mist theep " Strong temp=s.replace( t , s ) //I miss sheep" String s=" I am a student " String temp=s.trim(); //"I am a student"

  • 6.1.2 String

    4 public String substring(int beginIndex),beginIndex public String substring(int beginIndex,int endIndex),beginIndexendIndex-1String strSource=new String("Java is interesting");String strNewl=strSource.substring(5); // strNew1=is interestingString strNew2 =strSource.substring(5,6); // strNew2=i

  • 6.1.2 String

    5Stringpublic String replace(char oldChar,char newChar)s,newChar s oldChar public String replaceAll(String old ,String new)s,news old public String trim()s trim(),sString s= "I mist theep "Strong temp=s.replace( t , s )//I miss sheep"String s=" I am a student "String temp=s.trim(); //"I am a student"

  • 6.1.2 String

    6.(1) public String toUpperCase(Locale locale) //public String toUpperCase()public String toLowerCase(Locale locale) //public String toLowerCase()(2) public char[ ] toCharArray( )(3) getChars(int srcBegin,int srcEnd,char[ ] dst,int dstBegin)(4) public String concat(String str)+

  • 6.1.2 String

    6-3 Stringpublic class AccesString{ public static void main(String args[]) { int n1,n2,n3; String ko="Visual Baisc",La="java",s1,s2,s3,s4="C++"; s1=ko.concat(La); s2 = s1.substring (7, 16); s3=ko.replace('s','x'); n1=s1.length(); n2=s1.indexOf(La); n3=s1.lastIndexOf("Visual"); System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(n1); System.out.println(n2); System.out.println(n3); } }Visual BaiscjavaBaiscjavaVixual Baixc1612

  • 6.1 String

    6.2 StringBuffer

    6.3 StringTokenizer

    6.4

  • 6.2 StringBufferStringStringStringJavaString s=new String("Hello")s=Hello World //s

    StringJavaStringBufferStringBufferStringBuffer

  • 6.1 String6.2.1 StringBuffer

    6.2.2 StringBuffer

  • 6.2.1 StringBuffer StringBufferString StringBuffer s // sStringBuffer s =new StringBuffer("Hello"); // s.setCharAt(1,'o') //HelloHollo

    StringBuffer,public StringBuffer() 16 public StringBuffer(int) public StringBuffer(String) 16

  • 6.2.2 StringBuffer StringBuffer

    1append() StringBufferappendappend public StringBuffer append(boolean b) public StringBuffer append(char c) public StringBuffer append(String str) StringBufferStringBuffer sbfSource=new StringBuffer("1+2=)int nThree=3sbfSource.append(nThree)System.out.println(sbfSource.toString( )) 1+2=3

  • 6.2.2 StringBuffer2 insert public StringBuffer insert(intoffsetBoolean b) public synchronized StringBuffer insert(int offsetchar[] str) public synchronized StringBuffer insert(int indexchar[] sb int offset,int len) public synchronized StringBuffer insert(int offset,String str) StringBufferStringBufferStringBuffer StringBuffer sbfSource=new StringBuffer("1+=2") int nOne=1 sbfSource.insert(2nOne) System.out.println(sbfSource.toString())1+1=2

  • 6.2.2 StringBuffer3toString()

    4. (1) charAt(int index)char0 charAt StringBuffer sbfSource=new StringBuffer(10) sbfSource.append("My") char c=sbfSource.charAt(0) // M(2) getChars(int srcBeginint srcEndchar[] dstint dstBegin) dst. getChars StringBuffer sbfSource=new StringBuffer("You are the best!") char[] str sbfSource.getChars(0,2,str,0) // Yo

  • 6.2.2 StringBuffer5. (1)delete(int startint end) startend-1StringBuffer delete StringBuffer sbfSource=new StringBuffer("You are the best")sbfSource.delete(0,3) // are the best!(2)deleteCharAt(int index)StringBuffer

  • 6.2.2 StringBuffer6 public void ensureCapacity(int minimumCapacity) minimumCapacity 2. minimumCapacity public void SetLength(int newLength) newLength newLength

  • 6.2.2 StringBuffer7public StringBuffer replace(int start,int end,String str)startendstrStringBufferreplaceStringBuffer sbfSource=new StringBuffer("You are the best!")Stringstr=new String("I'm") sbfSource.replace(0,7,str) // I'm the best!

  • 6.2.2 StringBuffer8(1) publuc String substring(int start,int end)startendStringsubString StringBuffer sbfSource=new StringBuffer("You are the best!") String str=SbfSource.substring(02) // Yo(2) publuc String substring(int start)startString

    9public StringBuffer reverse(),StringBufferreverseStringBuffer sbfSource=new StringBuffer("You are the best!")String str=SbfSource.reverse() // !tseb eht era uoY

  • 6.2.2 StringBuffer10(1) public int capacity() capacity StringBuffer sbfSource=new StringBuffer(10) SbfSource.append("you") Systemoutprintln("+sbfSource.capacity()) 10(2)public int length( ) length StringBuffer sbfSource=new StringBuffer(10) SbfSource.append("you") Systemoutprintln(:+sbfSource.length( )) 3

  • 6.2.2 StringBuffer6-4//Reverse.javapublic class Reverse { public static void main(String args[]){ String strSource = new String(I love Java); String strDest = reverseIt ( strSource ); System.out.println(strDest); }public static String reverseIt(String source) { int i, len = source.length(); StringBuffer dest = new StringBuffer(len); for (i = (len - 1); i >= 0; i--) dest.append(source.charAt(i)); return dest.toString(); }}avaJ evol I

  • 6.1 String

    6.2 StringBuffer

    6.3 StringTokenizer

    6.4

  • 6.3 StringTokenizer 6.3.1

    6.3.2 StringTokenizer

  • 6.3.1 StringTokenizer StringTokenizer(String str) StringTokenizer(String sbString delim) // delim StringTokenizer(String SbString delimboolean returnTokens)Java('\t')('\n')('\r')delimStringTokenizer returnTokens true, false, StringTokenizer fenxi=new StringTokenizer("we are student");StringTokenizer fenxi=new StringTokenizer("we ,are ; student", ", ; ");

  • 6.3.2 StringTokenizer1public int countTokens()String str=new String("I love Java");StringTokenizer st=new StringTokenizer(str);int nTokens=st.countTokens(); // 32hasMoreElements()nextElement()hasMoreTokens()nextToken()nextToken(String delim)hasMoreTokenshasMoreTokens()nextToken()String

  • 6.3.2 StringTokenizer6-5StringTokenizer//TestToken.javaimport java.util.*;public class TestToken{ public static void main(String args[]) { //StringTokenizerStringTokenizer st = new StringTokenizer("this is a Java programming"); // while(st.hasMoreTokens()) { // System.out.println(st.nextToken()); } }}thisisaJavaProgramming

  • 6.1 String

    6.2 StringBuffer

    6.3 StringTokenizer

    6.4

  • 6.4 6.4.1

    6.4.2

  • 6.4.1 StringvalueOf() public static String valueOf(boolean b) public static String valueOf(char c) public static String valueOf(char[] data) public static String valueOf(char[ ]data,int offset,int count) public static String valueOf(double d) public static String valueOf(float f) public static String valueOf(long l) public static String valueOf(Object obj)

  • 6.4.1 6-7public class CovertString{ public static void main(String args[]) {int nInt = 10;float fFloat = 3.14f;double dDouble = 3.1415926; //Integer obj1 = new Integer(nInt); //Float obj2= new Float(fFloat); //Double obj3 = new Double(dDouble); //toStringString strString1 = obj1.toString();System.out.println(strString1);String strString2 = obj2.toString();System.out.println(strString2);String strString3 = obj3.toString(); System.out.println(strString3); }}

  • 6.4.2 IntegerDoubleFloatLongvalueOf( )public static Double valueOf(String s) throws NumberFormatExceptionpublic static Integer valueOf(String s) throws NumberFormatExceptionpublic static Float valueOf(String s) throws NumberFormatExceptionpublic static Long valueOf(String s) throws NumberFormatExceptionpublic static Double valueOf(String s) throws NumberFormatException String IntegerDoubleLongFloatValueOf

  • 6.4.2 DoubleFloatIntegerLongdoubleValue(), floatValue(), intValue(), longValue() String strPI=3.1415926; Double dpi=Double.valueOf(strPI); double ddPI=dpi.doubleValue( ); float ffPI=dpi.floatValue(); BooleanByteDoubleFloatIntegerLongparseDouble(String) parseFloat(String)parseInt(String) parseLong(String) staticboolean parseBoolean(Strings) staticint parseInt(Strings[, intradix]) staticbyte parseByte(Strings) staticdouble parseDouble(Strings) staticfloatparseFloat(Strings)

  • 6.4.2 6-8 public class CovertSimple{ public static void main(String args[]){ char[] cArray;int nInt;float fFloat;double dDouble;String strString = new String("I love Java"); String strInteger = new String("314"); String strFloat = new String("3.14"); String strDouble = new String("3.1416"); cArray = strString.toCharArray(); System.out.println(cArray); nInt = Integer.parseInt(strInteger); System.out.println(nInt); fFloat = Float.parseFloat(strFloat); System.out.println(fFloat); dDouble = Double.parseDouble(strDouble); System.out.println(dDouble); } } I love Java3143.143.1416