---
title: "字符串切割"
author: "rayloo"
date: "2007-12-18T11:01:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2007-12-18T11:01:00Z"
id: 1008879
url: "https://rayloo.iteye.com/blog/1008879"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*分割行  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@paramsrc,width,height  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@date  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@returnstaticString\[]  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@autor  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)publicstaticString\[]split(Stringsrc,intwidth,intheight)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Vectorvector=newVector(20);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringbuffer="";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intbufferWidth=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intmaxBufferWidth=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=0;i&lt;src.length();i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//如果遇到换行时  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(src.charAt(i)==' ')  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)vector.addElement(buffer);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)maxBufferWidth=Math.max(maxBufferWidth,bufferWidth);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)buffer="";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)buffer+=src.charAt(i);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//计算buffer的宽度  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)bufferWidth=Font.getFont(Font.FACE\_PROPORTIONAL,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Font.STYLE\_PLAIN,Font.SIZE\_SMALL).stringWidth(buffer);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//如果buffer的宽度大于显示区域的宽  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(bufferWidth&gt;width)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)vector.addElement(buffer.substring(0,buffer.length()-1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)bufferWidth=Font.getFont(Font.FACE\_PROPORTIONAL,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Font.STYLE\_PLAIN,Font.SIZE\_SMALL).stringWidth(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)buffer.substring(0,buffer.length()-1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)maxBufferWidth=Math.max(maxBufferWidth,bufferWidth);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)buffer="";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)i--;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(i==src.length()-1)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)vector.addElement(buffer);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)maxBufferWidth=Math.max(maxBufferWidth,bufferWidth);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//获取数量  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)String\[]text=newString\[vector.size()];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//将数组循环赋值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=0;i&lt;vector.size();i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)text\[i]=(String)vector.elementAt(i);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returntext;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)