![](/img/trans.png)
[英]Algorithm to find song by song name and artist name, match closest string
[英]I'm trying to separate the name of the song and the artist
我正在尝试将歌曲的名称和歌手分开,我将内容文本视图命名为“ SongName_Artist”,如何将SongsName和Artist分成2个textview
获取布局(您将需要一个ID):
mLayout = findViewById(R.id.rootLayout);
查找您的内容文字:
contentTextView = findViewById(R.id.contentTextView);
String contentText = contentTextView.getText().toString();
为歌曲和歌手创建TextView
:
TextView songTextView = new TextView(this);
TextView artistTextView = new TextView(this);
songTextView.setText(contentText.split("_")[0]);
artistTextView.setText(contentText.split("_")[1]);
将它们添加到布局:
mLayout.addView(songTextView);
mLayout.addView(artistTextView);
Java具有内置的split函数,在这种情况下可以使用。 以下是Java代码的外观:
String whole = "Song_Artist";
String[] pieces = whole.split("_");
String song = pieces[0]; //everything before the _
String artist = pieces[1]; //everything after the _
现在,您已经分解了字符串,可以继续使用setText来根据需要设置textViews。
您可以使用string.split()方法来分隔字符串。
String str = "soniye_falak";
String[] parts = string.split("_");
String song = parts[0]; // soniye
String artist = parts[1] //falak
split()方法用于将字符串拆分为子字符串数组,并返回新数组。 如果将空字符串(“”)用作分隔符,则该字符串将在每个字符之间分割。 注意:split()方法不会更改原始字符串。
您可以使用字符串Eg(“ your_string”)。split(“ _”)的拆分函数并收集返回值代码,请执行以下操作:
String song_artist = "S_A";
System.out.println(song_artist.split("_"));
String[] temp = song_artist.split("_");
for(int i=0; i<temp.length;i++) {
System.out.println(temp[i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.