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.
Related
I am a newbie in regex, I want to extract values between commas but I don't know how.
I have values like this :
[1000, Value_to_extract, 1150370.5]
and I used this Technic to simplify it:
String val = "[1000, Value_to_extract, 1150370.5]";
String designation=val.replace("[", "").replace("]", "").trim();
It give's me this result :
1000, Value_to_extract, 1150370.5
I don't know how to extract only Value_to_extract
I tried : String designation=val.replace("[", "").replace("]", "").replaceAll(".*, ,.*", "").trim();
but i doesn't work .
Thank you for your help.
String input = "[1000, Value_to_extract, 1150370.5]";
String[] parts = input.replaceAll("\\[\\] ", "") // strip brackets and whitespace
.split(","); // split on comma into an array
String valueToExtract = parts[1]; // grab the second entry
Notes:
You might also be able to use a regex here, q.v. the answer by #Thomas, but a regex will become unwieldy for extracting values from a CSV string of arbitrary length. So in general, I would prefer splitting here to using a regex.
someting like this:
,[ ]?([0-9]+[.]?[0-9]+),
breakdown
, // literal ,
[ ]? // 0 or 1 spaces
([0-9]+[.]?[0-9]+) // capture a number with or without a dot
, // another litteral ,
https://regex101.com/r/oR7nI8/1
Here are some options:
String val = "[1000, Value_to_extract, 1150370.5]";
//you can remove white space by
String noSpaces = val.trim();
System.out.println(noSpaces);
//you can split the string into string[] settting
//the delimiting regular expression to ", "
String[] strings = noSpaces.split(", ");
//the strings[1] will hold the desired string
System.out.println(strings[1]);
//in the private case of val, only Value_to_extract contains letters and "_" ,
//so you can also extract it using
System.out.println(val.replaceAll("[^a-zA-Z_]", ""));
If val does not well represent the more general need, you need to define the need more precisely.
I have following string
String str = "url:http://www.google.com"
Now I want to split the above string using :.
If I split above string using : then above string split into 3 segments.
But I want whole URL in one segment. How can I get the whole URL?
Three is an one way that I found using substring
String webURL = str.substring(4, str.length());
Is there any other best way to that?
You can call String.split(String, int) where the second argument is a limit (or count). Something like,
String str = "url:http://www.google.com";
String[] arr = str.split(":", 2);
System.out.println(arr[1]);
Output is (as requested)
http://www.google.com
String str= "url:http://www.google.com";
// find the first : and take string beyond that
str = str.substring(str.indexOf(':')+1);
System.out.println(str);
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 one string and I want to split it into substring in Java, originally the string is like this
Node( <http://www.mooney.net/geo#wisconsin> )
Now I want to split it into substring by (#), and this is my code for doing it
String[] split = row.split("#");
String word = split[1].trim().substring(0, (split[1].length() -1));
Now this code is working but it gives me
"wisconsin>"
the last work what I want is just the work "wisconsin" without ">" this sign, if someone have an idea please help me, thanks in advance.
Java1.7 DOC for String class
Actually it gives you output as "wisconsin> " (include space)
Make subString() as
String word = split[1].trim().substring(0, (split[1].length()-3));
Then you will get output as
wisconsin
Tutorials Point String subString() method reference
Consider
String split[] = row.split("#|<|>");
which delivers a String array like this,
{"http://www.mooney.net/geo", "wisconsin"}
Get the last element, at index split.length()-1.
String string = "Enter parts here";
String[] parts = string.split("-");
String part1 = parts[0];
String part2 = parts[1];
you can just split like you did before once more (with > instead of #) and use the element [0] istead of [1]
You can just use replace like.
word.replace(char oldChar, char newChar)
Hope that helps
You can use Java String Class's subString() method.
Refer to this link.
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");