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.

Similar Messages

  • Replace a string with another string in a file

    I have to replace a string with another string. Say for example in a html file it got a line like,
    <a href="##"></a>
    now i want to replace this ## with the full derived path like
    "c;\\sample folder\\sample subfolder\\samplefile.html"
    can someone help me to do this?
    pls tell me from the file opening till closing the file.

    I have to replace a string with another string. Say
    for example in a html file it got a line like,
    <a href="##"></a>
    now i want to replace this ## with the full derived
    path like
    "c;\\sample folder\\samplesubfolder\\samplefile.html"
    can someone help me to do this?
    pls tell me from the file opening till closing the
    file.
    public class Buckel {
      final static String CONST = "c:\\sample folder\\samplesubfolder\\samplefile.html";
      public Buckel() {
      public static void main ( String[] argv )  throws Exception {
        int    idx = 0;
        String in  = "<a href=\"##\"></a>",   // We'll imagine this just gets here somehow. If it's on the i/p file then take the '\' out B4 and aft the '##'.
               out = "";
        idx = in.indexOf( "##" );
        if ( idx > 0 ) {
          out = in.substring( 0, idx ) + CONST + in.substring( idx+=2, in.length() );
        System.out.println( out );
    }

  • How can I substitute a string with another string in a file

    I have a file. I have a substitute a keyword with another string in all the occurences of a file. How can I do this?

    I'm gonna give you the benifit of the doubt and assume you didn't mean to double post your question.
    As to substitute a keyword with a string one way is to read in the file and run it through a StreamTokenizer class to break it into words. Pull one word at a time, check it, and stick it into a StringBuffer. Once your done with the file overwrite it with what is in the StringBuffer.
    Another way might be to use the RandomAccessFile class, but I'm not really familiar with that because I hardly ever use it.

  • Replaces all occurnces of one String with another String in a given source

    Hi all I had found that there is a lot of interest int this topic. there is a little piece of code that I wrote for myself recently, so I fgigured that it might be helpful for some...
    This method will scan through a string "source", find all of the occurances of a String "before" and replace them with the "after".
    Enjoy :-)
    public static String replace(String source, String before, String after)
    StringBuffer sb = new StringBuffer(source);
    int startpos = source.indexOf(before);
    int endpos = startpos + before.length();
    int i = startpos;
    while( i > -1 )
         if( startpos > -1 && endpos <= source.length() )
         source = sb.replace(startpos, endpos, after).toString();
         int lastReplace = source.lastIndexOf(after) + after.length();
         if(lastReplace <= source.length())
              startpos = source.substring(lastReplace, source.length() ).indexOf(before);
                   endpos = startpos + before.length();
                   i = startpos;
         else
              i = source.length();
         else
              i = -1;
    return source;
    Oh yeah if you want, you can visit my site http://www.infobrokery.com it is where this code is used to replace the text URL with html.

    Since 1.4 the String class has a replaceAll method ...

  • How to replace part of a String with another String :s ?

    Got a String
    " Team_1/Team_2/Team_3/ "
    And I want to be able to rename part of the string (they are seperated by '/'), for example I want to rename "Team_3" to "Team C", and "Team_1" to "Team A" while retaining the '/' character how would I do this?
    I have tried using String.replace(oldValue, newValue); but this doesnt work, any ideas?

    What do you mean that it doesn't work? You do know that the method returns the modified string? The actual string that you invoke the method on is not altered.
    /Kaj

  • How to replace a particular striing with another string in jtextpane

    how to get replace a particular string with another string of a jtextpane without taking the text in a variable using getText() method .

    Thanks for your answer,
    "relevant object" means for you datasources, transfer rules and transformations ?
    With the grouping option "Save for system copy" I can't take easily all datasources, and others objects
    Will I have to pick all object one by one ?
    What would be the adjustement in table RSLOGSYSMAP ? For the moment it's empty.
    Regards
    Guillaume P.

  • Search given string array and replace with another string array using Regex

    Hi All,
    I want to search the given string array and replace with another string array using regex in java
    for example,
    String news = "If you wish to search for any of these characters, they must be preceded by the character to be interpreted"
    String fromValue[] = {"you", "search", "for", "any"}
    String toValue[] = {"me", "dont search", "never", "trip"}
    so the string "you" needs to be converted to "me" i.e you --> me. Similarly
    you --> me
    search --> don't search
    for --> never
    any --> trip
    I want a SINGLE Regular Expression with search and replaces and returns a SINGLE String after replacing all.
    I don't like to iterate one by one and applying regex for each from and to value. Instead i want to iterate the array and form a SINGLE Regulare expression and use to replace the contents of the Entire String.
    One Single regular expression which matches the pattern and solve the issue.
    the output should be as:
    If me wish to don't search never trip etc...,
    Please help me to resolve this.
    Thanks In Advance,
    Kathir

    As stated, no, it can't be done. But that doesn't mean you have to make a separate pass over the input for each word you want to replace. You can employ a regex that matches any word, then use the lower-level Matcher methods to replace the word or not depending on what was matched. Here's an example: import java.util.*;
    import java.util.regex.*;
    public class Test
      static final List<String> oldWords =
          Arrays.asList("you", "search", "for", "any");
      static final List<String> newWords =
          Arrays.asList("me", "dont search", "never", "trip");
      public static void main(String[] args) throws Exception
        String str = "If you wish to search for any of these characters, "
            + "they must be preceded by the character to be interpreted";
        System.out.println(doReplace(str));
      public static String doReplace(String str)
        Pattern p = Pattern.compile("\\b\\w+\\b");
        Matcher m = p.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (m.find())
          int pos = oldWords.indexOf(m.group());
          if (pos > -1)
            m.appendReplacement(sb, "");
            sb.append(newWords.get(pos));
        m.appendTail(sb);
        return sb.toString();
    } This is just a demonstration of the technique; a real-world solution would require a more complicated regex, and I would probably use a Map instead of the two Lists (or arrays).

  • Stepping through a query result set, replacing one string with another.

    I want to write a function that replaces the occurance of a string with another different string.  I need it to be a CF fuction that is callable from another CF function.  I want to "hand" this function an SQL statement (a string) like this:   (Please note, don't bother commenting that "there are eaiser ways to write this SQL..., I've made this simple example to get to the point where I need help.  I have to use a "sub_optimal" SQL syntax just to demonstrate the situation)
    Here is the string I want to pass to the function:
    SELECT
      [VERYLONGTABLENAME].FIRST_NAME,
      [VERYLONGTABLENAME].LAST_NAME,
      [VERYLONGTABLENAME].ADDRESSS
    FROM
      LONGTABLENAME [VERYLONGTABLENAME]
    Here is the contents of the ABRV table:
    TBL_NM,  ABRV    <!--- Header row--->
    VERYLONGTABLENAME, VLTN
    SOMEWHATLONGTALBENAME, SLTN
    MYTABLENAME, MTN
    ATABLENAME, ATN
    The function will return the original string, but with the abreviations in place of the long table names, example:
    SELECT
      VLTN.FIRST_NAME,
      VLTN.LAST_NAME,
      VLTN.ADDRESSS
    FROM
      LONGTABLENAME VLTN
    Notice that only the table names surrounded by brackets and that match a value in the ABRV table have been replaced.  The LONGTABLENAME immediately following the FROM is left as is.
    Now, here is my dum amatuer attempt at writing said function:  Please look at the comment lines for where I need help.
          <cffunction name="AbrvTblNms" output="false" access="remote" returntype="string" >
            <cfargument name="txt" type="string" required="true" />
            <cfset var qAbrvs="">  <!--- variable to hold the query results --->
            <cfset var output_str="#txt#">  <!--- I'm creating a local variable so I can manipulate the data handed in by the TXT parameter.  Is this necessary or can I just use the txt parameter? --->
            <cfquery name="qAbrvs" datasource="cfBAA_odbc" result="rsltAbrvs">
                SELECT TBL_NM, ABRV FROM BAA_TBL_ABRV ORDER BY 1
            </cfquery>
         <!--- I'm assuming that at this point the query has run and there are records in the result set --->
        <cfloop index="idx_str" list="#qAbrvs#">      <!--- Is this correct?  I think not. --->
        <cfset output_str = Replace(output_str, "#idx_str#", )  <!--- Is this correct?  I think not. --->
        </cfloop>               <!--- What am I looping on?  What is the index? How do I do the string replacement? --->
            <!--- The chunck below is a parital listing from my Delphi Object Pascal function that does the same thing
                   I need to know how to write this part in CF9
          while not Eof do
            begin
              s := StringReplace(s, '[' +FieldByName('TBL_NM').AsString + ']', FieldByName('ABRV').AsString, [rfReplaceAll]);
              Next;
            end;
            --->
        <cfreturn output_txt>
        </cffunction>
    I'm mainly struggling with syntax here.  I know what I want to happen, I know how to make it happen in another programming language, just not CF9.  Thanks for any help you can provide.

    RedOctober57 wrote:...
    Thanks for any help you can provide.
    One:
    <cfset var output_str="#txt#">  <!--- I'm creating a local
    variable so I can manipulate the data handed in by the TXT parameter.
    Is this necessary or can I just use the txt parameter? --->
    No you do not need to create a local variable that is a copy of the arguments variable as the arguments scope is already local to the function, but you do not properly reference the arguments scope, so you leave yourself open to using a 'txt' variable in another scope.  Thus the better practice would be to reference "arguments.txt" where you need to.
    Two:
    I know what I want to happen, I know how to make it happen in another programming language, just not CF9.
    Then a better start would be to descirbe what you want to happen and give a simple example in the other programming language.  Most of us are muti-lingual and can parse out clear and clean code in just about any syntax.
    Three:
    <cfloop index="idx_str" list="#qAbrvs#">      <!--- Is this correct?  I think not. --->
    I think you want to be looping over your "qAbrvs" record set returned by your earlier query, maybe.
    <cfloop query="qAbrvs">
    Four:
    <cfset output_str = Replace(output_str, "#idx_str#", )  <!--- Is this correct?  I think not. --->
    Continuing on that assumption I would guess you want to replace each instance of the long string with the short string form that record set.
    <cfset output_str = Replace(output_str,qAbrs.TBLNM,qAbrs.ABRV,"ALL")>
    Five:
    </cfloop>               <!--- What am I looping on?  What is the index? How do I do the string replacement? --->
    If this is true, then you are looping over the record set of tablenames and abreviations that you want to replace in the string.

  • How to use the special character pallet in Maverics with Illustrator

    How to use the special character pallet in Mavericks with Illustrator - I follow the help instructions, but the pasted in object looks like a rectangle with an X, I have done this before, in earlier versions of AI, but cannot make it work in CS6.
    In fact, I can open an old AI file (CS4), copy the character (a graphic symbol, also taken from the special character set), paste it in, and it works, but can't do this with a new one, from the drop down, Show Character Viewer, in the Mavericks menu bar.

    The box with an X indicates the font you are using does not have the glyph you are trying to paste. Try a different font.

  • I need to append a string to another string

    I'm working with some inherited code, I'm a Colf Fusion
    novice myself, and I'm trying to make this order form display the
    correct data. The problem is a lot of data in the database is
    missing. Description in the QStockDB query can contain a lot of
    stuff. For our full color work the text "4/0", "4/BLACK", and "4/4"
    are consistent so I'm changing the newitem (I know, not very
    descriptive but it's not my code) to CMYK Printing.
    <cfif #QStockDB.description# contains "4/0"><cfset
    newitem = "CMYK Printing"></cfif>
    <cfif #QStockDB.description# contains
    "4/4/BLACK"><cfset newitem = "CMYK Printing"></cfif>
    <cfif #QStockDB.description# contains "4/4"><cfset
    newitem = "CMYK Printing"></cfif>
    I want to then go back through description and compare it
    more to add more description. For instance with this:
    <cfif #QStockDB.description# contains "12 pt"><cfset
    newitem = newitem + " - BC"></cfif>
    I know that the job is a business card. So I want to append
    the newitem variable with " - BC". Likewise:
    <cfif #QStockDB.description# contains
    "catalog"><cfset newitem = newitem + " - Catalog
    Sheets"></cfif>
    displays CMYK Printing - Catalog Sheets. Or, it should... or,
    more precisely, I want it to. :)
    How do I append a string to another string?

    + is the addiion operator and works with numbers. Because
    your string is a ...well, string... you need to use an ampersand.
    Thus, instead of:
    <cfif #QStockDB.description# contains
    "catalog"><cfset newitem = newitem + " - Catalog
    Sheets"></cfif>
    Use...
    <cfif #QStockDB.description# contains
    "catalog"><cfset newitem = newitem & " - Catalog
    Sheets"></cfif>
    <cfoutput>#newitem#</cfoutput>
    At least I think that will work - haven't tested it
    though.

  • Replace hex character in a string with another hex character

    Hi Guys,
    Heres a problem scenario, hex character 92 looks very much like hex character 27 (apostrophe).
    I need to write a program that can replace all hex character 92 to hex 27 in a string.
    Being a novice on the regular expressions, I would appreciate if somebody can show
    the exact syntax to to perform this replacement.
    Many Thanks
    -Anil

    I don't think you need to use regex to do this:
    String s = something();
    char a = 0x27;
    char b = 0x92;
    s.replace(a,b);
    And don't forget that since Strings are immutable you need:
    s = s.replace(a,b);to do anything useful.

  • How can I replace the Special Character        '

    Hi Guru ,
    I want replace one Special Chareacter into space .
    Example -
    There have one variable .
    L_VAR = BANGA'LORE
    I want to delete the special character ( ' ) which is between A and L .
    Whenever I am tryiny to use replcae statement , it will show one Error Message .
    REPLACE ''' IN L_VAR WITH ' '.
    IT WILL GIVE ONE ERROR MESSAGE - " Literals that take up more than one line are not permitted."
    How Can I replace this .

    Hi Tarak Dey,
    This error
    Literals that take up more than one line are not permitted
    is not for the Replace statement. You would have missed a PERIOD in some statement before this.
    When we see the code that you have pasted,
    L_VAR = BANGA'LORE
    here period is missing at the end of the line. The error could be because of that.
    Kindly check.
    Best Regards,
    Ram.

  • Comparing string delimited by semicolon in a column with another string

    Hi
    I have requirement like comparing a column which contains the string delimited by semi colon with a string delimited by semicolon. i.e. comparing the multiple values with multiple values.for this purpose i am using the following code
    select ams.application_id,it_mgmt_chain
    FROM tb_ams038_application ams,THE
    (SELECT CAST
    (Appmgmt_Str_To_Table
    (NVL (ams.it_mgmt_chain,
    ) AS appmgmt_tabletype_string
    FROM DUAL
    AND (COLUMN_VALUE IN ( SELECT COLUMN_VALUE
    FROM THE
    (SELECT CAST
    (Appmgmt_Str_To_Table
    (nvl(NVL (replace('1234;8383;83939;93939',',',';'),
    ams.it_mgmt_chain
    ) AS appmgmt_tabletype_string
    FROM DUAL
    here in the above it_mgmt_chain has the string containing numbers seperated by semi colon. and the Appmgmt_Str_To_Table function used to split the string and return it in the form of list. and appmgmt_tabletype_string is a varray which is created for spliting and storing purpose.
    Can anyone please suggest the best way to do this task as the above code is giving a performance issue.

    This example uses a "|" not a semicolon but it leverages the REGEXP_SUBSTR, SUBSTR, LENGTH to perform a parsing action.
    http://blog.mclaughlinsoftware.com/plsql-programming/a-result-cache-function-returning-a-collection/
    You may wish to remove the "THE" and replace it with "TABLE" too. Good luck.

  • Formatting a string with Display String

    I have a field in my database that is a string with numbers in it and I need it formatted like so: "xx-xx-xx-xxx" nine characters long.
    For example: 92438014
    I need it to look like: 09-24-38-014
    and
    141234892
    14-12-34-892 etc..
    can anyone help me with some code that will do this for me? I'm not the best at crystal custom formatting.
    thank you
    Edited by: MarcieHennessy on Oct 14, 2009 10:47 PM

    Raghavendra.G
    I was on this site searching for an answer to my problem, and you provided the solution for me with your response to the above.  However, my problem was a bit different.  I needed a way to format a 9-digit number string into a standard Social Security number format.  I used your formula from above, and WOW - it worked!  If I could award you double Forum Points, I would.  But, since I can't do that, I will just say thanks, and ask God to bless you profusely.
    Kathryn J. Ryan.

  • Find occurences of a string inside another string

    How can I loop through String text for occurences of String lineSeparator, and replace each occurence with String lineShift?
    String lineSeparator = "<line_separator/>";
    String lineShift = "\n";
    String text = "This<line_separator/>is<line_separator/>a<line_separator/>test<line_separator/>";Do I have to use Patterns or something?

    String lineSeparator = "<line_separator/>";
    String lineShift = "\n";
    String text = "This<line_separator/>is<line_separator/>a<line_separator/>test<line_separator/>";
    String modified = text.replaceAll(lineSeparator, lineShift);
    System.out.println(modified);

Maybe you are looking for