Results 1 to 4 of 4
  1. #1
    BeholdMyGlory is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default Get a certain row in a string.

    Say I have a string, for example:

    String s = "abcdefgh\n" +
    "ijklmno\n" +
    "pqrstuvwxyz";

    and I know the character position in the string, how can i get the specific row where that character is, and store it in a string?
    For example, say I know that the character is at position 13 (the 'm'), how can I get the second row?

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Create other variables

    Actually, from a string variable point of view, a string is just a string. It does know about rows. The "/n" is just another part of the string. If you lay it out, it would look like:
    Java Code:
    String s = "abcdefgh\nijklmno\npqrstuvwxyz";
    You can use various combinations of string methods (indexOf, substring, etc) to obtain what you want, using "/n" as a token/separator. Also, you may want to investigate regular expressions (regex), but I have no experience with that.
    String API (methods):
    String (Java Platform SE 6)
    Regular Expressions:
    Introduction (The Java™ Tutorials > Essential Classes > Regular Expressions)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You could always split your String into an array of Strings with each item in the array being a row. To do this, simply use the newline char as your regex for splitting. Don't forget to use double backslashes or it won't work:
    Java Code:
      public static void main(String[] args)
      {
        String s = "abcdefgh\n" +
        "ijklmno\n" +
        "pqrstuvwxyz";
        
        String[] rows = s.split("\\n");
        System.out.println(Arrays.toString(rows));
      }

  4. #4
    BeholdMyGlory is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You could always split your String into an array of Strings with each item in the array being a row. To do this, simply use the newline char as your regex for splitting. Don't forget to use double backslashes or it won't work:
    Java Code:
      public static void main(String[] args)
      {
        String s = "abcdefgh\n" +
        "ijklmno\n" +
        "pqrstuvwxyz";
        
        String[] rows = s.split("\\n");
        System.out.println(Arrays.toString(rows));
      }
    Yes, I'm aware of how the split function works, but the problem is, I need to pick out exactly what row the character whose position is n is on.
    I guess I could do something like:

    Java Code:
        String s = "abcdefgh\nijklmno\npqrstuvwxyz";
        int charPos = 15;
        String[] sArr = s.split("\\n");
        int row;
        for(int i=0;i<sArr.length;i++) {
          int count = 0;
          for(int j=0;j<=i;j++) { //loop through all rows before and including this one, checking the sum of their length
            count+=sArr[j].length+1; //the +1 compensating for the \n
          }
          count--; //Character position starts at 0, length starts at 1
          if(count>=charPos) { //if we've went past the position of the character we want, we know that it must be located on the current row
            row = i;
            break;
          }
        }
    But, well... It just seems like an ugly solution. Isn't there any better way to do this?
    Also, I haven't tested the code, maybe it works, maybe not. Will have to try.
    Last edited by BeholdMyGlory; 12-25-2008 at 12:23 AM.

Similar Threads

  1. Replies: 2
    Last Post: 09-18-2008, 07:30 AM
  2. string vs string builder??
    By j2vdk in forum New To Java
    Replies: 6
    Last Post: 09-08-2008, 09:38 AM
  3. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  4. Replies: 1
    Last Post: 08-07-2007, 07:29 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

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