Problem in replacing characters of a string ?

Hello everybody,
I want to replace a few characters with their corresponding unicode codepoint values.
I have a userdefined method that gets the unicode codepoint value for a character.
1. I want to know how to replace the characters and have the replaced string after the comparision is over in the for loop in my main.
Currently , i am able to replace , but i am not able to have the replacements done in a single variable.
The output of the code is
e\u3006ame
ena\u3005e
But i want the output i require is,
e\u3006a\u3005e
Please offer some help in this regard
import java.io.*;
class Read1
     public static void main(String s[])
         String rp,snd;
         String tmp="ename";
         for(int i=0;i<tmp.length();i++)
             snd=getCodepoint(tmp.charAt(i));
             if(snd!=null)
                rp=replace(tmp,String.valueOf(tmp.charAt(i)),"\\u"+snd);
                System.out.println(rp);
public static String replace(String source, String pattern, String replace)
     if (source!=null)
         final int len = pattern.length();
         StringBuffer sb = new StringBuffer();
         int found = -1;
         int start = 0;
         while( (found = source.indexOf(pattern, start) ) != -1)
             sb.append(source.substring(start, found));
             sb.append(replace);
             start = found + len;
         sb.append(source.substring(start));
         return sb.toString();
     else return "";
...,Any help in this regard would be useful
Thanks
khurram

