简体   繁体   中英

Replacing certain characters in java

I have a SQL query passed to me in a text, here is a part of it:

WHERE (t.STIME > CAST(SYSTIMESTAMP AS TIMESTAMP) + NUMTODSINTERVAL(-86400000 * 0.001, 'SECOND')) ) t ORDER BY

m_2 ASC, m_1 ASC, t.STIME ASC

What I want to do, is to simply modify it in a way that I will have:

WHERE (t.STIME > CAST(SYSTIMESTAMP AS TIMESTAMP) + NUMTODSINTERVAL(-86400000 * 0.001, 'SECOND')) ORDER BY

m_2 ASC, m_1 ASC, t.STIME ASC )

So, I need to somehow remove: ") t" from within the String, but how to do that? I always receive "unmatched ')' parenthesis and I really don't know why x/

Here is a method I wrote for replacing this string:

 public static String replaceLast(String string, String toReplace, String replaceWith) {
        int last = string.lastIndexOf(toReplace);
        if (last < 0) return string;
        String ending = string.substring(last).replaceFirst(toReplace, replaceWith);
        return string.substring(0, last) + tail;
   }

then I'm trying to use it this way:

 if(sqlToTrim.lastIndexOf("\\) t") > 0){
                replaceLast(sqlToTrim, "\\) t ", " ");
                addLastParanthesis(sqlToTrim);
            }

But it is not replaced, basicly nothing changes - replaceLast is never used. I assume I messed up with regex, I searched through stack overflow but it seems \\ is the right combination that I should put before ). Thanks in advance for your help. Could anyone tell me what am I doing wrong?

If you need to know, why do I do that, why in this way - legacy code...

Strings are immutable. replaceLast(sqlToTrim, "\\\\) t ", " "); returns a new string. Do:

sqlToTrim = replaceLast(sqlToTrim, "\\) t ", " ");

and continue on from there. If addLastParenthesis modifies the string it should return it, ie

sqlToTrim = replaceLast(sqlToTrim, "\\) t ", " ");
sqlToTrim = addLastparenthesis(sqlToTrim);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM