尚未遇到过编码Android的问题。那么我如何使用一个变量的值作为新变量。
我想取一个像“file1.mp3”这样的变量的值剥离扩展,然后将文本附加到变量,并将其用作变量名,如file1_title.txt和file1_desc.txt。
所以fName [1]可能等于file1.mp3
然后我想创建新变量
file1_title.txt等于“Song Title One”
file1_desc.txt等于“文件一描述”
两者都基于fname [1]
的值fName [2]等于file2.mp3
file2_title.txt等于“Song Title Two”
file2_desc.txt等于“文件二的描述”
两者都基于值fName [2]
等...
这是如何为android
完成的答案 0 :(得分:2)
我不是100%确定我理解你的问题的细节,而是使用地图。 “key”将是歌曲标题,值将是描述。
一些跟进。很多handwaving,没有错误检查。假设有一个mp3文件进来,不知怎的,你从MP3文件中的标签读取标题和描述。 YMMV
// TreeMap will sort by titles which seems reasonable
Map<String, String> songMapTitleToDesc = new TreeMap<String, String>();
MyMP3Reader mmp3r = new MyMP3Reader(File inFile);
String songTitle = mmp3r.getSongTitle();
String songDesc = mmp3r.getSongDesc();
songMapTitleToDesc.put(songTitle, songDesc);
mmp3r.close(); // or whatever
答案 1 :(得分:1)
不确定这是否是您要找的。它是基本的Java字符串格式化。
String attr1 = "song.mp3";
String attr2 = attr1.split(".")[0] + ".txt";
自然添加必要的空值检查。
== UPDATE ==
因此,如果我理解正确,你会得到一个文件名(“asd.mp3”)并需要歌曲标题及其描述。
String attr1 = "song.mp3";
String songname = "";
String songdesc = "";
String[] splitArray = attr1.split(".");
if(splitArray[0] != null){
String songname = attr1.split(".")[0];
File f = new File(path + songname +".txt");
//I didn't quite understand in what format you get the data,
//especially the description. However, it could be in a map
//where the songname is the key, as suggested above, and here you would write that description to file(f)
}