I have a little problem with splitting a String
String anl_gewerk = "Text Text Text (KG 412/2)"
String[] parts = anl_gewerk.split("[(]");
anl_gewerk = parts[1];
anl_gewerk = anl_gewerk.replaceAll("\\(","").replaceAll("\\)","");
anl_gewerk = anl_gewerk.replace("KG","");
I have the aforementioned string, and I'm searching for "412/2".
Therefore, I want to split the String into two substrings searching for "(".
Finally I want to grab this String deleting "(", "KG", " " and ")".
When I select anl_gewerk = parts[0]; it works but I get the wrong part, when I change into parts[1] the App crashes.
Please help me
Try to change your code by this:
String anl_gewerk = "Text Text Text (KG 412/2)";
String[] parts = anl_gewerk.split("[(]");
anl_gewerk = parts[1];
String sub[] = anl_gewerk.split(" ");
String test = sub[1];
String result = test.replace(")","");// it gives your result 412/2
Related
I have a string which looks like this
[{"TransactionId":"3574780600039252015-12-24 T 14:22:03"
Now I want to split only the text where the "TransactionId" will be part one and after : will be the second part.
Code I've tried :
String[] transid_result = result.split(":");
String part1 = transid_result[0];
String part2 = transid_result[1];
The result is
part1 contains [{"TransactionId
part2 contains "3574780600039252015-12-24 T 14
I want part2 to contain "3574780600039252015-12-24 T 14:22:03"
Can anyone help me ?
You could search for the first : and manually split by that:
int firstColon = result.indexOf(":");
String part1 = result.substring(0,firstColon);
String part2 = result.substring(firstColon+1, result.length());
If you're sure the data will always be in this format, then you could use
String[] elements = result.split("\"");
String transactionId = elements[3];
I have a string that I want to break down and assign different part of this string to different variables.
String:
String str ="NAME=Mike|Phone=555.555.555| address 298 Stack overflow drive";
To Extract the Name:
int startName = str.indexOf("=");
int endName = str.indexOf("|");
String name = str.substring(startName +1 , endName ).trim();
But I can't extract the phone number:
int startPhone = arg.indexOf("|Phone");
int endPhone = arg.indexOf("|");
String sip = arg.substring(startPhone + 7, endPhone).trim();
Now how can I extract the phone number that is between delimiter "|".
Also, is there a different way to extract the name using the between delimiter "=" & the first "|"
You can split on both = and | at the same time, and then pick the non-label parts
String delimiters = "[=\\|]";
String[] splitted = str.split(delimiters);
String name = splitted[1];
String phone = splitted[3];
Note that his code assumes that the input is formatted exactly as you posted. You may want to check for whitespace and other irregularities.
String[] details = str.split("|");
String namePart = details[0];
String phonePart = details[1];
String addressPart = details[2];
String name = namePart.substring(namePart.indexOf("=") + 1).trim();
String phone = phonePart.substring(phonePart.indexOf("=") + 1).trim();
String address = addressPart.trim();
I hope this could help.
My requirement is to check if a group of words or a single word is present in a larger string. I tried using String.contains() method but this fails in case the larger string has new line character. Currently I am using a regex mentioned below. But this works for only one word. The searched text is a user entered value and can contain more than one word. This is an android application.
String regex = ".*.{0}" + searchText + ".{0}.*";
Pattern pattern = Pattern.compile(regex);
pattern.matcher(largerString).find();
Sample String
String largerString ="John writes about this, and John writes about that," +
" and John writes about everything. ";
String searchText = "about this";
Why not just replace line breaks with spaces, and on top of that, convert it all to lower case?
String s = "hello";
String originalString = "Does this contain \n Hello?";
String formattedString = originalString.toLowerCase().replace("\n", " ");
System.out.println(formattedString.contains(s));
Edit: Thinking about it, I don't really understand how line breaks make a difference...
Edit 2: I was right. Line breaks don't matter.
String s = "hello";
String originalString = "Does this contain \nHello?";
String formattedString = originalString.toLowerCase();
System.out.println(formattedString.contains(s));
here is code not using regex.
String largerString = "John writes about this, and John writes about that," +" and John writes about everything. ";
String searchText = "about this";
Pattern pattern = Pattern.compile(searchText);
Matcher m = pattern.matcher(largerString);
if(m.find()){
System.out.println(m.group().toString());
}
Result:
about this
I hope it will help you.
I want to retrieve the info of a line of text from a textfile in 2 different strings...
This is the situation, getting the password line from pwd.txt :
String pwdRetrieved = retreivePwd.getPwd("pwd.txt");
However the password is crypted and it needs 2 valors, the password itself + a "key", like this:
4B5CB9348D5ADB733D43C2FB57A6A971-admin_pwd
admin_pwd is the "key" or "reference" to the encrypted password.
Now what I want to do is get 4B5CB9348D5ADB733D43C2FB57A6A971 into a string and admin_pwd into another string, is that possible?
More specific, i want to read from the .txt until the character "-" is found and store it into a String, then i want it to keep reading after "-" and store it into another string.
Read the whole string, split on the "-" and retrieve the two parts from the created array:
String pwdRetrieved = retreivePwd.getPwd("pwd.txt");
String[] splitPwdRetrieved = pwdRetrieved.split("-");
String password = splitPwdRetrieved[0];
String key = splitPwdRetrieved[1];
You can try this..
String[] pwd = pwdRetrieved.split("-");
After you can split this array value into individual strings.
Using String's split method will do the trick:
String[] split = pwdRetrieved.split("-");
Will return a string array with the two strings you are after
String[] split = pwdRetrieved.split("-");
String enc=split[0];
String pass=split[1];
you could split your String at the - after reading the whole line like this:
String pwdRetrieved = retreivePwd.getPwd("pwd.txt");
String[] split = pwdRetrieved .split("-");
System.out.println(split[0]);
System.out.println(split[1]);
You could do this:
String[] parts = pwdRetrieved.split("-");
String password = parts[0];
String key = parts[1];
Or do this:
int dashPosition = pwdRetrieved.indexOf("-");
String password = pwdRetrieved.substring(0, dashPosition);
String key = pwdRetrieved.substring(dashPosition + 1);
I have a URL and I want it to look like this:
Action Manatee - Action
http://xxxxxx.com/songs2/Music%20Promotion/Stream/Action%20Manatee%20-%20Action.mp3
What is the syntax for trimming up to where it after this "Stream/" and make spaces where the %20 is. I also want to trim the .mp3
Hmm, for that particular example, I would split the string according to the '/' character then trim the text that follows the final '.' character. Finally, do a replace of "%20" into " ". That should leave you with the string you want
Tested
String initial = "http://xxxxxx.com/songs2/Music%20Promotion/Stream/Action%20Manatee%20-%20Action.mp3";
String[] split = initial.split("/");
String output = split[split.length-1];
int length = output.lastIndexOf('.');
output = output.substring(0, length);
output = output.replace("%20", " ");
String urlParts[] = URL.split("\/");
String urlLast = urlParts[length-1];
String nameDotMp = urlLast.replaceAll("%20");
String name = nameDotMp.substring(0,nameDotMp.length-5);
You could use the split() and replace() methods to accomplish this, here are two ways:
Split your string apart by using the forward slashes:
string yourUrl = [URL Listed];
//Breaks your URL into sections on slashes
string[] sections = yourUrl.split("\/");
//Grabs the last section after the slashes, and replaces the %20 with spaces
string newString = sections[sectiongs.length-1].replace("%20"," ");
Split your string at the Stream/ section: (Only use this if you can guarantee it will be in that form)
string yourUrl = [URL Listed];
//This will get everything after Stream (your song name)
string newString = yourUrl.split("Stream\/")[1];
//Replaces your %20s with spaces
newString = newString.replace("%20"," ");
URL songURL = new URL("yourpath/filename");
String filename = songURL.getFile();