首页编程java编程java中trim是什么意思 java中trim()方法是用来干什么的

java中trim是什么意思 java中trim()方法是用来干什么的

编程之家2023-10-1298次浏览

大家好,关于java中trim是什么意思很多朋友都还不太明白,不过没关系,因为今天小编就来为大家分享关于java中trim()方法是用来干什么的的知识点,相信应该可以解决大家的一些困惑和问题,如果碰巧可以解决您的问题,还望关注下本站哦,希望对各位有所帮助!

java中trim是什么意思 java中trim()方法是用来干什么的

java中 nextToken().trim()中的.trim()指什么哦

源播放器YOYOPlayer是如何实现的.以下是java开源播放器YOYOPlayer歌曲列表的实现,希望能对你有所帮助:

/*

* To change this template, choose Tools| Templates

java中trim是什么意思 java中trim()方法是用来干什么的

* and open the template in the editor.

*/

package com.hadeslee.yoyoplayer.playlist;

java中trim是什么意思 java中trim()方法是用来干什么的

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;

}

}

java中的string trim具体有什么用处。。。

就是把一个字符串中可能存在的首尾空格去掉。例如有一个字符串如下:

" 123456 abcefg"

那么经过trim()操作就变成了"123456 abcefg"

-------------------------------------------------------

有时候需要校验,判断某个字符串变量的值不能为空,这个“空”的意思不光是指null,还指empty(就是形如""全空格的这种值),这时就可以用trim()方法把首尾空格去掉,然后再判断一下length就知道是不是empty了。

JS里面也有这个函数,功能是一样的。

java中a[i][j].toString().trim().equals("")是什么意思

首先解释 a[i][j]这是一个二维数组,

这个i j你可以看成是坐标,而这个坐标内是对应一个值的。

a[i][j].toString()是将a[i][j]这个对象转化为String类型,即字符串类型

.trim()是将转化后的字符串类型去掉前后空格。

最后.equal是一个函数,这里的意思是将转化后并去掉前后空格的字符串与一个空字符串进行比较,这一系列的最终是返回一个布尔值,也就是true或者 false

通俗一点,就是看a这个二维数组中的某个值是否为空字符串。!

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中trim是什么意思和java中trim()方法是用来干什么的的问题解决了您的问题,那么我们由衷的感到高兴!

java为什么叫面向对象 简答题,为什么说java是面向对象的编程语言java security是什么,java.security包的jar包叫什么