This manual replacement thingy reminds me of quite an old technique, when
64KB of memory was considered enough for 20 users (at the same time that is!)
Suppose you have a buffer of, say, n characters. Starting at location i, a region
of chars have to be swapped with bytes starting at location j >= i+l_i; the lengths
of the two regions are l_i and l_j respectively.
Suppose the following method is available:public void reverse(char[] buffer, int f, int l_f) {
   for (int t= f+l_f; --t > f; f++) {
      char tmp=buffer[f]; buffer[f]= buffer[t]; buffer[t]= tmp;
}i.e. the above method reverses a region of characters, starting at position f
with length l_f. Given this simple method, the original problem can be solved
using the following simple sequence:reverse(buffer, i, j+l_j);
reverse(buffer, i, l_j);
reverse(buffer, i+l_j, j-i-l_i);
reverse(buffer, j+l_j-l_i, l_i);Of course, when replacing characters we don't need the last reversal.
kind regards,
Jos (dinosaurus)

Similar Messages

  • Replace characters in a string

    Hi,
    I need to replace all occurrences of control characters except space,newline,tabs in a string . I can give a replace statement for each of these characters but I want to avoid this by making use of regular expressions. Can anyone help me in this regard.
    I tried using the following replace statements with regular expression, but i am not getting the required results:
    replace all occurrences of REGEX '[[:cntrl:]]' in lv_char with space replacement count lv_count_r.
    ---> this replaces even the spaces
    replace all occurrences of REGEX '[[:cntrl:]][^[:space:]]' in lv_char with space replacement count lv_count_r.
    --> this replaced even some alpha numeric characters
    Thanks and Regards,
    Shankar

    is there anyway to do this without using regular
    expressions.. regular expressions are the last
    solution for me..Remember that you can never really replace the characters of a String. Strings are immutable. Once created they cannot change.

  • Replacing characters in a string

    I have an application where a user can enter information into a webform. I'm using JSP, but of course the backend there is a Java function also.
    I am trying to write a function which will replace when the user hits enter with a <br> (break tag).
    Right now it's a complicated, messy loop to look at each character and it has many flaws.
    Is there a "replace" function that will do this for me?
    However, I don't think in a webpage form it transfers the \n end of line characters.

    Nope compiler error
    symbol : method replaceAll (java.lang.String,java.lang.String)
    location: class java.lang.String
    return (txt.replaceAll("\n","<br>"));

  • Replacing characters in a string - URGENT!

    Hi guys
    I have a string - 003452.jpg.xml
    I want to remove the '.jpg'
    How do I do this ... I was using :
    String image1 = 002452.jpg.xml;
    String newString = image1.replace ( '.jpg',' ' );
    What am i doing wrong???
    thanks

    The replace method takes characters as its arguments not strings. If you files will always be in that format you can do something like:
    In j2se 1.4 a replace based on regular expressions was added so you could use:
    String image1 = "002452.jpg.xml";
    String newString = image1.replaceAll ("\\.jpg", "" );

  • Search and replace characters in a string

    I am very new to Sharepoint and need your help.  I have set up a calculated column in a sharepoint list that combines a IT release number to its Release title.  The list is set to appear on a calendar plus web part.  The idea is to show
    the title and release number of all deployments going to the field on a particular day.  The issue is that the release number has a bunch of zero's in it that I do not need to be displayed.  For example a release number could be "RND000000123456". 
    I need to parse out "D000000" so only "RN123456" along with its title show up on the calendar.  Please help!!

    Go here and search for "Remove characters":
    http://msdn.microsoft.com/en-us/library/office/bb862071(v=office.14).aspx
    From that page:
    Remove characters from text
    To remove characters from text, use the LEN, LEFT, and RIGHT functions.
    Column1
    Formula
    Description (possible result)
    Vitamin A
    =LEFT([Column1],LEN([Column1])-2)
    Returns 7 (9-2) characters, starting from left (Vitamin)
    Vitamin B1
    =RIGHT([Column1], LEN([Column1])-8)
    Returns 2 (10-8) characters, starting from right (B1)
    You could also try the REPLACE function:
    http://office.microsoft.com/en-us/windows-sharepoint-services-help/replace-function-HA001161055.aspx
    Brandon Atkinson
    Blog: http://sharepointbrandon.com

  • Need help in replacing special characters in a string

    Hi,
    please let me know the best way to replace all the special characters in a string with space.
    other than alphabets and numbers
    with regards.
    sumanth.

    please let me know the best way to replace all the special characters in a string with space.
    other than alphabets and numbers
    >
    Sumanth Nag Kristam wrote:
    > actually i need to replace hexa decimal char 0X1A in a string.... that is 'substitue' as per the chart
    > any pointers....
    >
    > chk the link for the ASCII codes
    > http://www.techonthenet.com/ascii/chart.php
    But in Hexa decimal value there is no special characters?

  • Replace multiple characters in a string

    Hi, I have some string fields that contain special characters such as ô and û and I want to replace them with ō and ū, and their upper case equivalents as well. How do I write a single formula that would find any of these characters in a string and replace them?
    Thanks,
    Will

    replace(replace(replace(replace(x,'ô','ō'),'û','ū'),'Ô','Ö'),'Û','Ü');
    where x is the string field.  I suggest using the unicodes rather than the actual character.  I do not think that I have the correct uppercase characters.  Please ensure that they are correct in your formula.

  • Replacing characters in a specific region of a String?

    I would like to change characters in a specific region of String.
    For example... change String from "leone24" to "leone31".
    I am not looking to change the occurance of 24 to 31 but the region of 5 to 6, in the above String, from 24 to 31. The characters in this region of the String will vary but the region will always be the position of characters that I want to change in the String.
    Thanks in advance,
    D

    This can be easily done with a StringBuffer:StringBuffer sb = new StringBuffer("leone24");
    sb.replace(5,7,"31");
    String newString = sb.toString();

  • VBA Word Find and Replace characters but excluding certain characters

    I am trying to write VBA code in Word that I will eventually run from a VBA Excel module. The aim of the project is to find specific strings in the open Word document that have length of either one or two characters and are of a certain format, and replace
    them with other strings in the same format. This is to do with transposing (i.e. changing the musical key) of chord symbols in a songsheet in Word. The Find and Replace strings are contained in ranges in an Excel workbook, which is why I want to eventually
    run the code from Excel. I'm much more experienced in writing VBA code in Excel than in Word, and I'm fairly confident with transferring the 'Word VBA' code into an Excel module.
    At the moment I'm trying out code entirely in Word, and I've come across a stumbling block. For example, I want it to Find "A" and replace with "B",
    BUT only if the "A" is NOT followed by "#" (sharp) or "b" (flat).
    Here is the code I've got in Word VBA, which I obtained by editing code produced by the recorder:
    Sub F_R()
    'Find text must have specific font
    With Selection.Find.Font
    .Bold = True
    .Underline = wdUnderlineWords
    .Superscript = False
    .Subscript = False
    End With
    'Replacement text must have specific font
    With Selection.Find.Replacement.Font
    .Bold = True
    .Underline = wdUnderlineWords
    .Superscript = False
    .Subscript = False
    End With
    'Find & Replace strings
    With Selection.Find
    .Text = "A" 'hard-coded here for testing, but this will
    'eventually be referenced to a cell in Excel
    .Replacement.Text = "B" 'hard-coded here for testing, but this will
    'eventually be referenced to a cell in Excel
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = True
    .MatchWholeWord = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    End Sub
    For the Find & Replace section I want to do something like:
    With Selection.Find
    .Text = "A"
    .Text <> "A#"
    .Text <> "Ab"
    .Replacement.Text = "B"
    End With
    - but this produces a syntax error, presumably because you can have only one .Text line (or it won't accept <>?)
    I tried adopting the way of excluding chars when using the Like operator, and while it compiles, it will not replace
    any "A":
    With Selection.Find
    .Text = "A[!b#]"
    .Replacement.Text = "B"
    End With
    I suspect that I'm going to have to change tack completely in the way I'm doing this. Do you have any suggestions, please?
    The chord names/symbols are preceded/succeeded by either spaces or paragraph returns and can look like these, for example (all Font Bold and Underlined words only):
    C<sup>7</sup>
    Dm<sup>7</sup>
    Eb<sup>-5</sup>
    Bb<sup>+11</sup>
    F#m<sup>7</sup>
    i.e. [ABCDEFG][b # | optional][m |optional][- + | superscript, optional][2 3
    5 6 7 9 11 13 | superscript, optional]
    The crux of my problem is that the note A should be treated as entirely distinct from Ab or A# (and similar for other flattened/sharpened notes).
    Sorry for long post.

    Hi Ian,
    It is not easy to find Microsoft forums. However this forum is for the Visual Studio Net version. 
    Try this forum for VBA.
    https://social.msdn.microsoft.com/Forums/en-US/home?forum=isvvba
    Success
    Cor

  • Problem in replacing with the unicode equivalent character ?

    Hello,
    I have a situation wherein i must replace
    m with \u3005
    n with \u3006
    o with \u3041
    etc ...,
    I get the codepoint value from the multibyte represenation .
    But the problem is
    I can not replace
    String temp="ename";
    temp.replace('m','\u3005');
    I am not allowed to hard code the value 3005, rather i am suppose to get the code point value from the multibyte representation.
    I have no problem in getting the code point value from the multibyte represenation.
    After i get the code point value i must concatenate the codepoint value with "\u"
    Currently i am following this implementation to do the replacement,
    String rp=null,snd;
    String tmp="ename";
    String hh="";
    for(int i=0;i<tmp.length();i++)
    snd=getCodepoint(tmp.charAt(i));
    if(snd!=null)
    rp=replace(String.valueOf(tmp.charAt(i)),
    String.valueOf(tmp.charAt(i)),"\\u"+snd);
    hh=hh+rp;
    else
    hh=hh+String.valueOf(tmp.charAt(i));
    //The replace method
    public static String replace(String source, String pattern, String replace)
    if (source!=null)
    final int len = pattern.length();
    StringBuffer sb = new StringBuffer();
    int found = -1;
    int start = 0;
    while( (found = source.indexOf(pattern, start) ) != -1)
    sb.append(source.substring(start, found));
    sb.append(replace);
    start = found + len;
    sb.append(source.substring(start));
    return sb.toString();
    else return "";
    Please tell me how to do the replacement
    Because when i display after the replacement is done i get
    e\u3006a\u3005e for the string "ename"
    where as i should be getting special characters in place of 'm' and 'n' in the string
    or i must atleast get e?a?e for the string "ename"
    Please do offer some suggesstions in this regard
    Your suggesstions would be very useful
    NOTE:
    I am not suppose to make use of the method replaceAll( ) to do the replacements
    Thanks and regards
    khurram

    I am getting these errors
    i am not able to understand why
    i looked into the java2 complete reference text
    ther is no method called isHexDigit( ) in it
    pleasae do help
    the errors are as follows
    [systpe@Agni ~]$ javac C1.java
    C1.java:9: cannot resolve symbol
    symbol : method isHexDigit (char)
    location: class java.lang.Character
    if(!Character.isHexDigit(ch))
    ^
    C1.java:11: cannot resolve symbol
    symbol : method digitValue (char,int)
    location: class java.lang.Character
    v |= Character.digitValue(ch, 16);
    ^
    C1.java:15: replace(char,char) in java.lang.String cannot be applied to (java.lang.String,j
    ava.lang.String)
    String subs = "ename".replace("m", String.valueOf(cv));
    ^
    3 errors
    Thanks & Regards
    khurram

  • Replacing 'word' in a string

    Hi! I want to replace Strings in a String, but if the the substring which has to be replaced is not a word, then i don't want it to be replaced. Eg.: I want to replace Words for other strings, where words are not substring of other Words :)
    Just want to ask if someone has made this before, and can help me out
    cu
    athalay

    But, my original question wasn't this. I
    want to replace words(string) for other strings in
    sentences(string). I hope it's clearer. So for example
    I have a sentence: This is an applepie. And if I want
    to replace apple for pear, then do not replace this
    sentence for pearpie, just remain it, because there's
    no "apple" word in this sentence. I hope it's clearer.
    My main problem is, that handling the First word of
    the sentence. It easy to replace " "+word+" " for what
    I want, but there are separatorchars like , . etc :)Search for the word you want to replace. Once you have found it, you will need to do a series of other checks...
    1) is it at the beginning or end of the sentence? (then you need to look at the character before or after the word depending on location in entire String to see if it is whitespace or separator character)
    2) is it surrounded by whitespace or separator characters on both sides? (get the surrounding characters and compare to a list of acceptable characters to make this a word, ie " ", ",", ";"...
    Only replace words that pass the conditions (beginning of sentence and followed by whitespace or separator, end of sentence and preceeded by whitespace or separator, or middle of sentence and surrounded by whitespace or separators).

  • How to count number of repeated characters in a String

    I have a String.... 10022002202222.
    I need to know how many 2's are there in the string... here the string contains eight 2's..
    Thanks in advance..

    it is workingYes, but... attention to surprises...
    SQL> var v1 varchar2(1000);
    SQL> exec :v1 := 'How to count the number of occurences of a characters in a string';
    PL/SQL procedure successfully completed.
    SQL> select length(:v1) - length(replace(:v1,'c')) from dual;
    LENGTH(:V1)-LENGTH(REPLACE(:V1,'C'))
                                       6
    SQL> exec :v1 := 'cccccc';
    PL/SQL procedure successfully completed.
    SQL> select length(:v1) - length(replace(:v1,'c')) from dual;
    LENGTH(:V1)-LENGTH(REPLACE(:V1,'C'))
    SQL> select length(:v1) - nvl(length(replace(:v1,'c')),0) from dual;
    LENGTH(:V1)-NVL(LENGTH(REPLACE(:V1,'C')),0)
                                              6
    SQL>

  • How to search special characters in a string

    Hi all,
    I want to search special characters in all string columns and all rows in the table.
    The table has about 5 string columns and about 5.000.000 rows. For each row and column, I have to search entries, which included special characters like ", !, ? or something else (f.ex. "Mama?Mia" or "!!!Hotel out of order!!!"). The problem, the character could stand at each position in the string.
    What's the best and most performance possibility to search such entries?? Is it possibility only by SQL; is there a special function for this?? Or must I use PL/SQL.
    Thanks for helping
    Dana

    HTH
    Laurent Schneider
    OCM DBA
    SQL> select * from z;
    S
    Mama?Mia
    a b c
    123
    SQL> select * from z where translate(s,'~ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz1234567890','~') is not null;
    S
    Mama?Mia
    SQL> select * from z where regexp_like(s, '[^[:alnum:][:space:]]');
    S
    Mama?Mia

  • How do I drop the last 3 characters in a string?

    Hello.  I'm trying to find a way to drop the last 3 characters in a string.  The length varies because it is a last name field.  The problem is that when a customer has a suffix on their name, III for example, it is appended to the last name all in the last name field.  If I use the ProperCase function, I end up with "Smith Iii".  How can I tell it to drop the last 3 characters?  I'll then append an UpperCase edition of the last 3 characters to that.  Thank you!

    Use the instring and right string functions then UpperCase

  • How to find out if there are repeated characters in a string

    hey guys
    well i kinda have a little problem figuring out how to find out
    if there are repeated characters in a string.
    if anyone can help, would appreciate it.
    thanks
    milos

    Try using the StringTokenizer class. if u already know which character to trace. this could do the job.
    eg. String str = "here and there its everywhere";
    StringTokenizer st = new StringTokenizer(str, "e");
    int rep = st.countTokens();

Maybe you are looking for

  • Firefox crashes whenever I try to start it

    Yesterday I went into the Apple store to address another issue I was having with my MacBook Pro. After getting that resolved, I found today that when I went to use my Firebox browser, it kept thinking it had to recover a past website that I had gone

  • How can i create a gradient with four colours that graduate from the four corners of a rectangle?

    I've been trying various ways of doing this and nothing's worked so far; I've tried blending two gradients, tried blending four different colours but no luck with that either. What I'm looking to do is exactly what is displayed when you double click

  • Re-install OSX without losing Windows7 partition? (preserving Bootcamp)

    Recently my computer stopped booting into mac OS (apparently unfixable, details on that deduction are below). The question: I plan to reinstall MacOSX (from my startup disc). However, I'd like to be sure that this won't destroy Bootcamp's partitionin

  • NWDS import RFC interface for the structure issue??

    Hi Gurus, I'm working on the SAP NetWeaver BPM with NWDS 7.3 SP12 rite now and got confused about the RFC interface structure stuff. I have a mapping activity defined in the process and the receiver interface needs a RFC. So I go to "Process Modeling

  • Transport of SLD-Systems from QA to PRD

    Hi, because we want to transport everything from QA-XI to PRD-XI we have to transport SLD-Systems as well. Therefore i tried to give some groups to the systems like QA and PRD group. I want to add the previously applied systems to these groups under