As the title of the question states: How can I remove , Null(""), from the String?
I have tried the following code, but it is not working:
String c = "customer_date, privacy_code, Null(""), ";
String nd = "Null(\"\")";
c = c.substring(0, c.lastIndexOf(nd));
If you want to remove it only from the end of the string, you can use String#replaceAll:
nd = nd.replaceAll("Null\\(\"\"\\),$", "");
Since it accepts a regex, I added the $ special character that matches the end of a string.
Please visit the String API to discover many useful methods that will help you.
Your code does not compile if you don't use escape character , any way you dont have to trouble your self with quotes you can only String nd = "Null"; instead like so
String c="customer_date, privacy_code, Null(\"\"), ";
String nd = "Null";
c=c.substring(0,c.lastIndexOf(nd));
System.out.println(c);
because a piece of string youre trying to remove starts with NULL anyway
I like using a sledgehammer to crack a nut!
String in = "customer_date, privacy_code, Null(\"\"), ";
String out = Arrays.asList(in.split(",")).stream()
.map(String::trim)
.filter(s -> !s.equals("Null(\"\")"))
.collect(Collectors.joining(", "));
Related
Hello all I an new to java I just want to know that can we convert "Hello" in Hello. I have gone through the internet answers but found that if any string has "" in that so we can use the replace method of java. So I just want to convert the "Hello" into Hello. So if you know please help
suppose
String s="Hello"
//Required Operation
System.out.println(s);//It should print Hello.
So if you know please help me. Actually I have a file which contains lots of data having " " and I only want that data without double quotes so is it possible to convert that.
Here is an example:
String s = "\"hello\"";
String result = s.replaceAll("\"", "");
System.out.println(result);
Actually if you declare your string String s="Hello", the variable s will not contain any quotes, because the quotes are Java syntax and mark the start and end of the String.
Use String.replaceAll()
str = str.replaceAll("\"", "");
As all the other answers you're able to use:
str = str.replaceAll("\"", "");
But their is another solution if you just want to erase the 1st and last char of your string ( so your " here) is to use substring like:
str = "Hello";
str = str.substring(0,str.length()-2);
I think that it could work for you
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 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.
I have to remove a particular token from a String variable.
for eg:
If the String variable is like "GUID+456709876790" I need to remove the "GUID+" part from the string and only need "456709876790".
How can it be done?
Two options:
As you're just removing from the start, you can really easily use substring:
text = text.substring(5);
// Or possibly more clearly...
text = text.substring("GUID+".length());
To remove it everywhere in the string, just use replace:
text = text.replace("GUID+", "");
Note the use of String.replace() in the latter case, rather than String.replaceAll() - the latter uses regular expressions, which would affect the meaning of +.
String s = "GUID+456709876790";
String token = "GUID+";
s = s.substring(s.indexOf(token) + token.length());
// or s = s.replace(token, "");
If you're using apache.commons.lang library you can use StringUtils just do:
StringUtils.remove(yourString, token);
String str = "GUID+456709876790"
str.substring(str.indexOf("+")+1)
Just try this one :
String a = "GUID+456709876790";
String s = a.replaceAll("\\D","");
I am assuming that you want only digits as I have used regex here to remove any thing that is not a digit
this works for me
String Line="test line 1234 abdc",aux;
token=new StringTokenizer(Line);
while(token.hasMoreTokens())
if(!("1234").equals(aux=token.nextToken())){
new_line+= aux+" ";
System.out.println("la nueva string es: "+ new_line);
}
This question already has answers here:
How can I check if a single character appears in a string?
(16 answers)
Closed 6 years ago.
I want to check if my string contains a + character.I tried following code
s= "ddjdjdj+kfkfkf";
if(s.contains ("\\+"){
String parts[] = s.split("\\+);
s= parts[0]; // i want to strip part after +
}
but it doesnot give expected result.Any idea?
You need this instead:
if(s.contains("+"))
contains() method of String class does not take regular expression as a parameter, it takes normal text.
EDIT:
String s = "ddjdjdj+kfkfkf";
if(s.contains("+"))
{
String parts[] = s.split("\\+");
System.out.print(parts[0]);
}
OUTPUT:
ddjdjdj
Why not just:
int plusIndex = s.indexOf("+");
if (plusIndex != -1) {
String before = s.substring(0, plusIndex);
// Use before
}
It's not really clear why your original version didn't work, but then you didn't say what actually happened. If you want to split not using regular expressions, I'd personally use Guava:
Iterable<String> bits = Splitter.on('+').split(s);
String firstPart = Iterables.getFirst(bits, "");
If you're going to use split (either the built-in version or Guava) you don't need to check whether it contains + first - if it doesn't there'll only be one result anyway. Obviously there's a question of efficiency, but it's simpler code:
// Calling split unconditionally
String[] parts = s.split("\\+");
s = parts[0];
Note that writing String[] parts is preferred over String parts[] - it's much more idiomatic Java code.
[+]is simpler
String s = "ddjdjdj+kfkfkf";
if(s.contains ("+"))
{
String parts[] = s.split("[+]");
s = parts[0]; // i want to strip part after +
}
System.out.println(s);