用java从字符串中截取指定子串

2012-11-12

  下面的代码是从最简单的思维去考虑问题的,没有去考虑运行效率问题。测试平台在mandriva 2009下面。java版本是1.6,因为自己以前较少接触java,所以写一下测试环境。

  1.编写代码,将文件名命名为TestGoogle.java.

  2.编写代码:

  public class TestGoogle

  {

  public static void main(String args[])

  {

  String s = new String("http://www.google.cn/search?hl=zh-CN&q=siag+office&btnG=Google&meta=&aq=f&oq=");

  int n_pos;

  n_pos = s.indexOf("&");

  String str_temp = new String();

  str_temp = s.substring(n_pos+1,s.length());

  n_pos = str_temp.indexOf("&");

  str_temp = str_temp.substring(0,n_pos);

  n_pos = str_temp.indexOf("=");

  str_temp = str_temp.substring(n_pos+1,str_temp.length());

  System.out.println(str_temp);

  }

  }

  3.编译: javac TestGoogle.java

  4.运行: java TestGoogle

  5.结果:siag+office.

分享到:
0
相关阅读
友情链接
© 2018 我考网 http://www.woexam.com 中国互联网举报中心 湘ICP备18023104号 京公网安备 11010802020116号
违法和不良信息举报:9447029@qq.com