+ 4
why can't i subtract strings
//This work System.out.println("abcd" + "efgh"); //This doesn't System.out.println("abcdefgh" - "efgh"); let me know if anyone knows....!!
19 Answers
+ 4
Davinder Kumar
You can solve your problem using StringBuilder like this
https://code.sololearn.com/c7RnV97Lff7F/?ref=app
+ 11
Thats a special case.. Not in general arithmetic. What if "abcdefgh"-" efghi" ?
Addition happens because it does concatination.
Java has only one overload operator, that is plus (+) . It don't allow minus(-) operator on strings.. no any other arithmetic operations on strings..
+ 7
Davinder Kumar You mean , the result you want by "abcdefgh" - "efgh" is "acbd"?
You can use replace method of String class like
System.out.print( "abcdefgh".replace("efgh",""));
//replaces "efgh" with empty string..
// syntax :
<original string>. replace( <replce new_substring>, <old sub_string> ) ;
+ 3
How can you? What is the possible answer you expect in general?
+ 3
Use split() string method maybe it's helpful to you
https://www.geeksforgeeks.org/split-string-java-examples/amp/
+ 3
Davinder Kumar
Actually you are not subtracting, you are removing each character of 2nd string from 1st string but from backward side.
+ 2
Output should be "abcd", just by subtracting method..!!!
+ 2
You can write a customized method for subtracting a string after you detailed specify how it works / means.
+ 2
AFAIK to this date there's no programming language that allows arithmetic operation with strings. But you're not the first to come up with that doubt
https://code.sololearn.com/ceF3uBMqFjTo/?ref=app
+ 2
Davinder Kumar
I know your question but you cannot subtract string in any language only concatenation is possible so your requirement is different as I said you want to remove each character of 2nd string from 1st string in reverse order.
'A' - 'B' is possible because of subtraction of ASCII value but how you can expect "AB" - "A" = "B"
+ 2
Davinder Kumar
Subtracting strings is more complicated than simple concatenation. Plus it is not very useful in real applications. But it makes for very interesting puzzle.😊
A͢J
Your solution was very similar to what I was trying privately. 😁
I did not post it because I was encountering inconsistent results I could not resolve.
I tweaked your code to illustrate them.
So far, I have not come up with a method on how to solve them..🤔
https://code.sololearn.com/c52o0rClVsCN/?ref=app
+ 1
Sakshi what if i have different case
"AGGXTXAYB" - "GTAB" = AGXXY
+ 1
Jayakrishna🇮🇳 ohk i got it that java does only support for + operator. Can you please tell any alternative way except split method....!!
+ 1
Sakshi it works only for specific strings. Anyway Thanks ..!
+ 1
Jayakrishna🇮🇳 sir it doesn't follow always consecutive patterns.
see this example. hope you get me now.!!
"AGGXTXAYB" - "GTAB" = AGXXY
+ 1
Davinder Kumar Yes. You need find your own approach here.
Since , here you can use "regex" advancedly , for single character replace from a group of characters..
You need a loop for each single match replace. But see it replaces both "GG", not single "G" from your example , so you need stop condition. So i think entirely you need to define a method for this result. No predined ways available I think...
+ 1
<DD> AB if we don't consider duplicates common...!
+ 1
A͢J You are right sir but that isn't my question...!
0
Davinder Kumar see you can input the string and then use split () method, i already given the link now you can own make logic how to do it?