Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

remove repeated operators (+-/*)

 
anoop kerala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all.. how to remove the duplicate operators in a string?
for eg: a++b should give a+b
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many operators do you have ?
 
anoop kerala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the expression should be for example a=(a+b)-c and if user mistakenly give a=(a++b)--c we have to get the correct expression
a=(a+B)-c

thank you..
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about..




There might be a super simple regex though ...
 
anoop kerala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot.. let me try this.. this is my first post and amazed by your fast reply..

will let you know once i try this..

regards,
lenin
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could improve this example by making the operator array a parameter. This way you explicitly define what operators should be removed.
 
anoop kerala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes.. your snippet addressed my req..

thanks..
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Janisch wrote:how about..




There might be a super simple regex though ...

Your replace option may not be the most efficient. You loop several times over the StringBuilder; indexOf starts from the start each time. You then do this for all 4 operators. You can improve performance by iterating over the StringBuilder once, deleting characters as needed:
 
Campbell Ritchie
Sheriff
Pie
Posts: 49754
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Carey Brown
Ranch Hand
Pie
Posts: 1634
22
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want the method to coalesce any number of multiple operators (e.g. "+++" --> "+") then you need to put an 'else' in front of the index increment.
 
Henry Wong
author
Marshal
Pie
Posts: 21386
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Janisch wrote:
There might be a super simple regex though ...



How about....



Henry
 
anoop kerala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good morning all..
thanks for your help..

the regex by wong is very nice i will give some eg:,

1) a++b output -> a+b

2)a+++b output -> a+b

but

3) a+-b output -> a+-b , but what i want is a+b

4)a++-b output -> a+-b, but what i want is a+b itself.

regards,
lenin





 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You only asked about removing repeated operators. +- is not a repeated operator. Moreover, a+-b is (mathematically) not the same as a+b -- it is the same as a-b. That's an essential difference.
 
anoop kerala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i said repeated operators..

but i gave eg in that way, that was my mistake..

let me explain,

1) a+*b output is a+*b , but the operator is getting repeated.. i think you got what i mean..

and hope you will help me..

lenin
 
Henry Wong
author
Marshal
Pie
Posts: 21386
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anoop Lenin wrote:and hope you will help me..


Your new requirement is actually *easier* than the previous one -- somewhat. Why don't you attempt to do it yourself? You can't learn if you don't try.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic