How to split a string with a delimiting regular expression like "\r"

Hello!I am a fresh in java programming.Please don't deride my question.What I want to realize is to get the current caret lines and cols in a JTextPane. My question is :After I fetch the string content of the JTextPane and try to split it with the end-line token like "\r" or "\r\n",it always comes me an wrong position.
here is my code:
public void caretUpdate(CaretEvent e) {
int ln,col;
JTextPane textSource = (JTextPane)e.getSource();
String sourceString = textSource.getText();
String subString = sourceString.substring(0,
     (e.getDot() == 0) ? 0 : e.getDot()-1);
String[] splitString = subString.split("\r",-1);
ln = splitString.length;
col = e.getDot() - subString.lastIndexOf("\r");
setCaretPosition(ln,col); //display ln and col in a label
I have got puzzled>_<.Please help me.Any help would be appreciated . (^-^)

Swing text components always use "\n" to separate lines, not "\r" or "\r\n". When you read text in from a file or paste from the clipboard, the line separators are converted to "\n" if necessary. They save the info about what line separators were used in the source file, so if you write the text back to a file it converts them back.

Similar Messages

  • How to split a string with linefeed character ( LF ) as a delimiter in VB6?

    Hi. I am writing a small program in VB 6.0. 
    I have a string named Data and it contains few characters along with <LF> in it. ( <LF> is a new line/line feed character).
    I want to split this string with <LF> as delimiter and store the tokens in an array. I tried with split function and its giving me type mismatch error. Can anyone tell me how to do it?
    I am giving the pseudo-code below.
    Dim Data as string
    Data = "hello, how are you<LS>good morning guys <LS> hi"
    Dim strarray() as stringMsgbox(Data)
    strarray = split(Data, "<LS>")
    When i see the Data in message box before split function, it shows up in different lines like this:
    hello, how are you
    good morning   guys 
    hi
    I want the final array strarray to be like this:   strarray(0) = "hello, how are you"
    strarray(1) = "good morning   guys "
    strarray(2)  = " hi"
    The spaces can be as it is. Just i need the string to be delimited wherever <LS> is there.
    Please help me as soon as possible. Though it is small one, i am stuck here.
    Thanks in advance,
    Satya.

    Hello,
    This forum is for VB.NET, please check the following resources
    http://www.vbforums.com/forumdisplay.php?1-Visual-Basic-6-and-Earlier
    http://social.msdn.microsoft.com/Forums/vstudio/en-US/6a0719fe-14af-47f7-9f51-a8ea2b9c8d6b/where-to-post-your-vb-6-questions?forum=vbgeneral
    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

  • How to split a string with regular expression

    Hi.
    I need to split a string with a regular expression.
    Example
    String = "this is; a test";rune haavik;12345;
    And I want the output to be:
    "this is; a test"
    rune haavik
    12345
    If I use this code:
    private void test1()
    String str = "\"this is; a test\";rune haavik;12345;";
    int i=0;
    String[] tmp = str.split(";");
    while(i<tmp.length)
    System.out.println(tmp);
    i++;
    Then it splits also in the "" text.
    Regards
    Rune haavik

    Rune haavik:
    The most effective way to achieve the end result is, I believe, to read the characters one by one, using a flag that indicates if we are inside quotation or not.
    Well, if we are in a mind game, then the following should do.
           String[] tmp = str.split(";(?![^\"]*\";)");

  • How to split a string with a space in between?

    Hi ABAP Guru's,
    Can you please help me out? How can I split a string into two which has a space in between?
    For example: lets suppose the string is 'LA CA USA'. How can I split this string? I have to dynamically determine the number of characters before the space therefore I cannot use number constant such as 2 or 3.
    With best regards,
    Ketan

    Hi
    Try like this
    DATA : opbal TYPE p DECIMALS 2 VALUE 0.
        SELECT * FROM zs_mkpf_mseg
                        into corresponding fields of table it_opbal
                        WHERE werks =  itmat-werks
                        AND matnr = itmat-matnr
                        AND budat = s_budat-low.
      *  sort  it_opbal by budat.
      loop at it_opbal.
    read table it_opbal.
          IF it_opbal-shkzg = 'S'.
            opbal = opbal + it_opbal-menge.
          ELSEIF it_opbal-shkzg = 'H'.
            opbal = opbal + it_opbal-menge * -1.
          ENDIF.
      endloop.
        append opbal TO itab-opbal.
      ENDFORM.                    " opbal_data
    you can also use thi s
    DATA: NAMES(30)    TYPE C VALUE 'Charly, John , Peter',
          NAMES2       TYPE STRING,
          ONE(10)      TYPE C,
          TWO(10)      TYPE C,
          THREE        TYPE STRING,
          FOUR(4)      TYPE C VALUE 'FOUR',
          DELIMITER(2) VALUE ','.
    SPLIT NAMES AT DELIMITER INTO ONE TWO.
    *     ONE contains 'Charly' and TWO contains 'John , Pet'.
    *     SY-SUBRC is 4, because TWO was not large enough to
    *     accommodate the whole of the remaining string
    SPLIT NAMES AT ',' INTO ONE TWO THREE.
    *     ONE contains 'Charly', TWO contains ' John',
    *     THREE contains ' Peter'.
    SPLIT NAMES AT ', ' INTO ONE THREE TWO.
    *     ONE contains 'Charly', THREE contains 'John',
    *     TWO contains 'Peter'.
    CONCATENATE NAMES '' INTO NAMES2 SEPARATED BY SPACE.
    SPLIT NAMES2 AT DELIMITER INTO ONE TWO THREE FOUR.
    *     ONE contains 'Charly', TWO contains 'John',
    *     THREE contains 'Peter ', FOUR is empty.
    SPLIT NAMES2 AT DELIMITER INTO ONE FOUR THREE.
    *     ONE contains 'Charly', FOUR contains 'John',
    *     THREE contains 'Peter', SY-SUBRC is 4, since
    *     FOUR was not large enough (spaces are significant
    *     characters!)
    Reward all helpfull answers
    Regards
    Pavan

  • Splitting a string with a delimiter

    Hi,
    Below are the lines of code in ABAP.
    v_gpdwstring(17)             TYPE c,
    v_afterdec(3)                TYPE c,
    v_beforedec(10)              TYPE c,
    SPLIT v_gpdwstring AT '.' INTO v_beforedec v_afterdec.
    The test value for v_gpdwstring = "1.53-"
    when the above SPLIT command is executed,
    v_beforedec is returning blank and v_afterdec is returning 53-
    Can anyone tell why v_beforedec is returning blank.
    Thanks

    Hello Radhika,
    When I look at this, it seemed ok.
    So I've tested like this:
    DATA:                                                    
    v_gpdwstring(17)             TYPE c,                     
    v_afterdec(3)                TYPE c,                     
    v_beforedec(10)              TYPE c.                                                                               
    break-point.                                             
    v_gpdwstring = '1.53-'.                                                                               
    SPLIT v_gpdwstring AT '.' INTO v_beforedec v_afterdec.   
    And the result was correct:  v_beforedec as '1', v_afterdec as '53-'.
    Can you re-check please? It seems ok...
    Kind regards,
    Bruno

  • How To Split the String for "."

    Hi Friends
    I am Using Following Code to Split one String.
    String str = "Jeetendra.choudhary";
    String[] sp_str = str.split(".");
    wdComponentApi.getMessageManager.reportSuccess(str[0]);
    wdComponentApi.getMessageManager.reportSuccess(str[1]);
    but its throwing null pointer exception. 
    when i am using following code its working fine.
    String str = "Jeetendra/choudhary";
    String[] sp_str = str.split("/");
    wdComponentApi.getMessageManager.reportSuccess(str[0]);
    wdComponentApi.getMessageManager.reportSuccess(str[1]);
    what may be the issue and how to split the string with "." ?
    Thanks & Regards
    Jeetendra

    "." is a special character.
    Use
    str.split("\\.");
    Regards
    Benjamin
    Edited by: Benjamin Hansen on Dec 29, 2009 7:52 AM

  • How to create a String with comma ?

    Hi All,
    I have a table with 10 records(employee name) and i have to make a string
    with comma delimiter and at the end with "."
    Can anybody tell me how to write a Java program for this ?
    Thanks in advance.

    // I believe the following example gives you the answer.
    class stringEG {
         public static void main(String args[]) {
         String emprs[] ={"1","2","3","4","5","6","7","8","9","10"};
         String vempname = "";
         for(int i=0; i<emprs.length; i++) {
         if(i == (emprs.length-1))
              vempname = vempname + emprs[i] + ".";
         else
              vempname = vempname + emprs[i] + ", ";
         System.out.println("vempname : "+vempname);
    Here dont assume that I asked you to get all the results and putting
    it into the string arrays. But its a simple example to suit your requirement.
    The half-way coding below answers your question, I hope.
    vempname = "";
    while (emprs.next()) {
    if(emprs.isLast())
    vempname = vempname + emprs.getString("empname") + ".";
    else
    vempname = vempname + emprs.getString("empname") + ", ";
    // nats.

  • How to split the string by datetime in sql

    Hi,
    How to split the string by datetime in sql, I've a table with comments column stores comments by datetime, while selecting I want to split and show as in rows by each jobref.
    can anyone help me in this please.
    Thanks,

    declare @callcentre table (comments varchar(max),lbiref varchar(200))
    insert into @callcentre
    select '(28/10/2014 14:56:14) xyz ..... call  logged   (28/10/2014 14:56:58) xyz ..... call updated   (28/10/2014 14:57:41)xyz ..... call updated','Vi2910201'
    insert into @callcentre
    select '(29/10/2014 14:56:14) xyz ..... call  logged   (29/10/2014 14:56:58) xyz ..... call updated   (29/10/2014 14:57:41)xyz ..... call updated','Vi2910202'
    insert into @callcentre
    select '(30/10/2014 14:56:14) xyz ..... call  logged   (30/10/2014 14:56:58) xyz ..... call updated  
    output:
    1) 28/10/2014 14:56:14, (28/10/2014 14:56:14) xyz ..... call  logged ,'Vi2910201'
     2) 28/10/2014 14:56:58 ,(28/10/2014 14:56:58) xyz ..... call updated ,'Vi2910201'
    3) 28/10/2014 14:57:41,  (28/10/2014 14:57:41)xyz ..... call updated,'Vi2910201'
    4) 28/10/2014 14:56:14, (28/10/2014 14:56:14) xyz ..... call  logged ,'Vi2910202'
     5) 28/10/2014 14:56:58 ,(28/10/2014 14:56:58) xyz ..... call updated ,'Vi2910202'
    6) 28/10/2014 14:57:41,  (28/10/2014 14:57:41)xyz ..... call updated,'Vi2910202'
    7) 28/10/2014 14:56:14, (28/10/2014 14:56:14) xyz ..... call  logged ,'Vi2910203'
     8) 28/10/2014 14:56:58 ,(28/10/2014 14:56:58) xyz ..... call updated ,'Vi2910203'
    Thanks,
    See this illustration
    declare @callcentre table (comments varchar(max),lbiref varchar(200))
    insert into @callcentre
    select '(28/10/2014 14:56:14) xyz ..... call logged (28/10/2014 14:56:58) xyz ..... call updated (28/10/2014 14:57:41)xyz ..... call updated','Vi2910201'
    insert into @callcentre
    select '(29/10/2014 14:56:14) xyz ..... call logged (29/10/2014 14:56:58) xyz ..... call updated (29/10/2014 14:57:41)xyz ..... call updated','Vi2910202'
    insert into @callcentre
    select '(30/10/2014 14:56:14) xyz ..... call logged (30/10/2014 14:56:58) xyz ..... call updated','Vi2910203'
    SELECT LEFT(p.u.value('.[1]','varchar(max)'),CHARINDEX(')',p.u.value('.[1]','varchar(max)'))-1) AS [Date],
    '(' + p.u.value('.[1]','varchar(max)') AS comments,
    lbiref
    FROM
    SELECT lbiref,CAST('<Root>' + STUFF(REPLACE(comments,'(','</Data><Data>'),1,7,'') + '</Data></Root>' AS XML) AS x
    FROM @callcentre c
    )t
    CROSS APPLY x.nodes('/Root/Data')p(u)
    and the output
    Date comments lbiref
    28/10/2014 14:56:14 (28/10/2014 14:56:14) xyz ..... call logged Vi2910201
    28/10/2014 14:56:58 (28/10/2014 14:56:58) xyz ..... call updated Vi2910201
    28/10/2014 14:57:41 (28/10/2014 14:57:41)xyz ..... call updated Vi2910201
    29/10/2014 14:56:14 (29/10/2014 14:56:14) xyz ..... call logged Vi2910202
    29/10/2014 14:56:58 (29/10/2014 14:56:58) xyz ..... call updated Vi2910202
    29/10/2014 14:57:41 (29/10/2014 14:57:41)xyz ..... call updated Vi2910202
    30/10/2014 14:56:14 (30/10/2014 14:56:14) xyz ..... call logged Vi2910203
    30/10/2014 14:56:58 (30/10/2014 14:56:58) xyz ..... call updated Vi2910203
    Please Mark This As Answer if it solved your issue
    Please Mark This As Helpful if it helps to solve your issue
    Visakh
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • How to split a string in LabView?

    Hello everyone,
    I'm using the LabView to control a robot named SHRIMP |||. The robot gets commands in hexadecimal format. I need to process info from the command line and therefore I want to split the string of the command line.
    In order to make myself clear this is the case: the command (in hex) is 040100 when 04 is the command itself (set velocity and steering angle), 01 is the velocity to set and 00 is the steering angle. When I read it in Labview I see - /04/01/00. I need to separate the velocity and angle from the string so I used the "search/split string" VI but it doesn't seem to help get rid of the "/" delimiter. I can get only the string before the delimiter or the string with the delimiter inside. When I tried to use offset, it seems that the LabView recognize each part of the command as one char in the string ("/04","/01","/00" are the 3 parts of the string) so I can't use it also. I tried to change the display of the string to hexadecimal format but still no luck- I couldn't convert the string to a number.
    Any ideas?
    Thanks,
    Rap Master

    RapMaster,
    What you probably want in this case is Scan From String. This takes in an input string, a formatter string similar to the ones used in C (here is a refresher if you are not familiar with printf formatters: http://www.psych.upenn.edu/~saul/parasite/man/man3​/printf.3.html), as well as some other parameters that are not as important to this project, and then outputs a number for each field that was represented by a formatter (i.e. %x). Double clicking on the VI brings up a window that will walk you through making the formatters, in case you feel more comfortable that way.
    I'm attaching a VI that does exactly what you requested, using Scan From String. To expand the functionality of this VI, just modify the formatter, and more outputs will appear for each formatter.
    David M.
    Applications Engineer
    National Instruments
    Attachments:
    ScanString.vi ‏8 KB

  • How to create a String with a specific size?

    how to create a String with a specific size?
    For example I want to create different Strings with the size of 100 , 1000 or 63k byte?

    String are immutable so just initialize it with the number of characters you want.
    You might want to look at java.lang.StringBuffer and see if that's what you want.

  • How to concatenate a string with single quotes

    Hi all,
        how to concatenate a string with single quotes to a variable.
    Sathya

    Hi sathyabama,
    1. simple
    2. use TILDE character <b>(`)</b>
       (just left to the '1' key)
    <b> `'mystring'`</b>
    3. just copy paste
    report abc.
    data : m(100) type c.
    concatenate `'amit mittal'` 'hello' into m separated  by space.
    write m.
    regards,
    amit m.

  • How to print a string with out using main method??

    how to print a string with out using main method??
    if we can tell me with example?
    thanks in adavance
    raja

    Hi,
    actually there's none. The UITableView class needs to "know" the tableView's height so either it can calc <number of rows> times <row height> or it has to sum the height of each single row.
    If the row which needs to be of different size always is the first or the last row you can user a tableHeader or tableFoot view.
    Regards
    Dirk

  • How to split this string(char1)char2(char3)char4 into (char1)char2 , .. etc

    how to split this string (char1)char2(char3)char4 into (char1)char2 , (char3)char4?
    String[] result = "(char1)char2(char3)char4".split("\\(");I want :
    result[0] = "(char1)char2" and
    result[0] = "(char3)char4"
    acutally char1,char2,char3, char4 ... is in the form of the below.
    (any charactors except round brace)any charactors except round brace(any charactors except round brace)any charactors except round brace
    I prefer String.split and Pattern.compile().split.
    Edited by: iamjhkang on Feb 5, 2009 3:37 PM
    Edited by: iamjhkang on Feb 5, 2009 3:41 PM

    iamjhkang wrote:
    especially on
    ?= and ?<
    Thanks.The following:
    (?=...)   // positive look ahead
    (?!...)   // negative look ahead
    (?<=...)  // positive look behind
    (?<!...)  // negative look behindare all "look-arounds". See: [http://www.regular-expressions.info/lookaround.html]

  • How to extend the range with a second Airport Express? Do I need a DSL cable?

    How to extend the range with a second Airport Express? Do I need a DSL cable?

    Thanks, now we need a bit more information, please, in order to provide you with the correct information that you need.
    I assume that you already have one AirPort Express.....either a model A1264 or A1392.....set up and operating OK, is that correct?
    If yes, you can extend the network either by using wireless or an Ethernet cable with a second AirPort Express.....either the A1264 or A1392 model. A wired connection is the way to go, if possible, since it provides better performance.  But, wireless might work OK for you if you want to try that.
    Let us know "how" you want to extend......wireless or wired?

  • How about the statement of regular expression like for this

    I what to get varians String array by one regular express for the statement like:
    ${user} like play ${game} in ${date}
    I want to get String[] as {"user","game","date"} by one regex, by str.split(regex). I tried many times but always fail to get the result I expected. May it be possible to meet the destination?
    thanks in advance
    Frederick

    why StringBuffer was using here?So one could split the pattern into multiple lines. But it just occurred to me that one could just use string concatenation, which would look better. I don't know why I bothered to use StringBuffefer.
    e.g.:
    String pattern =
        "^" +           // the start of a string
        "(dog)|(cat)" + // match either "dog" or "cat" at the start of the line
        "\\s*" +        // match an arbitrary amount of whitespace
        "$";            // but don't allow anything other than whitespace after dog or cat
    Pattern p = Pattern.compile(pattern);
    Pattern p = Pattern.compile("^(dog)|(cat)\\s*$");> isn't this look more simpler and better?
    In this case, yes. I was looking for a way to make regexps more explicit, with whitespace and comments, for more complicated cases.
    But those double quotes and plus signs add ugliness of their own, so a regexp would have to be pretty complex before it would be an improvement.

Maybe you are looking for

  • MacBook pro, snow leopard, iPhone and Bluetooth PAN problems

    Short Version: I am having connectivity issues connecting my new MacBook Pro (MBP) to the Internet thru a Bluetooth PAN on my iPhone4. This has been working fine for a few days and then today it just stopped working responding that my iphone connecti

  • What's new in Safari 7.0.6?

    What's new in Safari 7.0.6?  The update announcement doesn't say.

  • Message to Creative: suggesti

    Hello, It is a nice move from you to have firmwares that enable us (users of the Zen Micro and alike) to plug our MP3 players directly to a PC without having to install any drivers... BUT why isn't the player showing the music the way it has been org

  • A different number shows up on caller id

    when my daughter calls me from her phone,  occasionally one of her friends phone number will show up on my caller  id. It also happens on other peoples phone but it is always the same friends number.

  • Error when adding append structure

    Hi All, I need help. We have a "new" source system (GRC), but all the required config was put in place by the BASIS team. The required datasource was activated and tested - all working 100%. As soon as an append structure is added, no matter what the