java trim 为什么不去空格 java中trim()方法是用来干什么的
很多朋友对于java trim 为什么不去空格和java中trim()方法是用来干什么的不太懂,今天就由小编来为大家分享,希望可以帮助到大家,下面一起来看看吧!
java中trim()方法会清楚0开头吗
在 Java中,trim()方法用于去除字符串两端的空格(包括空格、制表符、换行符等)。trim()方法不会清除字符串中间的空格,只会去除两端的空格。
trim()方法不会清除字符串开头的零(0),它只会去除开头和结尾的空格字符。如果你想要去除字符串开头的零,可以使用其他方法,例如使用正则表达式或自定义方法来处理。
以下是一个示例,演示如何去除字符串开头的零:
在上述示例中,replaceFirst("^0+","")方法使用正则表达式 ^0+来匹配字符串开头的零,并将其替换为空字符串。
java中trim()方法是用来干什么的
trim()的作用是去掉字符串两端的多余的空格,注意,是两端的空格,且无论两端的空格有多少个都会去掉,当然
中间的那些空格不会被去掉,如:
String s=" a s f g";
String s1= s.trim();
那么s1就是"a s f g",可见,这和上面所说的是一样的。
trim()不仅可以去掉空格,还能去掉其他一些多余的符号,这些符号分别是:
\t \n \v \f \r \x0085 \x00a0? \u2028 \u2029
翻译过来分别是:水平制表符,换行符,垂直制表符,换页符,回车,后面的这几个除了问号外,其他的都是转义符形式写法。
扩展资料:trim()函数移除字符串两侧的空白字符或其他预定义字符。
功能除去字符串开头和末尾的空格或其他字符。函数执行成功时返回删除了string字符串首部和尾部空格的字符串,发生错误时返回空字符串("")。如果任何参数的值为NULL,Trim()函数返回NULL。
参考资料:Trim函数–百度百科
Java问题:从字符串中提取用空格隔开的数字
如果按照你说的意思“从字符串中提取用空格隔开的数字”
下面这种方法是对的"取出空格隔开的数据"
String str="16 5 12 136";
String[] s= str.split("");
//你可以把得到的值强转成int
for(int i= 0; i< s.length; i++){
System.out.println(s[i]);
}
如果按照你给的例子:“如字符串“16 5 12 136”输出为:16512136”
那么下面应该是对的
String str="16 5 12 136";
String[] s= str.split("");
//你可以把得到的值强转成int
for(int i= 0; i< s.length; i++){
System.out.print(s[i]);
}这样就可以输出你要的结果了
java中 nextToken().trim()中的.trim()指什么哦
源播放器YOYOPlayer是如何实现的.以下是java开源播放器YOYOPlayer歌曲列表的实现,希望能对你有所帮助:
/*
* To change this template, choose Tools| Templates
* and open the template in the editor.
*/
package com.hadeslee.yoyoplayer.playlist;
import com.hadeslee.yoyoplayer.util.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;
/**
*一个播放列表的基本实现
*@author hadeslee
*/
public class BasicPlayList implements PlayList{
private static final long serialVersionUID= 20071214L;
protected Vector<PlayListItem> playList;
protected int currentIndex=-1;
protected boolean isModified;
protected String M3UHome;//MP3U格式列表的位置
protected String PLSHome;//PLS格式列表的位置
private String name;//表示此播放列表的名字
private Config config;//全局的配置对象
private PlayListItem playing;//正在播放的项
public BasicPlayList(Config config){
this.config= config;
playList= new Vector<PlayListItem>();
}
public boolean load(String filename){
setModified(true);
boolean loaded= false;
if((filename!= null)&&(filename.toLowerCase().endsWith(".m3u"))){
loaded= loadM3U(filename);
} else if((filename!= null)&&(filename.toLowerCase().endsWith(".pls"))){
loaded= loadPLS(filename);
}
return loaded;
}
protected boolean loadM3U(String filename){
boolean loaded= false;
BufferedReader br= null;
try{
// Playlist from URL?(http:, ftp:, file:....)
if(Config.startWithProtocol(filename)){
br= new BufferedReader(new InputStreamReader((new URL(filename)).openStream()));
} else{
br= new BufferedReader(new FileReader(filename));
}
String line= null;
String songName= null;
String songFile= null;
String songLength= null;
while((line= br.readLine())!= null){
if(line.trim().length()== 0){
continue;
}
if(line.startsWith("#")){
if(line.toUpperCase().startsWith("#EXTINF")){
int indA= line.indexOf(",", 0);
if(indA!=-1){
songName= line.substring(indA+ 1, line.length());
}
int indB= line.indexOf(":", 0);
if(indB!=-1){
if(indB< indA){
songLength=(line.substring(indB+ 1, indA)).trim();
}
}
}
} else{
songFile= line;
if(songName== null){
songName= songFile;
}
if(songLength== null){
songLength="-1";
}
PlayListItem pli= null;
if(Config.startWithProtocol(songFile)){
// URL.
pli= new PlayListItem(songName, songFile, Long.parseLong(songLength), false);
} else{
// File.
File f= new File(songFile);
if(f.exists()){
pli= new PlayListItem(songName, songFile, Long.parseLong(songLength), true);
} else{
// Try relative path.
f= new File(config.getLastDir()+ songFile);
if(f.exists()){
pli= new PlayListItem(songName, config.getLastDir()+ songFile, Long.parseLong(songLength), true);
} else{
// Try optional M3U home.
if(M3UHome!= null){
if(Config.startWithProtocol(M3UHome)){
pli= new PlayListItem(songName, M3UHome+ songFile, Long.parseLong(songLength), false);
} else{
pli= new PlayListItem(songName, M3UHome+ songFile, Long.parseLong(songLength), true);
}
}
}
}
}
if(pli!= null){
this.appendItem(pli);
}
songFile= null;
songName= null;
songLength= null;
}
}
loaded= true;
} catch(Exception e){
} finally{
try{
if(br!= null){
br.close();
}
} catch(Exception ioe){
}
}
name= Util.getSongName(new File(filename));
return loaded;
}
protected boolean loadPLS(String filename){
boolean loaded= false;
BufferedReader br= null;
try{
// Playlist from URL?(http:, ftp:, file:....)
if(Config.startWithProtocol(filename)){
br= new BufferedReader(new InputStreamReader((new URL(filename)).openStream()));
} else{
br= new BufferedReader(new FileReader(filename));
}
String line= null;
String songName= null;
String songFile= null;
String songLength= null;
while((line= br.readLine())!= null){
if(line.trim().length()== 0){
continue;
}
if((line.toLowerCase().startsWith("file"))){
StringTokenizer st= new StringTokenizer(line,"=");
st.nextToken();
songFile= st.nextToken().trim();
} else if((line.toLowerCase().startsWith("title"))){
StringTokenizer st= new StringTokenizer(line,"=");
st.nextToken();
songName= st.nextToken().trim();
} else if((line.toLowerCase().startsWith("length"))){
StringTokenizer st= new StringTokenizer(line,"=");
st.nextToken();
songLength= st.nextToken().trim();
}
// New entry?
if(songFile!= null){
PlayListItem pli= null;
if(songName== null){
songName= songFile;
}
if(songLength== null){
songLength="-1";
}
if(Config.startWithProtocol(songFile)){
// URL.
pli= new PlayListItem(songName, songFile, Long.parseLong(songLength), false);
} else{
// File.
File f= new File(songFile);
if(f.exists()){
pli= new PlayListItem(songName, songFile, Long.parseLong(songLength), true);
} else{
// Try relative path.
f= new File(config.getLastDir()+ songFile);
if(f.exists()){
pli= new PlayListItem(songName, config.getLastDir()+ songFile, Long.parseLong(songLength), true);
} else{
// Try optional PLS home.
if(PLSHome!= null){
if(Config.startWithProtocol(PLSHome)){
pli= new PlayListItem(songName, PLSHome+ songFile, Long.parseLong(songLength), false);
} else{
pli= new PlayListItem(songName, PLSHome+ songFile, Long.parseLong(songLength), true);
}
}
}
}
}
if(pli!= null){
this.appendItem(pli);
}
songName= null;
songFile= null;
songLength= null;
}
}
loaded= true;
} catch(Exception e){
} finally{
try{
if(br!= null){
br.close();
}
} catch(Exception ioe){
}
}
name= Util.getSongName(new File(filename));
return loaded;
}
public boolean save(String filename){
// Implemented by C.K
if(playList!= null){
BufferedWriter bw= null;
try{
bw= new BufferedWriter(new FileWriter(filename));
bw.write("#EXTM3U");
bw.newLine();
Iterator<PlayListItem> it= playList.iterator();
while(it.hasNext()){
PlayListItem pli= it.next();
bw.write("#EXTINF:"+ pli.getM3UExtInf());
bw.newLine();
bw.write(pli.getLocation());
bw.newLine();
}
return true;
} catch(IOException e){
} finally{
try{
if(bw!= null){
bw.close();
}
} catch(IOException ioe){
}
}
}
return false;
}
public void addItemAt(PlayListItem pli, int pos){
playList.add(pos, pli);
for(int i= 0; i< playList.size(); i++){
if(playList.get(i).isSelected()){
currentIndex= i;
}
}
setModified(true);
if(Config.getConfig().getReadTagInfoStrategy().equals(Config.READ_WHEN_ADD)){
pli.getTagInfo();
}
}
public void removeItem(PlayListItem pli){
playList.remove(pli);
setModified(true);
for(int i= 0; i< playList.size(); i++){
if(playList.get(i).isSelected()){
currentIndex= i;
}
}
}
public void removeItemAt(int pos){
playList.remove(pos);
setModified(true);
for(int i= 0; i< playList.size(); i++){
if(playList.get(i).isSelected()){
currentIndex= i;
}
}
}
public void removeAllItems(){
playList.clear();
currentIndex=-1;
setModified(true);
}
public void appendItem(PlayListItem pli){
playList.add(pli);
setModified(true);
if(Config.getConfig().getReadTagInfoStrategy().equals(Config.READ_WHEN_ADD)){
pli.getTagInfo();
}
}
public void sortItems(int sortmode){
}
public PlayListItem getItemAt(int pos){
if(pos< playList.size()&& pos>-1){
return playList.get(pos);
}
return null;
}
public Vector<PlayListItem> getAllItems(){
return playList;
}
public int getPlaylistSize(){
return playList.size();
}
public void shuffle(){
int size= playList.size();
if(size< 2){
return;
}
List<PlayListItem> v= playList;
playList= new Vector<PlayListItem>(size);
while((size= v.size())> 0){
playList.add(v.remove((int)(Math.random()* size)));
}
begin();
//此次可能不一定需要这个方法,因为本身此方法就是打
//乱播放列表的顺序,所以此时再点击下一首不一定就是
//当前播放的歌曲的下一首
for(int i= 0; i< playList.size(); i++){
if(playList.get(i).isSelected()){
currentIndex= i;
}
}
}
public PlayListItem getCursor(){
if((currentIndex< 0)||(currentIndex>= playList.size())){
return null;
}
return getItemAt(currentIndex);
}
public void begin(){
currentIndex=-1;
if(getPlaylistSize()> 0){
currentIndex= 0;
}
setModified(true);
}
public int getSelectedIndex(){
return currentIndex;
}
public int getIndex(PlayListItem pli){
return playList.indexOf(pli);
}
public void nextCursor(){
//如果是随机播放,则随机取一个下标
if(config.getPlayStrategy()== Config.RANDOM_PLAY){
currentIndex=(int)(Math.random()* playList.size());
//否则就按顺序
} else if(config.getPlayStrategy()== Config.ORDER_PLAY){
currentIndex++;
}
if(config.isRepeatEnabled()&& currentIndex>= playList.size()){
currentIndex= 0;
}
}
public void previousCursor(){
//如果是随机播放,则随机取一个下标
if(config.getPlayStrategy()== Config.RANDOM_PLAY){
currentIndex=(int)(Math.random()* playList.size());
//否则就按顺序
} else if(config.getPlayStrategy()== Config.ORDER_PLAY){
currentIndex--;
}
if(config.isRepeatEnabled()&& currentIndex< 0){
currentIndex= playList.size()- 1;
}
}
public boolean setModified(boolean set){
return isModified= set;
}
public boolean isModified(){
return isModified;
}
public void setCursor(int index){
currentIndex= index;
playing= playList.get(index);
}
public void setName(String name){
this.name= name;
}
public String getName(){
return name;
}
/**
* Get M3U home for relative playlist.
*
*@return
*/
public String getM3UHome(){
return M3UHome;
}
/**
* Set optional M3U home for relative playlist.
*
*@param string
*/
public void setM3UHome(String string){
M3UHome= string;
}
/**
* Get PLS home for relative playlist.
*
*@return
*/
public String getPLSHome(){
return PLSHome;
}
/**
* Set optional PLS home for relative playlist.
*
*@param string
*/
public void setPLSHome(String string){
PLSHome= string;
}
public String toString(){
return name;
}
public void setItemSelected(PlayListItem pl, int index){
if(pl== null){
return;
}
// for(PlayListItem p: playList){
// p.setSelected(false);
//}
// pl.setSelected(true);
this.currentIndex= index;
}
}
END,本文到此结束,如果可以帮助到大家,还望关注本站哦!