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.
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.
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);
I have a string like "test.test.test"...".test" and i need to access last "test" word in this string. Note that the number of "test" in the string is unlimited. if java had a method like php explode function, everything was right, but... . I think splitting from end of string, can solve my problem.
Is there any way to specify direction for split method?
I know one solution for this problem can be like this:
String parts[] = fileName.split(".");
//for all parts, while a parts contain "." character, split a part...
but i think this bad solution.
Try substring with lastIndexOf method of String:
String str = "almas.test.tst";
System.out.println(str.substring(str.lastIndexOf(".") + 1));
Output:
tst
I think you can use lastIndexOf(String str) method for this purpose.
String str = "test.test.test....test";
int pos = str.lastIndexOf("test");
String result = str.substring(pos);
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!"
Hi I want to split a string as only two parts. i.e. I want to split this string only once.
EX: String-----> hai,Bye,Go,Run
I want to split the above string with comma(,) as two parts only
i.e
String1 ---> hai
String2 ---->Bye,Go,Run
Please help me how can I do it.
Use String.split(String regex, int limit) method:
String[] result = string.split(",", 2);
String[] result = string.split("\\s*,\\s*" ,2);
This is a very basic Java knowledge...
Have a look at String class definition before asking here:
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html
You should follow some Java tutorial before starting programming in Java.
if you check out the Java Doc of string
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html
You'll find one of the methods is
split(String regex)
then what you want is to use a regex like "," to get a table of strings
String str = "hai,Bye,Go,Run";
String str1 = str.substring(0, str.indexOf(','));
String str2 = str.substring(str.indexOf(',')+1);
You can use the String method:
public String[] split(String regex, int limit)
e.g. (not tested)
String str = "hai,Bye,Go,Run"
str.split(",", 2);
String str="hai,Bye,Go,Run";
//String 1
String str1=str.substring(0,str.indexOf(','));
//String 1
String str1=str.substring(str.indexOf(',')+1,str.length);
done :)