---
title: "面试中遇到的字符串题(NEW)"
author: "cuiran"
date: "2009-01-13T17:55:34Z"
tags: []
categories: ["编程语言"]
slug: "new"
draft: false
lastmod: "2009-01-13T17:55:34Z"
id: 315810
url: "https://cuiran.iteye.com/blog/315810"
---

今天下午在面试的时候遇到这样一个题，给定一个字符串str="   abc  ghi   jkl  mno  "；中间的空格是不相等的，要写两个方法，第一个是输入３进去返回ghi;第二种是：
输入ghi进去返回３.当时想到的方法是：用split();
现将代码复制:

package anxin.app;
/**
 * 
 *@author Cuiran
 *@date:2009-1-13 16:46
 *{@Email:cuiran2001@163.com}
 */
public class StringDemo {

	/**
	 * 
	 * @param 传进来的字符串
	 * @param 选取第三个
	 */
	public static void getString(String resString,int subcode){
		String strs=resString.split(" ");
		int n=0;
		for(int i=0;i<strs.length;i++){
			String newStr=strs*;
			if(newStr.length()!=0){
				n++;
				if(n==subcode){
					System.out.println(strs*);
				}
			}
			
		}
	}
	/**
	 * 
	 * @param 传进来的字符串
	 * @param 一部分字符串，返回数字
	 */
	public static void getInt(String resString,String substr){
		String strs=resString.split(" ");
		int n=0;
		for(int i=0;i<strs.length;i++){
			String newStr=strs*;
			if(newStr.length()!=0){
				n++;
				if(strs*.equals(substr)){
					System.out.println(n);
				}
			}
			
		}
	}
	/**
	 * @param args
	 */
	public static void main(String args) {
	String str="  abc  def   ghi   jkl  mno";

	StringDemo.getString(str, 3);
	StringDemo.getInt(str, "jkl");
	}

}