Replacing all occurrences of characters in a string with one new character?

Hi there,
I'm looking for a way that I could replace all occurrences of a set of specific characters with just one new character for each occurrence.
For example if I have a string which is "word1...word2.......word3....word4............word5" how would I be able to replace this with just one character such as ":" for each set of "." so that it would essentially appear like this "word1:word2:word3:word4:word5"
If I just use replace(".", ":") I am left with "word1:::word2:::::::word4::::word5::::::::::::"
So therefore I'm guessing this would require the use of replaceAll() maybe? but I'm not really very familiar with how regular expressions work and how this would be accomplished using them.
Any help would be greatly appreciated :) Thanks.

Yes, but "." means any character, so ".\+" means "any character repeated more than once". If you just mean a full stop ("period" for you Americans :p) you should use "\.+" as your regular expression, but remember that for Java you need a '\' escape to recognise '\' as '\', so in code you'd actually type your regex as:
"\\.+"Here's an example of one way to do it:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
    public static void main(String[] args) {
       String string = "example1.......example2...example3.....example4";
     Pattern pattern = Pattern.compile("\\.+");
     Matcher stringMatcher = pattern.matcher(string);
     string = stringMatcher.replaceAll(":");
     System.out.println(string);
}Edited by: JohnGraham on Oct 24, 2008 5:19 AM
Edited by: JohnGraham on Oct 24, 2008 5:22 AM

