I have a string
ContactPerson.BusinessPartner.name1,ContactPerson.BusinessPartner.name2,ContactPerson.fullname
Here i need to break the string based on comma and i have done that
ContactPerson.BusinessPartner.name1
ContactPerson.BusinessPartner.name2
ContactPerson.fullname
But i need to tokenize this again from the end that is i need to extract name1 and should store it corresponding to Businesspartner.Same case for name 2.It should be stored corresponding to the Busineespartner. For fullname also i should extract fullname and store to corresponding contactperson.So what i need is i should split the string from backwards where i encounter the (.) first and should split the string into two and store the string corresponding to the String before. The example i gave is simple. Normally we get
Strings like
Contactperson.Customer.Company.Businesspartner.name1 etc
so name1 should be stored correspndingly to th businesspartner.
Can anybody help me how to do this any idea??
You can get the last parts like this:
String input = "ContactPerson.BusinessPartner.name1,ContactPerson.BusinessPartner.name2,ContactPerson.fullname";
String[] parts = input.split(",");
for (String part : parts) {
String[] subparts = part.split("\\.");
String last = subparts[subparts.length - 1];
}
last will then contain name1, name2, ...
See String.split() for details.
try
String s = "ContactPerson.BusinessPartner.name1";
String name = s.replaceAll(".+\\.([^.]+)", "$1");
Related
I have String like String str = "Abhishek Patel(123121)"; Nd I want Split String in two part.
String Name = "Abhishek Patel";
String ID = 123121;
i had tried like this in in java
String str = "Abhishek Patel(123121)";
String a[] = str.split("(");
String Name =a[0];
You can use a combination of split and substring
String name = "Abhishek Patel(1234567)";
String[] parts = name.split("\\(");
System.out.println(parts[0]);
System.out.println(parts[1].substring(0, parts[1].length() -1));
As #JoakimDanielson has correctly pointed out, if the last ) is optional then it maybe be better to use replace rather than substring
System.out.println(parts[1].replace(")", ""));
Take advantage of two facts.
The split method by default throws away any empty strings that appear after the matches.
You don't need to escape ( or ) if they appear in [] characters in a regular expression.
So you can just write this.
String toSplit = "Abishek Patel(12345)";
String[] parts = toSplit.split("[()]");
This gives an array of only two elements, not three, and they are the name and id.
Try this. will help you
String str = "Abhishek Patel(123121)";
String a[] = str.replace("(", " ").replace(")", " ").split(" ");
String Name =a[0];
String id =a[1];
System.out.println(Name);
System.out.println(id);
EDIT-------------
as suggested by Scary Wombat that there could be 2 spaces in the name it self. You can change this to something else.
The basic idea was to remove the unwanted and boundry characters with one common and split then.
Thanks #ScaryWombat.
Java- Extract part of a string between two similar special characters.
I want to substring the second number, example :
String str = '1-10-251';
I want the result to be: 10
String str = "1-10-251";
String[] strArray = str.split("-");
System.out.println(strArray[1]);
I have a mobile number with some junk characters. I have to check whether the number contains
the junk character [#EEE] in it, and if so, I should remove them:
String mobile = "999#EEE99999999";
String junktobechecked ="#EEE";
My output should be 99999999999.
Note: This junk character is not a constant value.I have to get it from db table and check.
replace
if(mobile.contains(junktobechecked))
{
mobile = mobile.replace(junktobechecked, "");
}
You can use String.replace - note that you don't really need to check if the string contains the junk string - replace would just do nothing in this case:
String mobile = "999#EEE99999999";
String junktobechecked ="#EEE";
mobile = mobile.replace(junktobechecked, "");
remove certain unwanted character. It might give you some idea on how to proceed.
String data = "12EEE34567890"; // imagine a large string loaded from a file
data.replace("EEE", "")
first code is for find anything from string
your_string.contains("find_value")
now in case if you want to remove
String arr [] = your_string.split("remove character");
String ans = "";
for(String t : arr)
ans+=t;
If i want to replace one string variable with exact string in java, what can I do?
I know that replace in java , replace one exact string with another, but now i have string variable and i want to replace it's content with another exact string.
for example:
`String str="abcd";
String rep="cd";`
Now I want to replace rep content with"kj"
It means that I want to have str="abkj" at last.
If I understand your question, you could use String.replace(CharSequence, CharSequence) like
String str="abcd";
String rep="cd";
String nv = "kj";
str = str.replace(rep, nv); // <-- old, new
System.out.println(str);
Output is (the requested)
abkj
i think he wants:
String toReplace = "REPLACE_ME";
"REPLACE_ME What a nice day!".replace(toReplace,"");
"REPLACEME What a nice day!".replace(toReplace,"") results in:
" What a nice day!"
I have the following String (it is variable, but classpath is always the same):
C:.Users.mho.Desktop.Eclipse.workspace.GIT.BLUBB...bin.de.test.class.mho.communication.InterfaceXmlHandler
and I want to get just
de.test.class.mho.communication.InterfaceXmlHandler
out of this string. The end
InterfaceXmlHandler
is variable, also the beginning before 'de' and the path itself is variable too, but
de.test.class.mho.
isn't variable.
Why not just use
String result = str.substring(str.lastIndexOf("de.test.class.mho."));
Instead of splitting you could get rid of the beginning of the string:
String input = "C:.Users.mho.Desktop.Eclipse.workspace.GIT.BLUBB...bin.de.test.class.mho.communication.InterfaceXmlHandler";
String output = input.replaceAll(".*(de\\.test\\.class\\.mho.*)", "$1");
You can create a string-array with String.split("de.test.class.mho."). The Array will contain two Strings, the second String will be what you want.
String longString = ""; //whatever
String[] urlArr = longString.split("de.test.class.mho.");
String result;
if(urlArr.length > 1) {
result = "de.test.class.mho." urlArr[1]; //de.test.class.mho.whatever.whatever.whatever
}
You can use replaceAll() to "extract" the part you want:
String part = str.replaceAll(".*(?=de\\.test\\.class\\.mho\\.)", "");
This uses a look-ahead to find all characters before the target, and replace them with a blank (ie delete them).
You could quite reasonably ignore escaping the dots for brevity:
String part = str.replaceAll(".*(?=de.test.class.mho.)", "");
I doubt it would give a different result.