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.
Related
Input string is "ABC\1067546161"
I want to remove "\" backslash and get digits only from the string but we are getting string with ascii value.Result String is ABCF7546161 after print.
Please suggest some solution.
Input String is ABC\1067546161
Expected result is 1067546161
May be something like this
"ABC\1067546161".replaceAll("[a-zA-Z\\]", "")
I think this could work, but the code is ugly as hell..
String word = "ABC\1067546161";
char badChar = word.charAt(3);
String[] arr = word.split(Character.toString(badChar));
System.out.println(Integer.toOctalString(badChar) + arr[1]);
You only mentioned one string in the question, but on several cases, this would most likely not work.
As #TheLostMind pointed out in a comment, you can't replace the backslash directly because the String is created with that value.
The only way to do that is manipulate the input itself and convert it into a byte array instead of a String. Then you can call the String constructor that takes a byte[] as argument and it won't be converted.
Once you have that, you can use a regex to remove the part you don't want as others suggested. Here's the code I've used to test this:
public static void main(String[] args) {
// Input manipulation.
byte[] input = {'A','B','C','\\','1','0','6','7','5','4','6','1','6','1'};
String string = new String(input);
System.out.println(string);
// Splitting.
String[] result = string.split("\\\\");
System.out.println(result[1]);
}
I have one string which i need to divide into two parts using regex
String string = "2pbhk";
This string i need to divide into 2p and bhk
More over second part should always be bhk or rk, as strings can be one of 1bhk, 5pbhk etc
I have tried
String pattern = ([^-])([\\D]*);
You can use the following regex "(?=bhk|rk)" with split.
str.split("(?=bhk|rk)");
This will split it if there is one of bhk or rk.
This should do the trick:
(.*)(bhk|rk)
First capture holds the "number" part, and the second bhk OR rk.
Regards
String string = "2pbhk";
String first_part, second_part = null;
if(string.contains("bhk")){
first_part = string.substring(0, string.indexOf("bhk"));
second_part = "bhk";
}
else if(string.contains("rk")){
first_part = string.substring(0, string.indexOf("rk"));
second_part = "rk";
}
Try the above once, not using regex but should work.
In case you are looking to split strings that end with rk or bhk but not necessarily at the end of the string (i.e. at the word boundaries), you need to use a regex with \\b:
String[] arr = "5ddddddpbhk".split("(?=(?:rk|bhk)\\b)");
System.out.println(Arrays.toString(arr));
If you want to allow splitting inside a longer string, remove the \\b.
If you only split individual words, use $ instead of \\b (i.e. end of string):
(?=(?:rk|bhk)$)
Here is my IDEONE demo
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!"
I have string like this String s="ram123",d="ram varma656887"
I want string like ram and ram varma so how to seperate string from combined string
I am trying using regex but it is not working
PersonName.setText(cursor.getString(cursor.getColumnIndex(cursor
.getColumnName(1))).replaceAll("[^0-9]+"));
The correct RegEx for selecting all numbers would be just [0-9], you can skip the +, since you use replaceAll.
However, your usage of replaceAll is wrong, it's defined as follows: replaceAll(String regex, String replacement). The correct code in your example would be: replaceAll("[0-9]", "").
You can use the following regex: \d for representing numbers. In the regex that you use, you have a ^ which will check for any characters other than the charset 0-9
String s="ram123";
System.out.println(s);
/* You don't need the + because you are using the replaceAll method */
s = s.replaceAll("\\d", ""); // or you can also use [0-9]
System.out.println(s);
To remove the numbers, following code will do the trick.
stringname.replaceAll("[0-9]","");
Please do as follows
String name = "ram varma656887";
name = name.replaceAll("[0-9]","");
System.out.println(name);//ram varma
alternatively you can do as
String name = "ram varma656887";
name = name.replaceAll("\\d","");
System.out.println(name);//ram varma
also something like given will work for you
String given = "ram varma656887";
String[] arr = given.split("\\d");
String data = new String();
for(String x : arr){
data = data+x;
}
System.out.println(data);//ram varma
i think you missed the second argument of replace all. You need to put a empty string as argument 2 instead of actually leaving it empty.
try
replaceAll(<your regexp>,"")
you can use Java - String replaceAll() Method.
This method replaces each substring of this string that matches the given regular expression with the given replacement.
Here is the syntax of this method:
public String replaceAll(String regex, String replacement)
Here is the detail of parameters:
regex -- the regular expression to which this string is to be matched.
replacement -- the string which would replace found expression.
Return Value:
This method returns the resulting String.
for your question use this
String s = "ram123", d = "ram varma656887";
System.out.println("s" + s.replaceAll("[0-9]", ""));
System.out.println("d" + d.replaceAll("[0-9]", ""));