Similar Messages

  • Replace all occurrences of ENTER in a string

    Hi,
    I have a string which contains line breaks:
    '<HTML><HEAD>
    </HEAD>
    <BODY>'
    and it is shown to me in the debugger as:
    '<HTML><HEAD>#</HEAD>#<BODY>'
    Now I want to replace all line breaks in the string,
    but
    replace all occurrences of `#` in text with ' '.
    doesn't work.
    So if you have an idea how to replace the # symbol for Enter in a string please tell me ;).
    regards,
    Stefan

    Hi,
    Pls use
    replace all occurences of '#' in text with cl_abap_char_utilities=>newline.
    Eddy

  • Replace all occurrence with range - issue

    Hi,
    I have a range with "letters" like ":" or "-" or "."
    Note: all entries are with option = "equal"
    Now i need to "replace all occurrences" of "all entries in that range" with space.
    idea:
    loop at range assigning <range>.
    replace all occurrences of <range>-low in aaa with space
    endloop.
    Any better ideas ?
    Regards,
    Gordon

    Sorry but i think my question was not clear.
    is there another way instead of using the "loop" ?
    The translate or replace statement is not the problem
    thx,
    Gordon

  • Replace all special characters in a String with underscore

    I have a String which contains some special characters even(!,$,@,*....).
    I need to replace all the special characters with _ in my String. I do have an idea of using String.replace() and analogous forms, but I would be thankful if anyone can suggest me of a better and an efficient way.
    regards,
    fun_one

    Kaj,
    Thx for your earnest reply. I did have a peep into the API on this method. But the regular expression that I need to use here was beyond my understanding. It did specify some regex that I put to use (something like myString("\D","_"), assuming that I need to replace all non-digit characters ), but it really did not help me getting the result.
    Would you spare some code for me reg. the usage of regular expressions in such a scenario?
    cheers,
    fun_one

  • DUMP: replace all occurrences of space in string1 with string2.

    Why does this statement results in a dump:
    replace all occurrences of space in string1 with string2.
    same with
    replace all occurrences of ' ' in string1 with string2.
    string2 is a string without spaces!
    Is there any drawback on using this statements as a workaround?
    while sy-subrc eq 0.
      replace space with string2 into string1.
    endwhile.
    Thanks
    norbert

    Hi,
    See this example i got from ABAPDOCU
    replacing values
    DATA: t4(10) TYPE c VALUE 'abcdefghij',
          string4 LIKE t4,
          str41(4) TYPE c VALUE 'cdef',
          str42(4) TYPE c VALUE 'klmn',
          str43(2) TYPE c VALUE 'kl',
          str44(6) TYPE c VALUE 'klmnop',
          len4 TYPE i VALUE 2.
    string4 = t4.
    WRITE string4.
    REPLACE str41 WITH str42 INTO string4.
    WRITE / string4.
    string4 = t4.
    REPLACE str41 WITH str42 INTO string4 LENGTH len4.
    WRITE / string4.
    string4 = t4.
    REPLACE str41 WITH str43 INTO string4.
    WRITE / string4.
    string4 = t4.
    REPLACE str41 WITH str44 INTO string4.
    WRITE / string4.
    SKIP.
    ULINE.
    Example for condensing strings
    DATA: string9(25) TYPE c VALUE ' one  two   three    four',
          len9 TYPE i.
    len9 = strlen( string9 ).
    WRITE: string9, '!'.
    WRITE: / 'Length: ', len9.
    CONDENSE string9.
    len9 = strlen( string9 ).
    WRITE: string9, '!'.
    WRITE: / 'Length: ', len9.
    CONDENSE string9 NO-GAPS.
    len9 = strlen( string9 ).
    WRITE: string9, '!'.
    WRITE: / 'Length: ', len9.
    SKIP.
    ULINE.
    Thanks & Regards,
    Judith.

  • MY hard drive crashed and I had to replace it. Now when I try to sync my iPod with iTunes it says that I have to replace the old library (that is lost forever) with the new library to sync. I do not want to lose all the songs on my iPod! What can I do?

    MY hard drive crashed and I had to replace it. Now when I try to sync my iPod with iTunes it says that I have to replace the old library (that is lost forever) with the new library to sync. I do not want to lose all the songs on my iPod! What can I do?

    Copy all the content on your iPod back to your PC and into iTunes. Then go ahead and sync the iPod with your new library.
    See this older post from another forum member Zevoneer covering the different methods and software available to assist you with the task of copying content from your iPod back to your PC and into iTunes.
    https://discussions.apple.com/thread/2452022?start=0&tstart=0
    B-rock

  • I have a very similar problem (5506) in that I changed my appleID loginid and now none of my home shares work. All itunes have been re-authorized/authenticated with the new appleID string. Yet I still receive this error. I too am looking for suggestions.

    I have a very similar problem in that I changed my appleID loginid and now none of my home shares work (5506) . All itunes have been re-authorized/authenticated with the new appleID string. Yet I still receive this error. I too am looking for suggestions.

    If you no longer have the computer(s) you want to deauthorise,
    Log in to iTunes,  go to "view your account info" on the itunes store,  deauthorise all five, (Please Note: this can only be done Once every 12 months)  and then re-authorize your current Computer(s) one at a time.
    Authorise / Deauthorise About
    http://support.apple.com/kb/HT1420

  • I redirected iTunes to an external drive and moved my iTunes library and folder, but my hard drive is still low on space.  How can I be sure that my music is all on the external HD ONLY, especially with the new iCloud technology?

    I redirected iTunes to an external drive and moved my iTunes library and folder, but my hard drive is still low on space.  How can I be sure that my music is all on the external HD ONLY, especially with the new iCloud technology?  It doesn't look like I created any more space by doing this, AND I totally lost all my iTunes playlists.  I'm not sure if because I added old library materials manually if this messed up my playlists, or if the new location is just not reading my moved library.  In any case, I followed step by step instructions and found a few holes and thought I'd just "figure it out" but I guess I'm not as smart as I thought since I'm still scratching my head many hours later.
    Thanks for any support ;-)
    Eaglerocker

    Quick answer if you use iTunes' default preferences settings:  Copy the entire iTunes folder (and in doing so all its subfolders and files) intact to the other drive.  Open iTunes and immediately hold down the option (alt) key (shift on Windows) so you get a prompt to select a library, then guide it to the 'iTunes Library.itl' file in the moved iTunes folder.
    If this is to a new computer and you put the copied iTunes folder in the default location of Macintosh HD > Users > *User Name* > Music  then you don't even need to start with the option key held down, iTunes will automatically look for it there.  (Make sure there isn't anything already in the iTunes folder there that you want to keep since you will be replacing it with the one you are moving.)
    iTunes: How to move [or copy] your music [library] to a new computer [or another drive] - http://support.apple.com/kb/HT4527 - a somewhat bewildering and not always easily understandable set of options.

  • Windows XP (sp2) will not update after replacing the hard disk of my Presario V4435NR with a new one

    Hi,
    I had to replace my Presario V4435NR hard disk (80 GB) with a new 160 GB hard disk from Samsung. The original hard disk did not crash but was churning all the time and did not have enough space for all I wanted to put on it. I installed the new 160 GB hard disk (which of course did not have the recovery partition that my old one had on it) and ran recovery from the recovery DVD I made when I first bought the laptop. Everything worked fine until I tried to update my xp through windows update website. This is the error # I get from windows updates (0x80072EE2).
    How can I get my xp to run updates sealessly as it used to before?
    THank you
    Abdul

    I installed it already and things were fine (updates installed with no problems) for a while but the same problem resurfaced after I installed MS Office. Xp gave me the same error code when I tried to update. Woke up in the morning and tried windows update again and windows found lots of updates and installed them smoothly.
    What is causing this inconsistency?
    * Now, I'm unable to get any updates (windows updates website gives me the same error code)

  • Closing all folders and subfolders within a window with one command

    In list view, is there a way to close all folders and subfolders within a window with one command, instead of having to close each individually?
    Thanks!

    Thanks, all, for your feedback, but no cigar, yet:
    - opening folders with "option" is possible but problematic
    - none of this solves the question of closing all the folders and subfolders within a window in one shot.
    Any better solution would be appreciated.
    Thanks!

  • Whenever I open a new Firefox Browser, the other Firefox Browsers that I have minimized to the bottom of the screen all restore, (reopen up) onto the screen with the new opened Firefox Browser, how do I stop this? Just want the new one to open on screen

    Whenever I open a new Firefox Browser, the other Firefox Browsers that I have minimized to the bottom of the screen all restore, (reopen up) onto the screen with the new opened Firefox Browser, how do I stop this? Just want the new one to open on screen and the existing stay minimized.
    == This happened ==
    Every time Firefox opened
    == I think when a I updated or downloaded a newer version of FireFox at Firefox's suggestion.

    Hmm that does sound a bit strange - I use a system quite similar to yours (XP, FF 3.6.3), and don't see this behaviour.
    I would recommend trying to start Firefox in Firefox [[Safe Mode]] (make sure you close '''all''' running Firefox instances first), and see if this problem persists.
    If not, it could be some add-on causing this behaviour - restart Firefox in Safe Mode, disable all add-ons, and enable them one by one to find the guilty one.
    If it still happens in Firefox Safe Mode, some other program (or something in Windows itself) most likely is the cause. We may be able to help with this too, so let us know what works and provide as much additional information as possible to let us help you solve this.
    Please let us know how above works out, and if we can be of further assistance. Detailed feedback is appreciated and helps us to help you better, plus enables us to better help other users with similar issues.

  • I was able to use the program before. Now when I go in all I see id the blue screen with the new upgrade info.

    I was able to use the numbers program before. Now all I see is the blue screen with the new updates. How do I fix this. I have already downloaded the new version of Icloud & open office $ installed.

    Are you talking about doing this on your iphone?  Or something else?

  • I have replaced an app purchased on the app store with a newer app from the app store .  How can I sell/give away my old software?

    I have replaced an app purchased on the app store with a newer app from the app store .  How can I sell/give away my old software?

    iTunes App Store/Mac App Store content can't be resold or given away. If desired, click here and ask for a refund.
    (119373)

  • Looking to replace my Mac Pro 2.1 graphics card with one that is silent with at least 2 DVI outputs (needed for audio production), any recommendations?

    Looking to replace my Mac Pro 2.1 graphics card with one that is silent with at least 2 DVI outputs (needed for audio production), any recommendations?
    Thanks

    Thanks, but are these are silent?
    Why do they cost over 10 x what seems available for PC?
    Is it not possible to flash the cards for mac?
    Something similar to a ASUS Geforce 8400GS PCI-E 2.0 512 MB DDR2 Graphics Card EN8400GS Silent/P/512M?

  • Java.lang.String:   How can we replace the list of characters in a String?

    String s = "abc$d!efg:i";
    s = s.replace('=', ' ');
    s = s.replace(':', ' ');
    s = s.replace('$', ' ');
    s = s.replace('!', ' ');
    I want to check a list of illegal characters(Ex: $ = : ! ) present in the String 's' and replace all the occurance with empty space ' '
    I feel difficult to follow the above code style.. Because, If list of illegal characters increased, need to add the code again to check & replace the respective illegal character.
    Can we refactor the above code with any other simple way?
    Is there any other way to use Map/Set in this above example?

    RTFM
    http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
    That document is almost perfectly useless for someone
    who is just starting to learn regexes. What they
    need is a good tutorial, and the best one I know of
    for Java programmers is the one at
    this site. When you're ready to move on from
    introductory tutorials, get the latest edition of
    Mastering Regular Expressions, by Jeffrey Friedl.Dear Uncle,
    maybe you want to read that document first before judging its usefulness. And just in case you can't be bothered, the following is an exact quote, which accidently happens to clearly explain the op's question on the usage of \\[\\] in sabre's regex:
    Backslashes, escapes, and quoting
    The backslash character ('\') serves to introduce escaped constructs, as defined in the table above, as well as to quote characters that otherwise would be interpreted as unescaped constructs. Thus the expression \\ matches a single backslash and \{ matches a left brace.
    It is an error to use a backslash prior to any alphabetic character that does not denote an escaped construct; these are reserved for future extensions to the regular-expression language. A backslash may be used prior to a non-alphabetic character regardless of whether that character is part of an unescaped construct.
    Backslashes within string literals in Java source code are interpreted as required by the Java Language Specification as either Unicode escapes or other character escapes. It is therefore necessary to double backslashes in string literals that represent regular expressions to protect them from interpretation by the Java bytecode compiler. The string literal "\b", for example, matches a single backspace character when interpreted as a regular expression, while "\\b" matches a word boundary. The string literal "\(hello\)" is illegal and leads to a compile-time error; in order to match the string (hello) the string literal "\\(hello\\)" must be used.

Maybe you are looking for

  • HtmlDataTable rendering is resulting into java.lang.OutofMemoryException

    Requirement is such that thousands or records are to be displayed in data table in single page without pagination. --> JSF data table while rendering consumes lots of heap memory which is throwing out of memory exception. --> If we renders some limit

  • Problems using "decode"

    What I'm trying to do is: For each employee list the value of all pending orders, the value of all completed orders and the value of all orders issued by the employee. Using these tables: Orders Name ORD_ID SUPPLIER_ID ISSUING_EMP_ID ORDER_DATE ORDER

  • Passing BOUSER variable as a parameter to a DB2 stored procedure

    Hi, We have created a stored procedure in DB2, which accepts five parameters.  We have to pass the userrname of the logged-in user as one of the five parameters. We have created a DeskI report based on this stored procedure and we have used @variable

  • Sampling data (I have a deadline for 12:00 AM!!!) PLEASE HELP!

    Hello, I am getting data in such a format: 01\s\s343.43\s\s\3434\s\s454\r\n 02\s\s etc 03\s\s etc 04\s\s etc and from another source: 01\s\s343.4\s\s43.3\s\s\s545.6\r\n 01\s\s etc 01\s\s etc I am receiving the data from the second source (both are Po

  • Unable to use/instal​l apps on eprintcent​er

    Hi everyone I have a HP Deskjet 3070A which is causing me problems. I have added my printer to eprintcenter when  installed it, but the eprintcenter wants me to add a printer again and that's strange! Now I'm not able to download apps because I canno