Page 1 of 2 12 LastLast
Results 1 to 20 of 29
  1. #1
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Split string and joining substrings

    Hi

    I have a question.

    When I do a split string I get several strings (also called sub strings from what i can read.) Is there a way to mane the sub strings?

    I have about 100 sub strings that I want in an string array like this:
    Java Code:
    String [] Joint = {subs1, subs2, subs3, subs4} // and continue until split strings are "used up"
    Is there a way to do this??

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Split string and joining substrings

    Is there a way to mane the sub strings?
    Please explain what you are trying to do. I don't understand what "mane" means.

    If you want to concatenate a number of Strings together into one String see the StringBuilder class.

    I have about 100 sub strings that I want in an string array like this:
    Your statement is one way to build an array of Strings.

  3. #3
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Split string and joining substrings

    Quote Originally Posted by Norm View Post
    Please explain what you are trying to do. I don't understand what "mane" means.
    I meant name (TGIF)

    I am reading out of a xml file and parsing strings. (I'm using DOM to do so and the info I want is all in attributes) and so far I get the data / info out by using a string builder. Then move that string over to other class and then try to do stuff with it.

    More info here:
    Parser code
    and on the bottom Here is how I want to present it.

    I get the output out in single lines. If i could give names to the output of split I could make an array as I need it and then place it in the table.

    Something like this:
    Java Code:
                     String test = SBI.toString();
    		TableItem items = new TableItem(table, SWT.NONE);
    		  String delimiter = ",";
    		  /* given string will be split by the argument delimiter provided. */
    		  XMLItems = test.split(delimiter);
    		  /* print substrings */
    		  for(int i =0; i < XMLItems.length ; i++)
                              Item1 = XMLItem[i]
    			  Item2 = XMLItem[i+1]
                              Item3 = XMLItem[i+2]
                              Item4 = XMLItem[i+3]
    		String XMLItem [] = {Item1, Item2, Item3, Item4};

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Split string and joining substrings

    give names to the output of split I could make an array
    You don't need to give names to each String. Put them into the array as soon as you get them:
    Define the array to hold the 4 items and put the items into it as you get them.

  5. #5
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Split string and joining substrings

    Could you please give an example on code for setting array length?

  6. #6
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Split string and joining substrings

    Come to think of I guess it to be like this
    Java Code:
    String array[4]

  7. #7
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Split string and joining substrings

    Quote Originally Posted by KarlNorway View Post
    Come to think of I guess it to be like this
    Java Code:
    String array[4]
    You can declare array reference variable like this:

    Java Code:
    String[] array;
    or

    Java Code:
    String array[];

  8. #8
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Split string and joining substrings

    Ok but howw to set the array lenght?

  9. #9
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Split string and joining substrings

    Quote Originally Posted by KarlNorway View Post
    Ok but howw to set the array lenght?
    Java Code:
    array = new String[4];

  10. #10
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Split string and joining substrings

    Ok so I dont know how to make the array have a lenght of four items

    I have tried this but I only get one entry into the table.

    Java Code:
    String [] XMLItems = new String [3]; // trying to set String array lenght to 4 items (0-3)
    
    		SBI = DOMParser.sbi;
    
    		String test = SBI.toString(); //String to split
    		TableItem items = new TableItem(table, SWT.NONE); // where to place the strings
    		 
    		  // delimiter 
    		  String delimiter2 = ",";
    
    		  XMLItems = test.split(delimiter2);
    
    		  // print substrings 
    		  for(int i =0; i < XMLItems.length ; i++)
    			
    			 System.out.println(XMLItems[i]); // this gives same output as if I dont try to set lenght. I would like it to be like this: [item1] [item2] [item3] [item4] newline and repeat
    
    		  
    		  	
    		    items.setText(XMLItems); //Placing the string into the table
    If anyone can help out I would be really greatful...

    Could I use a while loop? So that for every fourth item it posts to the table
    Last edited by KarlNorway; 01-27-2012 at 07:27 PM.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Split string and joining substrings

    Please post the full text of the error message you get.

    The value in [] is the number of items to be held in the array, NOT the index of the last item.
    You are creating an array with three items.

    Did you miss all the examples posted earlier?
    new String[4]

  12. #12
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Split string and joining substrings

    I dont get any error message just not the result I want

    Thinking about I might have to repost to the table after every fourth item have been split.
    Last edited by KarlNorway; 01-27-2012 at 07:34 PM.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Split string and joining substrings

    the result I want
    What is the "result" you want?

    The split() method creates a new array with the split Strings. Your earlier definition of the array is replace by what the split method returns.
    Last edited by Norm; 01-27-2012 at 07:36 PM.

  14. #14
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Split string and joining substrings

    Is it possible to set the split to make the array lenght 4 items then?
    I have a total of (at least) 20 (*4) Item lines that should go in the table (see pic)

    Split string and joining substrings-table.jpg

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Split string and joining substrings

    Is it possible to set the split to make the array lenght 4
    I don't think so. You can easily check the array's length by using its .length attribute.
    If there is only one item, would the remaining elements be left null? Many methods fail with null values.

  16. #16
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Split string and joining substrings

    Ok .
    Java Code:
    System.out.println("Items:" + XMLItems.length); // gives 85
    So Im looking to do something that cant be done?

    Do you think it would be possible to have a counter that resets and posts the array for every fourth item in the array?

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Split string and joining substrings

    What is it you want done? Define that and we'll help you write the code to do it.
    There is not a method for every possible way to do things. That's why they hire programmers. Otherwise we'd be out of jobs.

  18. #18
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Split string and joining substrings

    I want the 85 items to go into the table in the pic above.

    The items are parsed from a XML file. Each item contains four attributes I want displayed in the table.
    I know that the Items comes out in the correct order since I put them out to console. I think that if I could group them into arrays of four the my code (so far) would give me the reslut I want.


    This is part of my own training in order to (hopfully) advance in the company I work for.

  19. #19
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Split string and joining substrings

    Dont get me wrong Norm I really appreciate the help you and others give me here..

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Split string and joining substrings

    Each item contains four attributes
    How does your program get these 4 attributes? Are they in strings? How many in each String? Does it vary?
    Are all 4 attributes always present?

Page 1 of 2 12 LastLast

Similar Threads

  1. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 PM
  2. String split
    By soccer_kid_6 in forum New To Java
    Replies: 3
    Last Post: 10-29-2010, 07:51 PM
  3. Replies: 9
    Last Post: 08-31-2010, 05:21 AM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Tags for this Thread

Posting Permissions

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