首页 Java indexof StringTokenizer

Java indexof StringTokenizer

举报
开通vip

Java indexof StringTokenizerJava indexof StringTokenizer import java.text.*; import java.util.*; public class DateFormatTest { //显示日期 public void showDate(Date date, int datestyle, Locale locale) { DateFormat currentDateFormat = DateFormat.getDateInstance(datestyle,locale); //创建...

Java indexof StringTokenizer
Java indexof StringTokenizer import java.text.*; import java.util.*; public class DateFormatTest { //显示日期 public void showDate(Date date, int datestyle, Locale locale) { DateFormat currentDateFormat = DateFormat.getDateInstance(datestyle,locale); //创建DateFormat对象currentDateFormat,用来分析传入参数locale的日期格式 String dateString = currentDateFormat.format(date); System.out.println("The date is: " + dateString); } //显示时间 public void showTime(Date date, int timestyle, Locale locale) { DateFormat currentTimeFormat = DateFormat.getTimeInstance(timestyle,locale); String timeString = currentTimeFormat.format(date); System.out.println("The Time is: " + timeString); } public static void main(String[] args) { int styles[] = {DateFormat.DEFAULT, DateFormat.FULL, DateFormat.LONG, DateFormat.SHORT}; //获得当前日期和时间 Date date = new Date(); DateFormatTest test = new DateFormatTest(); //打印中国日期和时间 System.out.println("中国:"); test.showDate(date, styles[0], Locale.CHINA); test.showTime(date, styles[0],Locale.CHINA); //打印美国日期和时间 System.out.println("美国:"); test.showDate(date, styles[0], Locale.US); test.showTime(date, styles[0],Locale.US); } } // public class Hello1 { public static void main(String[] args) { String str = "Hello"; //声明并初始化字符串对象str char ch[] = {‘a’,’b’,’c’,’d’,’e’,’f’,’g’}; //声明并初始化字符数组ch boolean b = true; //声明并初始化布尔型变量b int a = 10000; //声明并初始化整型变量a float f = 24.24f; //声明并初始化单精度变量f double d = 33.3333; //声明并初始化双精度变量d StringBuffer buffer = new StringBuffer(); //声明并初始化可变字符串对象buffer buffer.insert(0,d); //使用StringBuffer类中insert()方法,向buffer第0个位置插入各种类型值 buffer.insert(0," "); buffer.insert(0,f); buffer.insert(0," "); buffer.insert(0,a); buffer.insert(0," "); buffer.insert(0,b); buffer.insert(0," "); buffer.insert(0,ch); buffer.insert(0," "); buffer.insert(0,str); System.out.println(buffer.toString()+"\n"); buffer.deleteCharAt(6); //使用StringBuffer类中deleteCharAt()方法,删除buffer中第6个位置的字符 System.out.println(buffer.toString()+"\n"); buffer.delete(5,12); //使用StringBuffer类中delete()方法,删除buffer中第5个位置至第11位置之间的字符 System.out.println(buffer.toString()+"\n"); } } // import java.text.*; import java.util.Date; import java.util.Locale; public class Hello1 { //显示格式化货币 public void showMoney(double money, Locale locale) { NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale); String str = numberFormat.format(money); System.out.println("The Money is: " + str); } //显示格式化数字 public void showNumber(double num, Locale locale) { NumberFormat numberFormat = NumberFormat.getIntegerInstance(locale); numberFormat.setMaximumFractionDigits(2); String str = numberFormat.format(num); System.out.println("The Number is: " + str); } public static void main(String[] args) { Hello1 numberFormatTest = new Hello1(); double money = 3000.00; double number = 1000.0/3.0; //打印中国格式的货币和数字 System.out.println("中国:"); numberFormatTest.showMoney(money,Locale.CHINA); numberFormatTest.showNumber(number,Locale.CHINA); //打印美国格式的货币和数字 System.out.println("美国:"); numberFormatTest.showMoney(money,Locale.US); numberFormatTest.showNumber(number,Locale.US); } } ///////// public class Hello1 { StringBuffer strBuf1 = new StringBuffer("Hello world!"); //声明、创建并初始化StringBuffer类的对象strBuf1 StringBuffer strBuf2; //声明StringBuffer类的对象strBuf2 StringBuffer strBuf3 = new StringBuffer(10); //声明并创建StringBuffer类的对象strBuf3,并设定其长度为10 public Hello1() { strBuf2 = new StringBuffer("This is Java code."); //创建并初始化StringBuffer类的对象strBuf2 strBuf3 = new StringBuffer("Hello"); //创建并初始化StringBuffer类的对象strBuf3 String output = "strBuf1:"+strBuf1.toString()+"\nlength="+ strBuf1.length()+"\ncapacity="+strBuf1.capacity(); //使用 StringBuffer类的方法toString(),将StringBuffer类对象转化为String型字符串 使用 //使用StringBuffer类的方法length(),来获得该可变字符串的长度 // StringBuffer类的方法capacity(),来获得该可变字符串的的最大存储容量 System.out.println(output); strBuf1.setLength(30); //使用StringBuffer类的方法setLength(),来设置可变字符串的长度 System.out.println("After add strBuf1’s length,"); System.out.println("strBuf1’s capacity is:"+strBuf1.length()); strBuf1.ensureCapacity(66); //使用StringBuffer类的方法ensureCapacity(),来设置可变字符串的最大存储容 量 System.out.println("Set strBuf1’s capacity,"); System.out.println("Now strBuf1’s capacity is:"+strBuf1.capacity()); System.out.println(); System.out.println("strBuf2:"+strBuf2.toString()); System.out.println("Char at 0 in strBuf2 is:"+strBuf2.charAt(0)); System.out.println("Char at 9 in strBuf2 is:"+strBuf2.charAt(9)); char ch[] = new char[strBuf2.length()]; strBuf2.getChars(8,12,ch,0); //使用StringBuffer类的方法getChars(),来获取strBuf2中第8~12位的字符 System.out.println("The char from 8 to 12 is:"); for(int i=0;i<4;i++) { System.out.print("\""+ch[i]+"\","); } System.out.println("\n"); System.out.println("strBuf3:"+strBuf3.toString()); System.out.println("After append string to strBuf3,"); strBuf3.append(" world.StringBufferDemo!"); //使用StringBuffer类的方法append(),在strBuf3末尾插入字符串 System.out.println("New strBuf3:\n"+strBuf3.toString()); System.out.println("After set the 5th char,"); strBuf3.setCharAt(11,’!’); //使用StringBuffer类的方法setCharAt(),来更改strBuf3中第11位的字符 System.out.println("the new strBuf3:\n"+strBuf3.toString()); } public static void main(String[] args) { Hello1 stringBufferDemo = new Hello1(); } } // //package test; public class Hello1 { String str1 = "Hello world!This is Java code."; String str2; String str3 = new String("This is Java code."); public Hello1() { str2 = new String("This is Java code."); System.out.println("str1 is: "+str1); System.out.println("str2 is: "+str2); System.out.println("str3 is: "+str3+"\n"); System.out.println("Each item of str1 is:"); for(int i=0;i<str1.length();i++) { //使用charAt()方法得到String字符串中指定位置的字符,并利用循环分别 输出 char c = str1.charAt(i); System.out.print(c); } System.out.println(); byte b[] = str1.getBytes(); //使用String类的getBytes()方法,得到str1中所有字符对应的Unicode编码, 并存入byte型数组b中 System.out.println("Change each item of str1 to unicode is:"); for(int i=0;i<b.length;i++) { //利用循环分别输出str1中,所有元素对应的字符的Unicode编码 System.out.print(b[i]+","); } System.out.println(); System.out.println("The result of comparing str2 and str3 is:"); System.out.println(str2.equals(str3)); System.out.println("\nReplace all charcters \"a\" to \"e\" in str2"); System.out.println("The result of str2.replace() is: "+str2.replace(‘a’,’e’)); System.out.println("str2 is: "+str2+"\n"); System.out.println("The result of whether str1 is beginning with \"Hello\":"); System.out.println(str1.startsWith("Hello")); } public static void main(String[] args) { Hello1 stringDemo = new Hello1(); } } /// public class Hello1 { public static void main(String[] args) { String str1 = "abc"; String str2 = "aab"; String str3 = "abd"; String str4 = "abc"; String str5 = "ABC"; String str6 = "abcdefgabcde"; //以上完成字符串的声明及初始化 int i = str1.compareTo(str2); int j = str1.compareTo(str3); int k = str1.compareTo(str4); //以上调用String的compareTo()方法来比较字符串 System.out.println("str1 is:"+str1); System.out.println("str2 is:"+str2); System.out.println("str3 is:"+str3); System.out.println("str4 is:"+str4); System.out.println("str5 is:"+str5); System.out.println("str6 is:"+str6); System.out.print("The result of str1 compareTo str2 is:"); System.out.println(i); System.out.print("The result of str1 compareTo str3 is:"); System.out.println(j); System.out.print("The result of str1 compareTo str4 is:"); System.out.println(k); System.out.print("The result of str1 equals str5 is:"); System.out.println(str1.equals(str5)); //调用String的equals()方法来比较字符串 System.out.print("The result of str1 equalsIgnoreCase str5 is:"); System.out.println(str1.equalsIgnoreCase(str5)); //调用String的equalsIgnoreCase()方法来比较字符串 int m = str6.indexOf((int)’d’); //调用String的indexOf()方法,返回字符’d’第一次出现的位置 System.out.println("The char \"d\" first appear position is :"+m); int n = str6.indexOf((int)’d’,4); //调用String的indexOf()方法,返回字符’d’从第四位后,首次出现的位置 System.out.println("After 4th position The char \"d\" appear position is :"+n); } } //////////// public class Hello1 { public static void main(String[] args) { String str1 = "Java code.we will program Java code."; String searchStr = "Java code"; //以上声明创建了字符串对象 int i = str1.indexOf(searchStr); //在字符串str1中,查找并返回字符串对象searchStr的位置 int j = str1.indexOf(searchStr,str1.indexOf("we")); //在字符串str1中,从字符串"we"开始,查找并返回字符串对象 searchStr的位置 int m = str1.lastIndexOf(searchStr); //在字符串str1中,查找并返回字符串对象searchStr最后一次出现的位置 System.out.println("str1 is:"+str1); System.out.println("searchStr is:"+searchStr); System.out.println("i = str1.indexOf(searchStr):"+i); System.out.println("j = str1.indexOf(searchStr,str1.indexOf(\"we\")):"+j); System.out.println("m = str1.lastIndexOf(searchStr):"+m); } } ////////// import java.util.*; public class Hello1 { public static void main(String[] args) { String str1 = "Hello world!This is Java code,stringTokenizer Demo."; //声明并初始化字符串str1 String str2 = "How to use StringTokenizer?StringTokenizer?"; //声 明并初始化字符串str2 StringTokenizer strT1 = new StringTokenizer(str1," ,.!"); //创建StringTokenizer类的对象strT1,并构造字符串str1的分析器 //以空格 符、","、"."及"!"作为定界符 StringTokenizer strT2 = new StringTokenizer(str2," ?"); //创建StringTokenizer类的对象strT2,并构造字符串str2的分析器 //以空格 符及"?"作为定界符 int num1 = strT1.countTokens(); //获取字符串str1中语言符号的个数 int num2 = strT2.countTokens(); //获取字符串str2中语言符号的个数 System.out.println("str1 has "+num1+" words.They are:"); while(strT1.hasMoreTokens()) { //利用循环来获取字符串str1中下一个语言符号,并输出 String str = strT1.nextToken(); System.out.print("\""+str+"\" "); } System.out.println("\nstr2 has "+num2+" words.They are:"); while(strT2.hasMoreTokens()) { //利用循环来获取字符串str2中下一个语言符号,并输出 String str = strT2.nextToken(); System.out.print("\""+str+"\" "); } } } ///////////// import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Hello1 extends JFrame implements ActionListener { private JTextField nameTextField; private JPasswordField passwordTextField; private JPasswordField confirmPswTextField; JButton okButton; JButton exitButton; public Hello1() //构造方法 { super("Validate"); //声明JLabel标签对象 JLabel name = new JLabel("Name"); JLabel password1 = new JLabel("Password"); JLabel password2 = new JLabel("Comfirm Password"); //创建JButton对象okButton,并指定按钮上字符为"OK" okButton = new JButton("OK"); exitButton = new JButton("EXIT"); //创建JButton对象exitButton,并指定按钮上字符为"OK" okButton.addActionListener(this); //添加okButton事件监听器 exitButton.addActionListener(this); //添加exitButton事件监听器 //以下用来创建窗口中字符串及密码输入栏 nameTextField = new JTextField(15); passwordTextField = new JPasswordField(10); confirmPswTextField = new JPasswordField(10); //创建JPanel对象NamePanel面板,并在其中加入name标签及用户名输入栏 JPanel NamePanel = new JPanel(); NamePanel.add(name); NamePanel.add(nameTextField); //创建JPanel对象passwordPanel1面板,并在其中加入password1标签及密码输入栏 JPanel passwordPanel1 = new JPanel(); passwordPanel1.add(password1); passwordPanel1.add(passwordTextField); //创建JPanel对象passwordPanel1面板,并在其中加入password2标签及密码确认输入栏 JPanel passwordPanel2 = new JPanel(); passwordPanel2.add(password2); passwordPanel2.add(confirmPswTextField); //创建JPanel对象buttonPanel面板,并在该面板中加入2个按钮 JPanel buttonPanel = new JPanel(); buttonPanel.add(okButton); buttonPanel.add(exitButton); //创建并设置窗口布局方式为GridLayout Container container = this.getContentPane(); container.setLayout(new GridLayout(4,1)); //以下四句将组件添加至窗口中 container.add(NamePanel); container.add(passwordPanel1); container.add(passwordPanel2); container.add(buttonPanel); setSize(325,225); //设置窗口大小 setVisible(true); //设置窗口为可见 } public void actionPerformed(ActionEvent e) //按钮事件处理方法 { //处理按键的事件 if(e.getSource() == okButton) //单击okButton时,程序所作的处理 { validateDate(); //调用validateDate()方法 } if(e.getSource() == exitButton) //单击exitButton时,程序所作的处理 { this.setVisible(false); //设置窗口为不可见 System.exit(0); //退出程序 } } private void validateDate() { //检验窗口输入栏中,用户输入的数据是否符合要求 if(nameTextField.getText().equals("")|| passwordTextField.getPassword().equals("")|| confirmPswTextField.getPassword().equals("")) // end of condition JOptionPane.showMessageDialog(this,"Please fill all fields"); //以上 用来判断是否有输入栏为空 else if(!nameTextField.getText().matches("[a-zA-Z][a-zA-Z0-9]*")) JOptionPane.showMessageDialog(this,"Invalid name,the name must begin with letter"); //以上用来判断,用户所输入的用户名是否以字母开头,是否由字母及数字组 成 else if(nameTextField.getText().length()>15) JOptionPane.showMessageDialog(this,"The name’s length limit 15 letters"); //以上用来判断,用户输入的用户名,是否超过限定长度的15个字符 else if(passwordTextField.getPassword().length<4) JOptionPane.showMessageDialog(this,"The password’s length is too short"); //以上用来判断,用户输入的密码长度是否少于4个字符 else if(!validatePassword()) JOptionPane.showMessageDialog(this,"The password must contain letters and numbers"); //调用validatePassword()方法,用来判断用户输入的密码的格式是否符合要求, 必须同时包含字母和数字 else if(!compareTwoPassword()) JOptionPane.showMessageDialog(this,"The password input are not same"); //调用compareTwoPassword()方法,用来判断用户两次输入的密码是否一致 else JOptionPane.showMessageDialog(this,"All Data is OK!"); } private boolean validatePassword() //密码输入校验方法 { boolean b = false; String password = new String(passwordTextField.getPassword()); //将字符数 组passwordTextField.getPassword()的 //将字符数组 passwordTextField.getPassword()的 //将字符数组 confirmPswTextField.getPassword()的 //返回布尔型变量的值 } public static void main(String[] args) { Hello1 validateFrame = new Hello1(); validateFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
本文档为【Java indexof StringTokenizer】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_633423
暂无简介~
格式:doc
大小:52KB
软件:Word
页数:0
分类:互联网
上传时间:2017-10-20
浏览量:12