CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2010
    Posts
    10

    Getting multiple substrings of a string

    Hi,
    I'm looking for a way that I could get multiple substrings out of a string that look something like this:

    Original string: cat ate mouse ->
    sub1: ate mouse->
    sub2: mouse

    So each substring will have 1 less token than the string before it.
    Is anyone got any idea of how could I do this?

    Thank you very much!

  2. #2
    Join Date
    Apr 2010
    Posts
    5

    Re: Getting multiple substrings of a string

    sorry but i'm not use to programming lingo, but i'll go ahead and assume that by token u mean word.

    Code:
    public class test{
    	public static void main(String[] args){
    		String str = "Cat ate mouse";	//you can put any string u want here.
    		String[] words = str.split(" ");	//split the sentence into separate words
    		String[] subs = new String[words.length-1];	//Will contain the list of strings with "one less token"
    		for(int i=0; i<subs.length; i++) subs[i]="";
    		for(int i=1; i<words.length; i++)
    			for(int j=0; j<i; j++)
    				subs[j]+=words[i]+" ";
    		for(String sub:subs) System.out.println(sub);
    	}
    }
    or better yet, here's a function that will take a string and remove the first word from it.

    Code:
    public static String sub(String str){
    	String[] words = str.split(" ");
    	String sub = "";
    	for(int i=1; i<words.length; i++)
    		if(i==words.length-1)sub+=words[i];
    		else sub+=words[i]+" ";
    	return sub;
    }
    Last edited by GenePeer; April 5th, 2010 at 08:58 PM. Reason: I'm new to this forum, so i'm trying to help out us just as much as i'd want you to help me

  3. #3
    Join Date
    Apr 2010
    Posts
    10

    Thumbs up Re: Getting multiple substrings of a string

    That's exactly what I'm looking for!
    Thank you very much!

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

    Re: Getting multiple substrings of a string

    Some additional advice: When concatenating strings in a loop always use a StringBuilder (or StringBuffer if you need it to be thread safe). The performance cost of concatenating strings in loops multiple time, especially if they are large, can be enormous.

    If you want more info on this read this.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Apr 2010
    Posts
    10

    Smile Re: Getting multiple substrings of a string

    Thank for the advice Keang! I definitely need to use stringbuilder on this one.

    Trying to implement my own suffix tree that can take in a phrase rather than
    just one string so it need to read heap load of documents in one go.

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