String in Java can we convert "Hello" into Hello - java

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

Related

String Replace Based On Variable Value

I've the following string: {"array 1":[ ....
And I would like to remove everything preceding [.
For that I use: .replace("{\"array 1\":", ""); and that works well.
However, I've several arrays, so I'd like to do the replace based on a variable that holds the array name.
For example:
String arr_name = "array 1";
....replace('{\"arr_name\":", "");
Is it possible to use variable key to replace a string?
EDIT:
I've ended up adding another element to parse the array in JSON which removed its name.
Thank you all for the quick comments and suggestions.
you can do a string format, here is an example of that :
String arr_name = "array 1";
....replace(String.format ("{\"%s\"", arr_name), "");
Just use a substring, starting at the index of [.
String input = "{\"array 1\":[key:value...";
String result = input.substring(input.indexOf('['));
This gives [key.value...

How to delete , Null(""), from the end using Java

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(", "));

How to replace substrings that contain any integer in java?

How to replace a sub-string in java of form " :[number]: "
example:
string="Hello:6:World"
After replacement,
HelloWorld
ss="hello:909:world";
do as below:
String value = ss.replaceAll("[:]*[0-9]*[:]*","");
You can use a regex to define your desired pattern
String pattern = "(:\d+:)";
string EXAMPLE_TEST = ':12:'
System.out.println(EXAMPLE_TEST.replaceAll(pattern, "text to replace with"));
should work depending on what exactly you want to replace...
Do like this
String s = ":6:";
s = s.replaceAll(":", "");
Edit 1: After the question was changed, one should use
:\d+:
and within Java
:\\d+:
This is the answer for replacing :: as well.
This is the regexp you should use:
:\d*:
Debuggex Demo
And here is a running JavaCode snipped:
String str = "Hello :4: World";
String s = str.replaceAll(":\\d*:","");
System.out.println(s);
One problem with replaceAll is often, that the corrected String is returned. The string object from which replaceAll was called is not modified.

Replace String variable with exact string

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!"

Splitting string into substring in Java

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.

Categories