+ 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....!!

19th Nov 2022, 8:07 AM
Davinder Kumar
Davinder Kumar - avatar
19 Answers
+ 4
Davinder Kumar You can solve your problem using StringBuilder like this https://code.sololearn.com/c7RnV97Lff7F/?ref=app
19th Nov 2022, 8:37 PM
A͢J
A͢J - avatar
+ 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..
19th Nov 2022, 8:17 AM
Jayakrishna 🇮🇳
+ 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> ) ;
19th Nov 2022, 8:55 AM
Jayakrishna 🇮🇳
+ 3
How can you? What is the possible answer you expect in general?
19th Nov 2022, 8:09 AM
Jayakrishna 🇮🇳
+ 3
Use split() string method maybe it's helpful to you https://www.geeksforgeeks.org/split-string-java-examples/amp/
19th Nov 2022, 8:14 AM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
+ 3
Davinder Kumar Actually you are not subtracting, you are removing each character of 2nd string from 1st string but from backward side.
19th Nov 2022, 9:10 AM
A͢J
A͢J - avatar
+ 2
Output should be "abcd", just by subtracting method..!!!
19th Nov 2022, 8:10 AM
Davinder Kumar
Davinder Kumar - avatar
+ 2
You can write a customized method for subtracting a string after you detailed specify how it works / means.
19th Nov 2022, 9:44 AM
JaScript
JaScript - avatar
+ 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
19th Nov 2022, 10:38 AM
Ipang
+ 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"
19th Nov 2022, 3:34 PM
A͢J
A͢J - avatar
+ 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
19th Nov 2022, 10:28 PM
Bob_Li
Bob_Li - avatar
+ 1
Sakshi what if i have different case "AGGXTXAYB" - "GTAB" = AGXXY
19th Nov 2022, 8:38 AM
Davinder Kumar
Davinder Kumar - avatar
+ 1
Jayakrishna🇮🇳 ohk i got it that java does only support for + operator. Can you please tell any alternative way except split method....!!
19th Nov 2022, 8:40 AM
Davinder Kumar
Davinder Kumar - avatar
+ 1
Sakshi it works only for specific strings. Anyway Thanks ..!
19th Nov 2022, 8:56 AM
Davinder Kumar
Davinder Kumar - avatar
+ 1
Jayakrishna🇮🇳 sir it doesn't follow always consecutive patterns. see this example. hope you get me now.!! "AGGXTXAYB" - "GTAB" = AGXXY
19th Nov 2022, 8:58 AM
Davinder Kumar
Davinder Kumar - avatar
+ 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...
19th Nov 2022, 9:47 AM
Jayakrishna 🇮🇳
+ 1
<DD> AB if we don't consider duplicates common...!
19th Nov 2022, 3:18 PM
Davinder Kumar
Davinder Kumar - avatar
+ 1
A͢J You are right sir but that isn't my question...!
19th Nov 2022, 3:21 PM
Davinder Kumar
Davinder Kumar - avatar
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?
19th Nov 2022, 8:54 AM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar