#1 2008-12-01 16:24:48

fwalle
Member
Registered: 2008-08-01
Posts: 50

Cut a String

Hi,

I want to cut a string and to take a part of it between '*'...

exple I have


aaaa  aa   *   bb *cccc * dddd

So I what only 'bb'

So I try :

fields != null &&fields.matches("[\\*]")?
fields. split("[\\*]")[1]:"11"
:"22"           

It doesn't work I always have '11'


I MUST test if '*' exist because some time my field is like :
"aaa freigeoghogn" without '*' ..

Thanks for help...

Working on :
TIS 2.4.1 r16077

Last edited by fwalle (2008-12-01 16:26:55)

Offline

#2 2008-12-01 18:37:52

amaumont
Talend team
Registered: 2006-09-20
Posts: 471

Re: Cut a String

Use the following code because the matches method try to match the entire string:

Code:

... fields.matches(".*\\*.*") ...

else you could use

Code:

... fields.contains("*") ...

Last edited by amaumont (2008-12-01 18:38:18)

Offline

#3 2008-12-02 09:28:59

fwalle
Member
Registered: 2008-08-01
Posts: 50

Re: Cut a String

ok And How I cut the string? with split ? ?


this doesn't work:

fields.matches(".*\\*.*")


I have an index array out of bouds after  with this:
fields. split(".*\\*.*")[1]

Last edited by fwalle (2008-12-02 09:48:26)

Offline

#4 2008-12-02 09:49:24

fwalle
Member
Registered: 2008-08-01
Posts: 50

Re: Cut a String

I find IT I shouldn't change the split one smile


fields!= null &&
fields.matches(".*\\*.*") ?
fields. split("[\\*]")[1]:"11"


THANKS GUYYYYyyyyyyyyyyyy

Offline

#5 2008-12-02 10:18:52

shong
Talend team
Registered: 2007-08-29
Posts: 10310
Website

Re: Cut a String

Hello fwalle

Do you want to get the string only between the first * and the second *?
if so, here is a routine:

Code:

// template routine Java
package routines.ff;

public class myRoutines {

 
    public static String cutString(String message){
        int length = message.length();
        int firstMark = 0;
        int secondMark = 0;
        int numMark = 0;
        String result = null;
        String s;
        for (int i = 0; i < length; i++) {
            if (numMark == 2) {
                break;
            }
            s = message.substring(i, i + 1);
            if (s.equals("*")) {
                numMark++;

                if (numMark == 1) {
                    firstMark = i;
                } else {
                    secondMark = i;

                }

            }

        }

        if (numMark == 2) {
            result = message.substring(firstMark + 1, secondMark);
            return result.trim();
        } else {
            return null;
        }
    }
}

Best regards

         shong


Uploaded Images


Email:shong@talend.com
Choose Talend, Enjoy Talend!
New & Event: Talend Help Center
Talend-->the leader of open source data management and application integration solutions!

Offline

#6 2008-12-02 10:33:37

fwalle
Member
Registered: 2008-08-01
Posts: 50

Re: Cut a String

Yes I want the string between first and second '*' but with split it is working and more easier ... it seem to be ok ...
Thanks a lot

Offline

Board footer

Powered by FluxBB