I have following two string:
String one:
"abcabc/xyzxyz/12345/random_num_09/somthing_random.txt"
String Two:
"abcabc/xyzxyz/12345/"
What i want to do is attach path "random_num_09/somthing_random.txt" from string one two string two. So how can i subtract string two from string one and then attach remaining part to string two.
I have tried to do it by searching for the second last "/" in the string one and then doing sub string and attaching it to string two.
But is there any better way of doing it.
Thanks.
I think the best way is to use substrings, as you said:
String string_one = "abcabc/xyzxyz/12345/random_num_09/somthing_random.txt";
String string_two = "abcabc/xyzxyz/12345/";
String result = string_two + string_one.substring(string_one.indexOf(string_two)+1));
The other possibility is to use regex, but you would still be doing concatenation to get the result.
Pattern p = Pattern.compile(string_two+"(.*)");
Matcher m = p.matcher(string_one);
if (m.matches()) {
String result = string_two+m.group(1);
}
rather that a substring, replace is simpler to use:
String string1 = "abcabc/xyzxyz/12345/random_num_09/somthing_random.txt";
String string2 = "abcabc/xyzxyz/12345/";
String res = string2 + string1.replace(string2, "");
Related
I used this:
String message += getResources().getString(R.string.string1) + "some more word...";
and I wanted to send this string via sms, but it is not working. It works fine without the string resource. Am I missing something?
#forpas answer is absolutely correct, but you can also concat string resource this way.
<string name="name">Name %s</string>
String nameText = getString(R.string.name,"khemraj");
When you use += operator with a String the result is a concatenation of the previous value of the String with some new String.
When you define a String variable like this:
String s;
the variable s is not initialized, so this:
s+="something";
is not allowed.
So instead of
String message += getResources().getString(R.string.string1) + "some more word...";
do
String message = getResources().getString(R.string.string1) + "some more word...";
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);
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);
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 8 years ago.
I have data with repeating pattern '###'.I want to extract all strings between this pattern.
The data is like -
son###can###e###nick###54###
how can i get all data between the pattern '###'.
try this:
String patternString = "###";
Pattern pattern = Pattern.compile(patternString);
String[] split = pattern.split(text);
You can use Scanner with "###" delimiter.
Scanner in = new Scanner("son###can###e###nick###54###");
in.useDelimiter("###");
while(in.hasNext)
String x = in.next();
//Do something with x;
x will hold everything between ###. You can easily store them in an Array inside the loop or use them anyway you like.
Try this:
String str = "son###can###e###nick###54###";
String newStr = str.replaceAll("[#]+", "");
or you can separate all world via
String rem = "###";
Pattern pattern = Pattern.compile(rem);
String[] splitarr = pattern.split(text);
for(int i=0;i<aplitarr.length();i++)
{
String word=aplitarr[i].ToString();
}
Hope this may help you!
try,
String[] separated = CurrentString.split("###");
separated[0]; // this will contain "son"
separated[1]; // this will contain "can"
or
StringTokenizer tokens = new StringTokenizer(CurrentString, "###");
String first = tokens.nextToken();// this will contain "son"
String second = tokens.nextToken();// this will contain "can"
I want to split string got from bluetooth. i'm using
StringTokenizer splitStr = new StringTokenizer(readMessage, "\\|");
String numberSpeed = splitStr.nextToken(); //splitStr[0].replaceAll("\\D+","");
String numberTorque = splitStr.nextToken(); //splitStr[1].replaceAll("\\D+","");
numberSpeed = numberSpeed.replaceAll("\\D+","");
numberTorque = numberTorque.replaceAll("\\D+","");
Did it with split string before.
If i get corupted data without delimiter the app crashes while trying to do impossible.
How to check if there is delimiter or not and then proceed split or skip it?
you can check for delimeter in string by contains() method
if(str.contains("_your_delimiter")) { //for safe side convert your delimeter and search string to lower case using method toLowerCase()
//do your work here
}
Try this, I use it in my app.
String container = numberSpeed ;
String content = "\\D+";
boolean containerContainsContent = StringUtils.containsIgnoreCase(container, content);
It will return true if it has delimiter, and false it not.
Use that with an if statement.
ex.
if(containerContainsContent){
//split it
} else {
//skip it
}
This is quote from tokenizer docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.
Try to user String.split() instead.
if(str.contains(DEILIMITER)) {
String tab[] = str.split(DEILIMITER);
//enter code here
}