CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2007
    Posts
    273

    extraction from a string

    Hello, I had a string like this:

    ....... x.y.z.home.room.bed.sheet.type.price

    I need to put in an array the last three( sheet, type, price). How can I do, the best chance?
    thanks

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: extraction from a string

    You could use the String's split method to split the string at each '.' and extract the last 3 array elements.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jul 2007
    Posts
    273

    Re: extraction from a string

    Quote Originally Posted by keang View Post
    You could use the String's split method to split the string at each '.' and extract the last 3 array elements.
    potentially I could have something of 1000 string to split; do you think split is even fast or is there something better? thanks

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: extraction from a string

    Why not try it and see if it's fast enough before worrying about it.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Jul 2007
    Posts
    273

    Re: extraction from a string

    Quote Originally Posted by keang View Post
    Why not try it and see if it's fast enough before worrying about it.
    Maybe could be not good even I don't notice that its speed is slow..?

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: extraction from a string

    Maybe could be not good even I don't notice that its speed is slow..?
    Sorry, I can't understand that sentence. Are you saying that although it doesn't appear to be slow to you it may not be efficient enough?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Jul 2007
    Posts
    273

    Re: extraction from a string

    OK see I assume split is the only chance; now: what's the best?
    Code:
    		StringBuilder sb = new StringBuilder();
    		for (int ii=0; ii < 1000; ii++ ){
    			sb.append("home" + ".");
    		}
    		sb.replace(sb.length()-1, sb.length()-1, "");
    		String[] sp =  sb.toString().split("[.]");
                     //way 1
    		for (int i=0;  i < sp.length && i<=2; i++)
    			  System.out.println (  sp[sp.length-1-i] );		
    
                    //way2
    		List<String> list = new ArrayList<String>( Arrays.asList(sb.toString().split("[.]") ));		
    		for (int q=0; q < 3; q++) {			
    			System.out.println(  list.remove( list.size() -1 ) );			
    		}
    way1 or way2? I would prefer a stack but I'm not able to convert the split string in a stack<String>

    thanks

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: extraction from a string

    OK see I assume split is the only chance;
    No, I didn't say that. There are many ways of doing this but using the split() method is the easiest to code and understand.

    The point is you shouldn't be worrying about performance efficiencies until you know there is a problem. And by 'know' I mean having analyzed the code using a profiling tool and not just because you think it might be a problem.

    now: what's the best?
    That depends on what you mean by 'Best'. Your code returns the items in reverse order - is that really you want?

    Assuming you mean coding style (and you want the items in the correct order) I would do:
    Code:
    	StringBuilder sb = new StringBuilder();
    	for ( int ii = 0; ii < 1000; ii++ ) {
    		sb.append("home" + ii + ".");
    	}
    
    	sb.setLength(sb.length() - 1);
    
    	String[] sp = sb.toString().split("\\.");
    	for ( int i = sp.length - 3; i < sp.length; i++ ) {
    		System.out.println(sp[i]);
    	}
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured