How to extract particular part of a string? - android

I have a Link which never changes. The only thing which changes is the file name (in bold). so how do I get the file name from this link? I want to display this in a textview or toast-
http://apkins.aptoide.com/aptoide-8.1.0.1.apk?=x

Here is a regex-less solution:
public static String getFileName(String link) {
int endIndex = link.lastIndexOf('?');
return link.substring(26, endIndex);
}
It's pretty simple. It first gets where the file name ends i.e. the position of the last "?". Then it "cuts out" a new string from the link starting from the 27th character to the end of the file name.

This should do the trick
String fileName = url.substring(url.lastIndexOf('/') + 1);
You can also try URLUtil.guessFileName(url, null, null)
Edit 1
String url = "any url"

String given = "http://apkins.aptoide.com/aptoide-8.1.0.1.apk?=x";
String extractedAppName = given.substring(given.lastIndexOf("/") + 1, given.lastIndexOf("?"));
This should work. It takes the position of the / before the name and then the position of the ?. It takes the string (substring) between those two characters. Just give it a try and you'll see.

Related

Parsing a string?

So I have the following string:
String text = "\t\t\torder #168\n\t\t\tpaid\n\t\t\tview 4 items\n\t\t\tpicked up\n\t\t\tcomplete pickup\n\t\t\t2 stops";
How do I parse this string so that I always get the 2 in front of stops? I have tried the following, but it always returns 2 stops.
String substr = "complete pickup";
String numberOfStops = text.substring(text.indexOf(substr) + substr.length());
numberOfStops = numberOfStops.replaceAll("^\\s+","").replaceAll("\\s+$","");
The short way:
numberOfStops = numberOfStops.replaceAll("^\\s+","").replaceAll("\\s+$","").replace("stops","");
The flexible way is using Regex, and Pattern and Match classes. Let me know if you need it

Remove all <tag></tag> with text between

I'm trying to remove all text tagged like this (including the tags)
<tag>TEXT</tag>
from a String.
I have tried
.replaceAll("<tag>.+/(tag)*>", "")
or
.replaceAll("<tag>.*(tag)*>", "")
but neither works correctly and I can't replace the tagged text with ""
I don't know exactly what you want, so here are a few options:
String text = "ab<tag>xyz</tag>cd";
// Between
text.replaceAll("<tag>.+?<\/tag>", "<tag></tag>"); // ab<tag></tag>cd
// Everything
text.replaceAll("<tag>.+?<\/tag>", ""); // abcd
// Only tags
text.replaceAll("<\/?tag>", ""); // abxyzcd
EDIT:
The problem was the missing ? after the .+. The question mark only matches the first occurence, so it works when multiple tags are present which was the case.
Change to this ,
String nn1="<tag>TEXT</tag>";
nn1=nn1.replace("<tag>","");
nn1=nn1.replace("</tag>","");
OR
String nn1="<tag>TEXT</tag>";
nn1=nn1.replaceAll("<tag>","");
nn1=nn1.replaceAll("</tag>","");
Output : TEXT
I hope this helps you.
public static void removeTAG()
{
String str = "<tag>Your Long String</tag>";
for(int i=0;i<str.length();i++)
{
str = str.replace("<tag>", "");
str = str.replace("</tag>", "");
}
System.out.println(str);
}
Here what i did and output was as expected
Output Your Long String
You can use the below regular expression.
.replaceAll("<tag>.+?<\/tag>", "<tag></tag>");
This removes all the tags whether it's an HTML or an XML tag.

how to remove special character from string except + in android?

I am fetching number from contact book and sending it to server. i get number like this (+91)942 80-60 135 but i want result like this +9428060135.+ must be first character of string number.
Given your example you want to replace the prefix with a single + character. You also want to remove other non-numeric characters from the number string. Here's how you can do that:
String number = "(+91)942 80-60 135";
number = "+" + number.replaceAll("\\(\\+\\d+\\)|[^\\d]", "");
The regex matches any prefix (left paren followed by a + followed by one or more digits, followed by a right paren) or any non digit character, and removes them. This is concatenated to a leading + as required. This code will also handle + characters within the number string, e.g. +9428060135+++ and +(+91)9428060135+++.
If you simply wanted to remove any character that is not a digit nor a +, the code would be:
String number = "(+91)942 80-60 135";
number = number.replaceAll("[^\\d+]", "");
but be aware that this will retain the digits in the prefix, which is not the same as your example.
You can use String.replace(oldChar, newChar). Use the code below
String phone = "(+91)942 80-60 135"; // fetched string
String trimmedPhone = phone.replace("(","").replace(")","").replace("-","").trim();
I hope it will work for you.
check this. Pass your string to this function or use as per code goes
String inputString = "(+91)942 80-60 135";
public void removeSpecialCharacter(String inputString) {
String replaced = inputString.replaceAll("[(\\-)]", "");
String finalString = replaced.replaceAll(" ", "");
Log.e("String Output", " " + replaced + " " + second);
}

how to get text with using substring

I try to get only this part "9916-4203" in "Region Code:9916-4203 " in android. How can I do this?
I tried below code, I used substring method but it doesn't work:
firstNumber = Integer.parseInt(message.substring(11, 19));
If you know that string contains "Region Code:" couldn't you do a replace?
message = message.replace("Region Code:", "");
Assumed that you have only one phone number in your String, the following will remove any non-digit characters and parse the resulting number:
public static int getNumber(String num){
String tmp = "";
for(int i=0;i<num.length();i++){
if(Character.isDigit(num.charAt(i)))
tmp += num.charAt(i);
}
return Integer.parseInt(tmp);
}
Output in your case: 99164203
And as already mentioned, you won't be able to parse any String to Integer in case there are any non-digit characters
Im going to guess that what you want to extract is the full region code text minus the title. So maybe using regex would be a good simple fit for you?
String myString = "Region Code:9916-4203";
String match = "";
String pattern = "\:(.*)";
Pattern regEx = Pattern.compile(pattern);
Matcher m = regEx.matcher(myString);
// Find instance of pattern matches
Matcher m = regEx.matcher(myString);
if (m.find()) {
match = m.group(0);
}
Variable match will contain "9916-4203"
This should work for you.
Java code sourced from http://android-elements.blogspot.in/2011/04/regular-expressions-in-android.html
In Java the substring() method works with the first parameter being inclusive and the second parameter being exclusive. Meaning "Hello".substring(0, 2); will result in the string He.
In addition to excluding the parsing of something that isn't a number like #Opiatefuchs mentioned, your substring method should instead be message.substring(12, 21).

How to replace character at particular index in android?

I am working on android. I have a string containing huge data. In that string I want to replace a particular character to another character. I got the index of the character which I want to replace. But I am unable to replace that character.
How can I do that?
String str = "data1data2mdata2test1test2test3dd"
int ind = str.indexOf("m");
System.out.println("the index of m" + ind);
Now in the above string I want to replace the character "m"(after data2) to "#".
Now how can I replace the m to #. Please help me in this reagard.
You can use substring:
String newStr = str.substring(0, ind) + '#' + str.substring(ind + 1);
Try this:
str = str.replaceFirst("m", "#");
It will replace the first m to #
String str1 = "data1data2mdata2test1test2test3dd"
String str = str1.replace("m", "#");
System.out.println(str);
So you are getting 10 as system out,
so this way you can replace it like,
Str.replace('m', '#')--->when you want all occurrences of it to replace it,
Or if you want only first occurrence to be replaced by # then you can do following trick,
StringBuffer buff=new StringBuffer();
buff.append(Str.substring(0,ind)).append("#").append(Str.substring(ind+1));
i hope it would help

Categories

Resources