How do I use the value of a variable AS a variable - android

Haven't come across this in coding for the android yet. 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.
so fName[1] might equal file1.mp3
and then I want to create to new variable
file1_title.txt equals "Song Title One"
file1_desc.txt equals "Description of file One"
both based on the value of fname[1]
fName[2] equals file2.mp3
file2_title.txt equals "Song Title Two"
file2_desc.txt equals "Description of file Two"
both based on of the value fName[2]
etc...
How is this done for the android

I'm not 100% sure I understand the details of your questions, but use a Map. 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. 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.
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.
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)
}

Related

Android : Split string with { character

I have an string
String name = "\"edge_followed_by\":{\"count\":46199005},\"followed_by_viewer\":false,"
I want only this 46199005.
But { shows an error, when try to split the string
String[] separated = name.split("edge_followed_by\":{\"count\":");
Showing a suggestion , number expected and want me to replace with *.
Can anyone help me in this.
Just replace { with \{.
split is trying to use it as a part of regular expression.
Ideally, you should use JSON to parse this if you have proper structure. but if you want to get only the number you can split it using ":" and then split using "}". it should give you the exact number.
Why not to use:
String[] separated = name.split(":");
separated[2].split("}")[0];
Your string is not exact JSON object otherwise you can simply do json parsing and get the count value.
You can get count value using subtring operations like below:
String name = "\"edge_followed_by\":{\"count\":46199005},\"followed_by_viewer\":false,";
String substr = name.substring(name.indexOf("\"count\":") + 10);
String finalstr = substr.substring( 0, substr.indexOf("},"));
Log.d("Extracted_Value", finalstr); // output -> 46199005
There can be multiple ways. This is just one. Hope it will help you!

get specific characters of a String

I need to extract specfic information of a String. So I have to create two new Strings with the necessary information isolated.
The structure of the String: {line1=necessary information 1, line2=necessary information 2}
As you can see, I need the String values (necessary information 1: after '=' and before ',' and necessary information 2)
This is the String:
String telefonname_nummer = listview.getItemAtPosition((int) position).toString();
Thanks a lot.
You can try something like this:
String yourInfo = "line1=necessary information 1, line2=necessary information 2";
String[] parts = yourInfo.split(",");
String info1 = parts[0].split("=")[1];
String info2 = parts[1].split("=")[1];
try this way may help you
String urString = "line1=necessary information 1, line2=necessary information 2";
// First split your string with ","
String[] splitedString = urString.split(",");
//as u mention in Qestion u want string after "="
//so,
String firstString = splitedString [0].split("=")[1];
String scndString = splitedString [1].split("=")[1];
Log.e("firstString ",firstString );
Log.e("scndString",scndString);

How can i get few characters from String?

I want to retrieve few characters from string i.e., String data on the basis of first colon (:) used in string . The String data possibilities are,
String data = "smsto:....."
String data = "MECARD:....."
String data = "geo:....."
String data = "tel:....."
String data = "MATMSG:....."
I want to make a generic String lets say,
String type = "characters up to first colon"
So i do not have to create String type for every possibility and i can call intents according to the type
It looks like you want the scheme of a uri. You can use Uri.parse(data).getScheme(). This will return smsto, MECARD, geo, tel etc...
Check out the Developers site: http://developer.android.com/reference/android/net/Uri.html#getScheme()
Note: #Alessandro's method is probably more efficient. I just got that one off the top of my head.
You can use this to get characters up to first ':':
String[] parts = data.split(":");
String beforeColon = parts[0];
// do whatever with beforeColon
But I don't see what your purpose is, which would help giving you a better solution.
You should use the method indexOf - with that you can get the index of a certain char. Then you retrieve the substring starting from that index. For example:
int index = string.indexOf(':');
String substring = string.substring(index + 1);

need to retrieve data from the database

I have a very long string in the database that needs to be retrieved into a swipe view.
But,the problem is that the string comprises of set of "\n\n"
Whenever it is separated with this expression i need to put it in another slide,i mean i am using SWIPE view here..
if(tablecolumn==\\n\\n)
{
code to break it to parts
}
Is this how i should be doing it?
If i am wrong,how to break this string to different parts and enable it into SWIPE VIEW in to different swipe view?
You can simply break your string comprising of a special character like this :-
String str ="mynameisjhon.yournameisdash.bla";
, here you have a string concatenated with " . " (period character)
to break this string do this :-
StringTokenizer st = new StringTokenizer(str, "."); //break the string whenever "." occurs
String temp =st.nextToken(); // it will have "my name is jhon" break
String temp2 = st.nextToken();// it will have "your name is dash"
String temp3 = st.nextToken();//it will have "bla"
now your string is breaked into parts!
Anything else?
Load the whole string into your ViewAdapter and seperate it via substring
or load the string in your Activity/Fragment seperate it via substring, put the strings in an ArrayList, an initiate your ViewAdapter with the ArrayList as data source
either way use substring

Extract text from String

I have one String and into this string I have a url between two characters # such as "Hello world #http://thisurl# my name is Pippo" I want to take the url (http://thisurl) between two #.
How can I do ? Thanks
String data[] = str.split("#"); //spilliting string and taking into array
ArrayList<String> urlList = new ArrayList<String>();
for (int i = 0; i < data.length; i++) {
if(data[i].contains("http://"))
urlList.add(data[i]); //if string contains "http://" it means it is url save int list.
}
now you can get all uls from urlList.get(i) method.
this urlList will give you all the urls available in the string. I dint applied any null or other check. Apply it and try. If want something else try modifying content and checks.
Try String.split(). You really should be trying to google these things first.
here is an example - http://www.java-examples.com/java-string-split-example
The split method divides a string into several strings and store them into an array using a delimiter which can be defined by you.
the second element in the resulting array will be your URL

Categories

Resources