Replacing ' char in a string

How can I add a \(backslash) before before a '(apostrophe) in a String ?
I've been tryin' myString.replaceAll("'","\'") and myStringBuffer.deleteCharAt & insert but with no success

sorry... i don't read the API before answering u ;) replaceAll related to regex n it's new for me... it's alredy 11:15 PM, i'm going to sleep (i don't want to learn about regex for now)...
here is other solution:
public class ItsWork {
     public static String insertBefore(String original, String add, String before) {
          StringBuffer sb = new StringBuffer();
          int i = 0;
          do {
               i = original.indexOf(before);
               if (i != -1) {
                    sb.append(original.substring(0, i));
                    sb.append(add);
                    sb.append(before);
                    original = original.substring(i + 1, original.length());
               } else break;
          } while (true);
          sb.append(original);
          return sb.toString();
     public static void main(String[] args) {
          String myString = "do this realy don't work?";
          System.out.println(myString);
          myString = insertBefore(myString, "\\", "\'");
          System.out.println(myString);
}

Similar Messages

  • Replacing chars in a string by index and not sequence??

    Hi,
    I wish to replace the first character of a string, the only functions I see are replace and translate but they only replace based on char value, I have a work around do but concat strings and chars but I was wondering if there is a more graceful approach to doing this,
    Cheers.

    concat strings and chars but I was wondering if there is a more graceful approach to doing thissubstr & concatenation are graceful enough.

  • Replace char on String

    Hi All,
    String strValue = "ABC TPT 0694";
    My String is as shown above, I am looking for a way to replace char at byte 6 (position 6) with another Char in String.
    For example at location 6 it is "T" I want to replace it with char "O". can any one tell me how to do this

    String strValue = "ABC TPT 0694";
    char[] chars = strValue.toCharArray();
    chars[5] = 'O';
    strValue = String.valueOf(chars);

  • Replacing chars in strings

    Here is the situation:
    int x=4
    char letter1='Z'
    String word1="blah blah"
    /code]
    I was wonering if the was an easy way of replacing a character in a string with another at a ceratin point in the string. In this case, I want a string that would say "blahZblah". Thanks.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    Sure
    int x=4
    char letter1='Z'
    String word1="blah blah"
    [String word2 = word1.substring(0,x) + letter1 + word.substring(x + 1)[/b]

  • Deleting the last char of a string

    How can I Delete the last char of a string???
    example:
    asd@fdg@vvdfgfd@gdgdfgfd@gdfgdf@
    I want delete the last @!
    Tks in advance!

    hi,
    try this:
    lv_count = strlen(string).
    lv_count_last = lv-count - 1.
    replace string+lv_count_last(lv_count) in string by ' '.
    This should work.
    thnx,
    ags.
    Edited by: Agasti Kale on Jun 4, 2008 9:03 PM

  • I want to replace "\" with "/" in a string.

    I tried this...
    String whatever = myString.replace("\\", "/");
    ...and this...
    String whatever = myString.replace("\", "/");
    ...but I get an error.
    I was able to get it to work doing it this way...
    String slashStr = "\\/";
    char slashArr[] = slashStr.toCharArray();
    whatever = whatever.replace(slashArr[0], slashArr[1]);
    But I think that is silly.
    Can someone tell me why the first method won't work?

    String whatever = myString.replace("\\", "/");
    String.replace
    ...and this...
    String whatever = myString.replace("\", "/");
    ...but I get an error.This is correct. There is no method String.replace(String,String)
    only String.replace(char,char)
    I was able to get it to work doing it this way...
    String slashStr = "\\/";
    char slashArr[] = slashStr.toCharArray();
    whatever = whatever.replace(slashArr[0], slashArr[1]);
    But I think that is silly.It is a bit.
    String s1 = "This\\Is\\The\\Original";
    String s2 = s1.replace('\\','/');
    ...Or something like that... Single quotes indicate a char literal
    - double-quotes indicate a string literal.
    Talden

  • Deleting Chars in a String.

    I found some methods in the documantation which should work. like replace for example.
    If I have, let's say the string "2 metres" and I want to cut off the " metres", i just write it like:
    stringName.replace(" custom", ""); ?
    But my compiler complains with: replace(char,char) in java.lang.String cannot be applied to (java.lang.String, java.lang.String... what am I doing wrong?

    Thanks, that did help much. But now I have another
    problem.. i want to convert an Int to a string and I
    can't get it to work...
    Stringname = valueOf(Integername);
    That's what I thougt, but i get "cannot resolve
    symbol. Method valueOf(int)....
    Stringname = String.valueOf(Integername);/Kaj

  • Replacing substrings in a string.

    I have three strings.
    String one
    String two
    String three
    String one is a substring occuring somewhere in String three.
    I want to replace String one occuring in String three with String two.
    How can i do this.
    Please help.

    Hi ,
    you cannot replace the String but you can get the replaced String
    for more information see the following links
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#replace(char, char)
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#replaceFirst(java.lang.String, java.lang.String)
    API in String class is
    1. String replace(char oldChar, char newChar)
    2. String replaceAll(String regex, String replacement)
    3. String replaceFirst(String regex, String replacement)
    the above three API returns the result but the original string cannot be changed.
    If you want to change the original string use StringBuffer instead of String. for more detail for StringBuffer is see the following link
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/StringBuffer.html

  • How to use replace(CharacterSequence, CharacterSequence) in String class

    Need simple java program using the following
    how to use replace(CharacterSequence, CharacterSequence) in String class
    Kindly help

    From http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replace(java.lang.CharSequence,%20java.lang.CharSequence)
    replace
    public String replace(CharSequence target,
    CharSequence replacement)Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".
    Parameters:
    target - The sequence of char values to be replaced
    replacement - The replacement sequence of char values
    Returns:
    The resulting string
    Throws:
    NullPointerException - if target or replacement is null.
    Since:
    1.5

  • Replacing chars

    I have a problem with replacing chars. I want to replace the following chars <, > and '
    I've implemented the following code:
    remove(String str) {
        Pattern p = Pattern.compile("[<>']");
        Matcher m = p.matcher(str);
        StringBuffer sb = new StringBuffer();
        boolean result = m.find();
        boolean deletedIllegalChars = false;
        while (result) {
          deletedIllegalChars = true;
          m.appendReplacement(sb, " ");
          result = m.find();
        // Add the last segment of input to the new String
        m.appendTail(sb);
        str = sb.toString();
        return str;
    }The problem now is, that the ' is not replaced after running the method above. What I'm doing wrong?
    P.S.: Maybe my problem doesn't fit to this forum, but I don't found a better place.

    The problem now is, that the ' is not replaced after
    running the method above. What I'm doing wrong?
    It is on my computer, maybe the string doesn't really contain the ascii apostrophe-signle quote but some special "smart quote" or the acute/grave accent symbol. They all look like the similar but aren't the same.
    For instance the preferred character for apostrophe is \u2019 according to the unicode spec. Maybe you want to be replacing it instead/aswell.
    P.S.: Maybe my problem doesn't fit to this forum, but
    I don't found a better place.I think Java Programming and New to Java get most of the miscallaneous posts..

  • Replace frist in html string

    Hi Guys,
    I have an html string defined in property file.<html>,,,,,,<body>,,,,,blah blah $1 blah blah .........</html>
    I have html and eg $1, $2 so on.
    I want to replace $1 with another string value in the program, I tried str.replaceFirst("\\$1",string);
    but didnt work,
    please help me replcing it,
    thanks,

    DrClap wrote:
    It didn't work because that method takes a regular expression as its first parameter. (The API documentation does mention that fact.) The regular expression you passed it ("$1") ...The OP did escape the $, but &#92;&#92; get eaten by the forum software as a new-line char.
    @OP, the replaceFirst(...) returns a new String (and does not change the original String!), so you will have to do:
    String s = "<html>,,,,,,<body>,,,,,blah blah $1 blah blah .........</html>";
    s = s.replaceFirst("\\$1", "XYZ");

  • Find and replace value in Delimited String

    Hi All,
    I have a requirement, where i need to find and replace values in delimited string.
    For example, the string is "GL~1001~157747~FEB-13~CREDIT~A~N~USD~NULL~". The 4th column gives month and year. I need to replace it with previous month name. For example: "GL~1001~157747~JAN-13~CREDIT~A~N~USD~NULL~". I need to do same for last 12 months.
    I thought of first devide the values and store it in variable and then after replacing it with required value, join it back.
    I just wanted to know if there is any better way to do it?

    for example (Assumption: the abbreviated month is the first occurance of 3 consecutive alphabetic charachters)
    with testdata as (
    select 'GL~1001~157747~FEB-13~CREDIT~A~N~USD~NULL~' str from dual
    select
    str
    ,regexp_substr(str, '[[:alpha:]]{3}') part
    ,to_date('01'||regexp_substr(str, '[[:alpha:]]{3}')||'2013', 'DDMONYYYY') part_date
    ,replace (str
             ,regexp_substr(str, '[[:alpha:]]{3}')
             ,to_char(add_months(to_date('01'||regexp_substr(str, '[[:alpha:]]{3}')||'2013', 'DDMONYYYY'),-1),'MON')
    ) res
    from testdata
    STR
    PART
    PART_DATE
    RES
    GL~1001~157747~FEB-13~CREDIT~A~N~USD~NULL~
    FEB
    02/01/2013
    GL~1001~157747~JAN-13~CREDIT~A~N~USD~NULL~
    with year included
    with testdata as (
    select 'GL~1001~157747~JAN-13~CREDIT~A~N~USD~NULL~' str from dual
    select
    str
    ,regexp_substr(str, '[[:alpha:]]{3}-\d{2}') part
    ,to_date(regexp_substr(str, '[[:alpha:]]{3}-\d{2}'), 'MON-YY') part_date
    ,replace (str
             ,regexp_substr(str, '[[:alpha:]]{3}-\d{2}')
             ,to_char(add_months(to_date(regexp_substr(str, '[[:alpha:]]{3}-\d{2}'), 'MON-YY'),-1),'MON-YY')
    ) res
    from testdata
    STR
    PART
    PART_DATE
    RES
    GL~1001~157747~JAN-13~CREDIT~A~N~USD~NULL~
    JAN-13
    01/01/2013
    GL~1001~157747~DEC-12~CREDIT~A~N~USD~NULL~
    Message was edited by: chris227 year included

  • How to delete the last char in a String?

    i want to delete the last char in a String, but i don't want to convert the String to a StringBuffer or an array, who knows how to do?

    Try it in this way
    String MyString = "ABCDEF";
    MyString = MyString.substring(0,MyString.length()-1);

  • How to remove last char in a string by space

    I have to implement backspace application(remove a last char in a string , when we pressed a button).
    for ex: I enter the no
    1234
    instead of 1236
    So when i press a button on the JWindow... it should display
    123
    so that i can enter 6 now.
    I tried to display the string "123 " instead over "1234" but it is not working when the no background is specified. but works fine when a background color is specified.
    The string is displayed as
    Graphics2D g = (Graphics2D)window.getGraphics();
    AttributedString as = new AttributedString(string, map);
    g.drawString(as.getIterator(),x,y);
    In the map, the background is set to NO_BACKGROUND.
    Thanks and regards

    Deja vu. I saw this kind of post before, and I'm sure
    it was today.http://forum.java.sun.com/thread.jspa?threadID=588110&tstart=0
    Here it is.

  • Deleting last char in a string...

    I have a string that is created as follows: strBuffer += k.getKeyChar();(k is KeyPressed ...KeyEvent) How can I delete the last Char in the string strBuffer?

    Hi
    // simply substring
    if (strBuffer.length() > 1)
        strBuffer = strBuffer.substring(0, strBuffer.length()-1);
    // you may also use
    if (strBuffer.length() > 1)
        StringBuffer buffer = new StringBuffer(strBuffer).
                                   deleteCharAt(strBuffer.length()-1);
        strBuffer = buffer.toString();
    }I've not tested the above code myself & I've written the above code on the go after reading your query. The above code definitely work & help you.
    Regards,
    JPassion

Maybe you are looking for

  • How to increase the max character for a table name..!!

    Hi All, The maximum characters that supports for the table Name is 30 How can I increase this so that I can have table names lengthier than 30 This is required as I was doing a conversion of the database from SQL server 2005 to Oracle 9i Many thanks

  • Using Search Strings

    Dears We have a scenario where bank is using NTRF both for check and Bank Transfer. Only way to distinguish that it is check is that immediately after External Transaction the check number is of 6 character otherwise it is Bank Transfer. In the EBS c

  • What should be the data type for a field in ztable?

    i am desing a ztable , wht should be the datatype for a field if i want to enter values like -1.5 and -2.5 , negative values with decimals what should be the data type???

  • Automatically create PO for STO PR

    Dear Gurus,       There have central distribute center and non-central distribute center in our company, which are defined as different plant in one company in SAP system. The goods moving between central DC and non-central DC is used stock transfer

  • SPA 122 remove restricted access domains

    Hello My spa 122 has a customization profile active and i want to remove it. But i don't know how to do it. Firmeware Update, Factory reset, disabling provisioning don't help. I tried to create a sample config file with the Cisco Profile Compiler but