当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
用java从字符串中截取指定子串
发布时间:2010/2/10 14:06:36 来源:www.xue.net 编辑:城市总裁吧

  下面的代码是从最简单的思维去考虑问题的,没有去考虑运行效率问题。测试平台在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.

广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved