Loading...
Logo
Processing Forum
Hi, i have  a big text file, and i need to select from that text file only words that are between specific characteres, lets say : ";;" and ";"

so, for example , if i have this in my txt file :


kjhas lkjaslk  slks ;;agge ; lkjqwe lkjjk tal ws ;;qv ; jhr


i need to select "agge" and "qv" because both words are between ";;" and ";"



Which is the easiest way of doing this in processing?


thanks



Telmo

Replies(4)



hello

there are possibly different approaches.

One could be:

do a split() at ;;

you get a resulting array A

Now for-loop over A with a var i
and use indexOf to determine the pos P of ; in each member of A (which is A[i] )
now use subString to get the start of A[i] to the ; and put into a String s
Copy code
  1. String s = A[i].subString (0,P);

Now you can store s in an ArrayList B using add
http://www.openprocessing.org/sketch/81729

see reference for all used commands

see the list here :
http://processing.org/reference/String.html

not tested

Greetings, Chrisir      


Hi, i been trying to code this following  Chrisir instructions .

I got it pretty much working but the problem i have is that when use " indexOf(";"); "  in an array item that doesnt has the ";" i get as result -1.


And when i apply (-1) to subString i get an  error.



How can i fix this? 
Is there any way of discard the items in my array that doesnt has the ";"



any idea?


Copy code


  1. String men = "kjhas lkjaslk  slks ;;agge ; lkjqwe lkjjk tal ws ;;qv  ; jhr";
  2. String[] list = split(men, ";;");
  3. println(list);

  4. String s;
  5.  
  6. int p;
  7.  
  8. ArrayList<String> o = new ArrayList();


  9. for (int i = 0; i < 3 ; i++)
  10. {

  11.   p = list[i].indexOf(";");
  12.   println(p);
  13.   
  14.   s = list[i].substring (0,p );
  15.   o.add(s);
  16.   

  17. }

  18. println(o);





well done!    



here...

list.length gives you the length of the array you get by using split()

Copy code

  1. String men = "kjhas lkjaslk  slks ;;agge ; lkjqwe lkjjk tal ws ;;qv  ; jhr";
  2. String[] list = split(men, ";;");
  3. println(list);

  4. String s;
  5.  
  6. int p;
  7.  
  8. ArrayList<String> o = new ArrayList();

  9. for (int i = 0; i < list.length ; i++)
  10. {
  11.   s="";   
  12.   p = list[i].indexOf(";");
  13.   println(p);
  14.   if (p>-1) {
  15.       s = list[i].substring (0,p );
  16.       o.add(s);
  17.   }
  18. }
  19. println(o);


[0] "kjhas lkjaslk  slks "
[1] "agge ; lkjqwe lkjjk tal ws "
[2] "qv  ; jhr"
-1
5
4
[agge , qv  ]