---
title: "截取字符串，是汉字的，如果截取了半个就必须截取完整"
author: "befairy"
date: "2010-10-26T16:59:46Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-10-26T16:59:46Z"
id: 794698
url: "https://befairy.iteye.com/blog/794698"
---

package number;

public class TestString {

	public static void main(String args) {
		String str ="中国mobile!";
		System.out.println(cutString(str,3));

	}
	public static String cutString(String str,int length){
		StringBuffer sbStr=new StringBuffer();
		int count=0,i=0;
		for(;i<length&&count<length;i++,count++){
			sbStr.append(str.charAt(i));
			if(String.valueOf(str.charAt(i)).matches("")){
			   count++;
			}
		}
		return sbStr.toString();
	}

}