Detect special character from a String

Hello guys,
Some of the files in unix box contain special characters . I need to strip these special character in the filename to underscore. however, I do not want to strip off special character like _ - ^ . @
Pattern escaper = Pattern.compile("([^a-zA-z0-9_-^.@])");
I couldn't strip off the caret. Appreciate any advice please. Thanks in advance!
Cheers,
Mark

Hi Alice,
I tried your suggestion:
     public static void main(String[] args) throws IOException {
          rename(args[0], args[0].replaceAll("[^a-zA-Z0-9\\\\/:@._^-]","_"));
     public static void rename(String from, String to) throws IOException{
          // File (or directory) with old name
          File file1 = new File(from);
          // File (or directory) with new name
          File file2 = new File(to);
          // Rename file (or directory)
          boolean success = file1.renameTo(file2);
          if (!success) { // File was not successfully renamed
               System.out.println("failed to rename : " + file1);
C:\workspace\TestOnly\bin>java -cp . com.test.TestClass C:\Apps\mud\1~2.txt
C:\workspace\TestOnly\bin>java -cp . com.test.TestClass C:\Apps\mud\1^2.txt
failed to rename : C:\Apps\mud\12.txt
C:\workspace\TestOnly\bin>java -cp . com.test.TestClass C:\Apps\mud\[email protected]
C:\workspace\TestOnly\bin>java -cp . com.test.TestClass C:\Apps\mud\1@2$-_3mn.txt
C:\workspace\TestOnly\bin>java -cp . com.test.TestClass C:\Apps\mud\1@2$-_3mn.txt
failed to rename : C:\Apps\mud\1@2$-_3mn.txt
C:\workspace\TestOnly\bin>java -cp . com.test.TestClass C:\Apps\mud\1@2$-_3m'n.txt
C:\workspace\TestOnly\bin>java -cp . com.test.TestClass C:\Apps\mud\1@2$-_3m'^n.txt
failed to rename : C:\Apps\mud\1@2$-_3m'n.txt
C:\workspace\TestOnly\bin>java -cp . com.test.TestClass C:\Apps\mud\1@2$-_3m'^n.txtbut however, whenever I put in a caret in the filename, it always failed to rename. Appreciate your further advice please. thanks in advance!
Cheers,
Mark

Similar Messages

  • Counting a Special Character from a String

    Hello,
    I have a String let say 'ABC-X', 'ABC-X-Y' ....
    so how can we find the count/No. of Hypen('-') .. in a string....

    I have a String let say 'ABC-X', 'ABC-X-Y' ....
    so how can we find the count/No. of Hypen('-') .. in a string....
    SQL> var str varchar2(20);
    SQL> exec :str := 'ABC-X-Y';
    PL/SQL procedure successfully completed.
    SQL> select length(:str) - nvl(length(replace(:str,'-')),0) from dual;
    LENGTH(:STR)-NVL(LENGTH(REPLACE(:STR,'-')),0)
                                                2
    SQL>

  • Replacing a special character in a string with another string

    Hi
    I need to replace a special character in a string with another string.
    Say there is a string -  "abc's def's are alphabets"
    and i need to replace all the ' (apostrophe) with &apos& ..which should look like as below
    "abc&apos&s def&apos&s are alphabets" .
    Kindly let me know how this requirement can be met.
    Regards
    Sukumari

    REPLACE
    Syntax Forms
    Pattern-based replacement
    1. REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF]
    pattern
              IN [section_of] dobj WITH new
              [IN {BYTE|CHARACTER} MODE]
              [{RESPECTING|IGNORING} CASE]
              [REPLACEMENT COUNT rcnt]
              { {[REPLACEMENT OFFSET roff]
                 [REPLACEMENT LENGTH rlen]}
              | [RESULTS result_tab|result_wa] }.
    Position-based replacement
    2. REPLACE SECTION [OFFSET off] [LENGTH len] OF dobj WITH new
                      [IN {BYTE|CHARACTER} MODE].
    Effect
    This statement replaces characters or bytes of the variable dobj by characters or bytes of the data object new. Here, position-based and pattern-based replacement are possible.
    When the replacement is executed, an interim result without a length limit is implicitly generated and the interim result is transferred to the data object dobj. If the length of the interim result is longer than the length of dobj, the data is cut off on the right in the case of data objects of fixed length. If the length of the interim result is shorter than the length of dobj, data objects of fixed length are filled to the right with blanks or hexadecimal zeroes. Data objects of variable length are adjusted. If data is cut off to the right when the interim result is assigned, sy-subrc is set to 2.
    In the case of character string processing, the closing spaces are taken into account for data objects dobj of fixed length; they are not taken into account in the case of new.
    System fields
    sy-subrc Meaning
    0 The specified section or subsequence was replaced by the content of new and the result is available in full in dobj.
    2 The specified section or subsequence was replaced in dobj by the contents of new and the result of the replacement was cut off to the right.
    4 The subsequence in sub_string was not found in dobj in the pattern-based search.
    8 The data objects sub_string and new contain double-byte characters that cannot be interpreted.
    Note
    These forms of the statement REPLACE replace the following obsolete form:
    REPLACE sub_string WITH
    Syntax
    REPLACE sub_string WITH new INTO dobj
            [IN {BYTE|CHARACTER} MODE]
            [LENGTH len].
    Extras:
    1. ... IN {BYTE|CHARACTER} MODE
    2. ... LENGTH len
    Effect
    This statement searches through a byte string or character string dobj for the subsequence specified in sub_string and replaces the first byte or character string in dobj that matches sub_string with the contents of the data object new.
    The memory areas of sub_string and new must not overlap, otherwise the result is undefined. If sub_string is an empty string, the point before the first character or byte of the search area is found and the content of new is inserted before the first character.
    During character string processing, the closing blank is considered for data objects dobj, sub_string and new of type c, d, n or t.
    System Fields
    sy-subrc Meaning
    0 The subsequence in sub_string was replaced in the target field dobj with the content of new.
    4 The subsequence in sub_string could not be replaced in the target field dobj with the contents of new.
    Note
    This variant of the statement REPLACE will be replaced, beginning with Release 6.10, with a new variant.
    Addition 1
    ... IN {BYTE|CHARACTER} MODE
    Effect
    The optional addition IN {BYTE|CHARACTER} MODE determines whether byte or character string processing will be executed. If the addition is not specified, character string processing is executed. Depending on the processing type, the data objects sub_string, new, and dobj must be byte or character type.
    Addition 2
    ... LENGTH len
    Effect
    If the addition LENGTH is not specified, all the data objects involved are evaluated in their entire length. If the addition LENGTH is specified, only the first len bytes or characters of sub_string are used for the search. For len, a data object of the type i is expected.
    If the length of the interim result is longer than the length of dobj, data objects of fixed length will be cut off to the right. If the length of the interim result is shorter than the length of dobj, data objects of fixed length are filled to the right with blanks or with hexadecimal 0. Data objects of variable length are adapted.
    Example
    After the replacements, text1 contains the complete content "I should know that you know", while text2 has the cut-off content "I should know that".
    DATA:   text1      TYPE string       VALUE 'I know you know',
            text2(18)  TYPE c LENGTH 18  VALUE 'I know you know',
            sub_string TYPE string       VALUE 'know',
            new        TYPE string       VALUE 'should know that'.
    REPLACE sub_string WITH new INTO text1.
    REPLACE sub_string WITH new INTO text2.

  • Strip the first character from a string

    i am trying to strip the leftmost character from a string
    using the following:
    <cfset Charges.DTL_CHG_AMT_EDIT =
    #Right(Charges.DTL_CHG_AMT_EDIT,Len(Charges.DTL_CHG_AMT_EDIT)-1)#>
    i keep getting the following error:
    Parameter 2 of function Right which is now -1 must be a
    positive integer

    > RemoveChars() much easier than Right()? How so?
    Semantically, if the object of the exercise is to *REMOVE
    CHARacters from a
    string* (which it is, in this case), it is simply better
    coding to use
    removeChars() rather than right(). That, and it's one less
    function call
    (the RIGHT() solution also requires a call to LEN() for it to
    work).
    So removeChars() is "easier" because it reflects the intent
    of the exercise
    directly, is simpler logic, is easier to read, and - I can't
    be arsed
    counting keystrokes - is probably less typing.
    That'd be how.
    Adam

  • Replace a character from a string

    Hi,
    how can I replace a particular character from a string on a particular occurrence? Say for eg, if i wan to replace 'a' of second occurrence from 'abcabcabc', then what should i do? I guess the normal REPLACE function replaces every occurrence in a given string. So is there any other way to solve this?
    Thanks!

    If you are in 9i:
    Then you got to split the string into based on the nth position.
    SQL> select replace('abddefabc','a','x') from dual;
    REPLACE('
    xbddefxbc
    SQL> select instr('abddefabc','a',2) from dual;
    INSTR('ABDDEFABC','A',2)
                           7
    SQL> select substr('abddefabc',1,instr('abddefabc','a',2)-1),substr('abddefabc',instr('abddefabc','a',2)) from dual;
    SUBSTR SUB
    abddef abc
    SQL> select substr('abddefabc',1,instr('abddefabc','a',2)-1),replace(substr('abddefabc',instr('abddefabc','a',2)),'a','X') as replaced from dual;
    SUBSTR REP
    abddef XbcOfcourse, later on you can join the string..
    Jithendra

  • How to insert special character from Oracle form builder 10g

    Dear all,
    I need help. how to insert special character like 'Superscript or Subscript ' from oracle form builder 10g. I had try in Oracle form builder 6i with press ALT+ASCII code in the text item and it work, but in the oracle form builder 10g this method doesn't work... would you like to help me...somebody please...
    Best Regard,
    Dedy P.T.

    What do you mean by insert ... from Forms Builder? Do you mean you want to add it as text in a string of pl/sql code or as part of boiler plate text (label) or a value on the Property Palette?
    For special characters you would need set NLS_LANG to something that would support the characters you want to use. For the Builder to see the change, you would need to set NLS_LANG to something like:
    NLS_LANG=AMERICAN_AMERICA.UTF8
    This can be done in the Windows Registry or system. As I mentioned, this will only apply to the Builder and will have nothing to do with a running form. For running forms you would need to set this in default.env. As for things like super and sub scripts, these are font formats and not necessarily characters. For the most part, I don't believe these are supported in Forms.

  • Special character '  in the string variable

    Hi,
    I need to insert in the string veriable the special character '. How should I do it ?
    variable = '''.
    It does not work ...
    BR
    Wojcieh

    1)Declare a variable and assign the special character to that variable.
    2)Then use the CONCATENATE option to get the value in the string variable.
    Just copy and execute this code.
    REPORT zztest.
    <b>DATA : temp(1) VALUE ''''.</b> Decalare your special character
    DATA : variable TYPE string.
    START-OF-SELECTION.
      BREAK-POINT.
      variable = 'SDNSAP'.
      CONCATENATE temp variable temp INTO variable.
      WRITE : / variable.
    <b>The output is</b>
    'SDNSAP'
    Regards,
    AS.

  • How to discard the special character from field-Table.

    Hi
    In my data i am having a lot of SPECIAL CHARACTER also involved, I want to display only the consumer name (PURELY), is there any method/function module to discard the special character and display only the A-Z character as stored in DB.
    Gaurav

    if itab-field CA 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    or
    if itab-field CP 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
    then do ur else calculation.
    else.
    delete itab.
    Edited by: tahir naqqash on Feb 17, 2009 4:27 PM
    Edited by: tahir naqqash on Feb 17, 2009 5:21 PM

  • Picking character from a string

    Hi guys
    How to pick each character of a string containing few character without using a loop. Like "LabVIEW" will become a array of string "L","a","b" etc
    Thanks in advance
    Niladri

    Why would you not want to use a loop?
    You could use String Subset in a loop to get each byte.
    You could alsoe use String to U8 array to get an array of bytes.  Then convert that back to individual string characters by using the typecast function in a loop with autoindexing turned on.
    Attachments:
    Example_VI_BD.png ‏10 KB

  • Reading a string containing special character from a result set

    My Code....
    PrintStream p = new PrintStream(fout);
    if (connection == null)
    connection = getConnection();
    CallableStatement proc = connection.prepareCall("{call testing_read()}");
    rs = proc.executeQuery();
    while ( rs.next() )
    String page2 = rs.getString(1);
    System.out.println(page2);
    rs.getString(1); is a string value eg. 'Rebekah \n Govender' that is being set in a stored procedure.
    The output of my program is :'Rebekah \n Govender' , i need for it to read the special characters, and produce this output :
    Rebekah
    Govender
    Without using a result set , it works fine.
    eg.
    String page2 = 'Rebekah \n Govender'
    System.out.println(page2);
    Will give an output of :
    Rebekah
    Govender
    I need to read values from the results set, pls help me someone....
    Please help....

    String page2 = rs.getString(1).replace("\\n", "\n"); When you include source code in your posts, please enclose it in CODE tags. There's a button for that above the input textarea.
    ThE-MaRaC wrote:
    Hi,
    you can try something like this:
    import java.io.UnsupportedEncodingException;;
    *  Main class, manages the complete game
    public class Main {
    public static void main(String[] args) {
    String page2 = "Rebekah \n Govender";
    try {
    page2 = new String(page2.getBytes("ISO-8859-1"), "ETF-8");
    } catch (UnsupportedEncodingException e) {}
    System.out.println(page2);
    }Regards,
    Hercog MarioThat is a disgusting hack which you shouldn't be recommending to anyone, and it's totally irrelevant here anyway. Also, as written, it does absolutely nothing because you misspelled "UTF-8" and you swallowed the exception that would have told you so.

  • Problem to put a special character in a string

    I read a file that contains some special characters.
    One of them has a hexadecimal value '8D'.
    When i put this in a string or a char, it give me '?'.
    If i read my file int by int, this char value is 65533
    How can I do to display this character as it must be? How can i transform it to put it in a string?

    8D is "reverse linefeed" in unicode: http://www.unicode.org/charts/PDF/U0080.pdf
    Obviously it's something else in your system's character encoding. What do you think you should get from 8D?

  • How to select special character from OLE DB source in SSIS?- Truncation error.

    Hi,
    I have to pull data from one Oracle table and for that I am using OLE DB source in SSIS. In OLE DB source I write query like, 
    SELECT     DESCRIPTION
    FROM         INV.ITEMS_LOCATION
    and packages failed with truncation error. When I redirect this column for truncation I see below result in data viewer,
    ErrorCode
    ErrorColumn
    DESCRIPTION
    ErrorCode - Description
    -1071607693
    8884
    HC Afd Set__ZDV?N07-03075
    The data was truncated.  
    -1071607693
    8884
    HC Afd Set__Ventiel 4DO1-?
    The data was truncated.  
    -1071607693
    8884
    HC Afd Set__Ventiel 3DO3-?
    The data was truncated.  
    -1071607693
    8884
    HC Afd Set__Ventiel 4DP01-?
    The data was truncated.  
    -1071607693
    8884
    HC Afd Set__Ventiel DBDS-10-?
    The data was truncated.  
    -1071607693
    8884
    HC Afd Set__Filter TXW?
    The data was truncated.  
    -1071607693
    8884
    HC Afd Set__ZDV?N07-03075
    The data was truncated.  
    -1071607693
    8884
    HC Afd Set__Ventiel 4DO1-?
    The data was truncated.  
    -1071607693
    8884
    HC Afd Set__Ventiel 3DO3-?
    The data was truncated.  
    -1071607693
    8884
    HC Afd Set__Ventiel 4DP01-?
    The data was truncated.  
    -1071607693
    8884
    HC Afd Set__Ventiel DBDS-10-?
    The data was truncated.  
    I don't how to put this value in table. I use code page 65001(UTF-8) and 1252, but it is not working, and giving error like 
    [OLE DB Source [16]] Error: Column "DESCRIPTION" cannot convert between unicode and non-unicode string data types.
    Data Type of 'DESCRIPTION' source(External) column is varchar(240) and same- varchar(240) I used for output column and in my table's data type. I also tried with varchar(1000) for output column in OLE DB Source but same truncation error.
    I also tried to convert DESCRIPTION into navachar using below cast function but got error.
    SELECT   -- TO_NVARCHAR(DESCRIPTION) AS DESCRIPTION,
    --CAST(DESCRIPTION AS NVARCHAR(240)) DESCRIPTION, 
    Please let me know how to load this value in table. Thank you in advance.
    Vicky

    Thanks ArthurZ for reply.
    But for testing I am just using OLEDB source and OLE DB destination only in my ssis package.
    For Column "DESCRIPTION", data type is VARCHAR(240)
    in external column, output column and target table column.
    And I tried to cast source column into nvarchar and also set target column to nvarchar to handle this kind of special characters but package is failing,
    I also tried TO_NCHAR(DESCRIPTION)
    in my source query to convert it to nvarchar but error is something like character mismatch.
    Vicky

  • How to get specific character from a string

    I have a value such as: 0-5 or 123-30. This is a combination
    from 2 different IDs.
    All I need is to get the number on the right (5 from 0-5 OR
    30 from 123), any number on the right of the "-" character not
    including
    the "-"
    I'm not sure what ColdFusion function can I use safely.
    I said safely since all I can find is either Left or Right
    functions. With these 2 functions, I need to specify the number of
    character to extract while in my case the application is growing so
    the ID may currently be only 1 digit each (0-5) but later it may
    grow longer (123-5677).
    If anyone know the function, please help me. Thank you!

    Something like this?
    Mid(string, Find("-",string)+1 ,
    Len(string)-Find("-",string))
    Since
    Mid(string, start, count)
    and
    Len(string or binary object)
    and
    Find(substring, string [, start ])
    Or perhaps ....
    ListLast(string, "-")
    since
    ListLast(list [, delimiters ])
    Phil

  • Request for an sql to scan for a special character from the tables

    Hi Gurus
    request for an sql statement for finding the character (and replace with a single byte character ) which occupy
    multibytes(in a unidata char set database) the database which is yet to be migrated to unidata(multibyte) database
    any kind of help is highly apprciated
    Thanks in advance

    Query below will find all bulti-byte characters in string column:
    select  string_column,
            substr(string_column,column_value,1)
      from  some_table,
            table(
                  cast(
                       multiset(
                                select  level
                                  from  dual
                                  connect by level <= length(string_column)
                       as sys.OdciNumberList
      where lengthb(substr(string_column,column_value,1)) > 1
      order by string_column,
               column_value
    /SY.

  • Remove Special Character "/" from file name

    I'm trying to upload a PDF located on my desktop to a server that won't accept special characters, and the mac path is by default ~/desktop/file ... all those forward slashes read as "special characters" and the file is rejected. I moved the file as high as I could, out of the user folder and onto the HD, but it still contains a "/" as the initial character even there.
    How do I get rid of the "/" at the beginning of the file name? Thanks...

    You can't; every location where a file can be placed on a disk available to Mac OS X contains a / in the path. Contact the server's operators or move the file to a computer running another OS.
    (42358)

Maybe you are looking for