Results 1 to 4 of 4
Thread: Get a certain row in a string.
- 12-23-2008, 10:53 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 8
- Rep Power
- 0
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?
- 12-23-2008, 11:36 PM #2
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:
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.Java Code:String s = "abcdefgh\nijklmno\npqrstuvwxyz";
String API (methods):
String (Java Platform SE 6)
Regular Expressions:
Introduction (The Java™ Tutorials > Essential Classes > Regular Expressions)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
-
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)); }
- 12-25-2008, 12:18 AM #4
Member
- Join Date
- Dec 2008
- Posts
- 8
- Rep Power
- 0
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:
But, well... It just seems like an ugly solution. Isn't there any better way to do this?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; } }
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
-
Error: cannot resolve symbol' on Person (java.lang.String, java.lang.String)
By baltimore in forum New To JavaReplies: 2Last Post: 09-18-2008, 07:30 AM -
string vs string builder??
By j2vdk in forum New To JavaReplies: 6Last Post: 09-08-2008, 09:38 AM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM -
Help with insertName(String name) and deleteName(String name)
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:29 AM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks