We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Substrings..? (Read 930 times)
Substrings..?
Oct 13th, 2009, 12:04pm
 
Can someone help me understand this function please? I have a string that is made of 4 digita, and I want to name a variable for the first, and another for the last pairs of numbers. This is the example in Visualising Information, but I do not really understand how using (0,2) gives "CC" as I am trying to use
'   String minx = x.substring(1,3);  ' to select the third and fourth digits in the string.

Clear this up for me please! Thanks, L.

String str1 = "CCCP";
String str2 = "Rabbit";
String ss1 = str1.substring(2);     // Returns "CP"
String ss2 = str2.substring(3);     // Returns "bit"
String ss3 = str1.substring(0, 2);  // Returns "CC"
println(ss1 + ":" + ss2 + ":" + ss3);  // Prints 'CP:bit:CC'
Re: Substrings..?
Reply #1 - Oct 13th, 2009, 1:04pm
 
"The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex."
Indices in Java start at 0.
So  goes from the first character, included, to the 3rd (0, 1, 2), excluded.
To get the last two characters, you have to use the other version: "The substring begins with the character at the specified index and extends to the end of this string." so use (2) (or if you prefer, (2, 4))
Re: Substrings..?
Reply #2 - Oct 13th, 2009, 1:04pm
 
they are numbered like this

Code:
0 1 2 3
C C C P


so when using substring with only one digit, like str1.substring(2); it is like : get string from 2 till end, in this case that is C and P.
when using 2 digits it is "get string from to but without 2" in this case 0 and 1 what is CC

thats actually what you need for your example as well.
Re: Substrings..?
Reply #3 - Oct 13th, 2009, 2:25pm
 
Ok that seems prettly clear, but when I plug it in I get an error saying 'string index out of range:4'... not sure what it doesnt like about it.

This is a selection of the data i am importing (4 digits so should work!)

1441      pm          sym      
1551      pm          sym
2332      pm          sym
1818      pm              rep
2002      pm          sym
Re: Substrings..?
Reply #4 - Oct 13th, 2009, 2:47pm
 
if one line is your whole string you have to end it by saying str1.substring(2,4) instead of just str1.substring(2)
Re: Substrings..?
Reply #5 - Oct 13th, 2009, 2:57pm
 
Hmm, still no, could it be a factor that some of the strings start with a 0? eg 0123..i thought that woulnt matter with strings rather than integers..

my whole code is here( the table class is pasted at the end because i was having trouble making it work another way:

PImage blueImage;
Table dataTable;
int rowCount;

                   

void setup(){
 size (200, 181);
 blueImage = loadImage("blueImage.png");
 dataTable = new Table("testnums.txt");
 rowCount = dataTable.getRowCount();
}
 
 
 
 
void draw() {
 background(255);
 image(blueImage, 0, 0);
 smooth();
 fill(192,0,0);
 noStroke();

for(int row = 1; row < rowCount; row++) {
 String time = dataTable.getString(row, 0); // column 1.
 String daytime = dataTable.getString(row, 1); // column 2.
 String kind = dataTable.getString(row, 2);//column3.
 String hourx = time.substring(2,4);
 
 
 // ellipse(x, y, 9, 9);
 
 
 //textFont("Helvetica.dfont", 32);
 //fill(51);
 //text("time");
 
 println(time);
 println(daytime);
 println(kind);
 println(hourx);
}
}




class Table {
 String[][] data;
 int rowCount;
 
 
 Table() {
   data = new String[10][10];
 }

 
 Table(String filename) {
   String[] rows = loadStrings(filename);
   data = new String[rows.length][];
   
   for (int i = 0; i < rows.length; i++) {
     if (trim(rows[i]).length() == 0) {
       continue; // skip empty rows
     }
     if (rows[i].startsWith("#")) {
       continue;  // skip comment lines
     }
     
     // split the row on the tabs
     String[] pieces = split(rows[i], TAB);
     // copy to the table array
     data[rowCount] = pieces;
     rowCount++;
     
     // this could be done in one fell swoop via:
     //data[rowCount++] = split(rows[i], TAB);
   }
   // resize the 'data' array as necessary
   data = (String[][]) subset(data, 0, rowCount);
 }


 int getRowCount() {
   return rowCount;
 }
 
 
 // find a row by its name, returns -1 if no row found
 int getRowIndex(String name) {
   for (int i = 0; i < rowCount; i++) {
     if (data[i][0].equals(name)) {
       return i;
     }
   }
   println("No row named '" + name + "' was found");
   return -1;
 }
 
 
 String getRowName(int row) {
   return getString(row, 0);
 }


 String getString(int rowIndex, int column) {
   return data[rowIndex][column];
 }

 
 String getString(String rowName, int column) {
   return getString(getRowIndex(rowName), column);
 }

 
 int getInt(String rowName, int column) {
   return parseInt(getString(rowName, column));
 }

 
 int getInt(int rowIndex, int column) {
   return parseInt(getString(rowIndex, column));
 }

 
 float getFloat(String rowName, int column) {
   return parseFloat(getString(rowName, column));
 }

 
 float getFloat(int rowIndex, int column) {
   return parseFloat(getString(rowIndex, column));
 }
 
 
 void setRowName(int row, String what) {
   data[row][0] = what;
 }


 void setString(int rowIndex, int column, String what) {
   data[rowIndex][column] = what;
 }

 
 void setString(String rowName, int column, String what) {
   int rowIndex = getRowIndex(rowName);
   data[rowIndex][column] = what;
 }

 
 void setInt(int rowIndex, int column, int what) {
   data[rowIndex][column] = str(what);
 }

 
 void setInt(String rowName, int column, int what) {
   int rowIndex = getRowIndex(rowName);
   data[rowIndex][column] = str(what);
 }

 
 void setFloat(int rowIndex, int column, float what) {
   data[rowIndex][column] = str(what);
 }


 void setFloat(String rowName, int column, float what) {
   int rowIndex = getRowIndex(rowName);
   data[rowIndex][column] = str(what);
 }
 
 
 // Write this table as a TSV file
 void write(PrintWriter writer) {
   for (int i = 0; i < rowCount; i++) {
     for (int j = 0; j < data[i].length; j++) {
       if (j != 0) {
         writer.print(TAB);
       }
       if (data[i][j] != null) {
         writer.print(data[i][j]);
       }
     }
     writer.println();
   }
   writer.flush();
 }
}
Re: Substrings..?
Reply #6 - Oct 13th, 2009, 3:51pm
 
works here...
Quote:
String test = "0148";
println(test.substring(0,2));
println(test.substring(2,4));
Re: Substrings..?
Reply #7 - Oct 13th, 2009, 4:08pm
 
Hmm, your example works for me too, but still nothing for mine. Will have a play around with it. Thanks, L.
Re: Substrings..?
Reply #8 - Oct 14th, 2009, 1:08am
 
Do a println of the data you want to process, to ensure it is what you expect to be.
Re: Substrings..?
Reply #9 - Oct 14th, 2009, 3:55am
 
Well I got around it by adding a '0' to the start o all of the numerical data in my table, then changing the substrings to go from (3,5) etc, which seems to have sorted it for some reason!(?)

Thanks again for your help.
Page Index Toggle Pages: 1