简体   繁体   English

如何使用变量的值作为变量

[英]How do I use the value of a variable AS a variable

Haven't come across this in coding for the android yet. 尚未在android编码中遇到此问题。 So how do I use the value of one variable as a new variable. 因此,如何将一个变量的值用作新变量。

I want to take the value of a variable like "file1.mp3" strip off the extension and then append text to the variable and use it as the variable name like file1_title.txt and file1_desc.txt. 我想从扩展名中删除“ file1.mp3”之类的变量的值,然后将文本附加到变量中,并将其用作诸如file1_title.txt和file1_desc.txt之类的变量名称。


so fName[1] might equal file1.mp3 因此fName [1]可能等于file1.mp3

and then I want to create to new variable 然后我想创建新变量

file1_title.txt equals "Song Title One" file1_title.txt等于“歌曲标题一”

file1_desc.txt equals "Description of file One" file1_desc.txt等于“文件一的描述”

both based on the value of fname[1] 两者都基于fname [1]的值


fName[2] equals file2.mp3 fName [2]等于file2.mp3

file2_title.txt equals "Song Title Two" file2_title.txt等于“歌曲标题二”

file2_desc.txt equals "Description of file Two" file2_desc.txt等于“文件二的描述”

both based on of the value fName[2] 两者都基于值fName [2]


etc... 等等...

How is this done for the android android如何做到这一点

I'm not 100% sure I understand the details of your questions, but use a Map. 我不确定100%是否了解您所提问题的详细信息,但可以使用地图。 The "key" would be the song title, the value would be the description. “键”将是歌曲标题,值将是描述。

Some followup. 一些后续行动。 Lots of handwaving, no error checking. 大量的手工操作,没有错误检查。 Assumes that an mp3 File is coming in, and somehow you read the title and description from the tags in the MP3 file. 假定一个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

Not sure if this is what you're looking for. 不知道这是您要找的东西。 It is basic Java string formating. 它是基本的Java字符串格式。

String attr1 = "song.mp3";
String attr2 = attr1.split(".")[0] + ".txt";

Naturally add the necessary null checks. 自然地添加必要的空检查。

==UPDATE== ==更新==

So if I understand you correctly, you get a file name ("asd.mp3") and need the song title and its description. 因此,如果我对您的理解正确,则会得到一个文件名(“ 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)

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM