Bob,
I think your suggestion achieves the same thing. However, isn't the
overhead for tokenizing a whole string a lot higher that splitting at
just the insert location?
Jonathan
On Jun 26, 2009, at 9:34 AM, Robert Hanson wrote:
> Well, what you are doing is adding "tokens", so there is a context
> for this. It does sound a bit specialized, but we could consider
> it. You can do it with:
>
> x = "this is a test"
> token = "now"
> print (x.split(" ") + token).join(" ")
>
> #or, to insert:
>
> y = x.split(" ")
> print (y[1][2] + token + y[3][0]).join(" ")
>
> The idea there is to "tokenize" on space, make the addition (which
> is in Jmol script an array append operation), then recreate the
> string by joining with space again.
>
> This would work with newline characters as well, "\n"
>
> Bob
>
>
> On Wed, Jun 24, 2009 at 11:16 PM, Jonathan Gutow <[email protected]>
> wrote:
> Because I'm generating strings that require inserting and appending
> words, I keep sticking in little pieces of code to make sure the
> white space is correct. Thus I propose two versions of a utility
> function that would do this and check the white space (see below).
> The first version will append the word to the end (could call it
> append word). The second will insert it at the specified position.
>
> Any thoughts?
> Jonathan
>
>
> String insertWord(String oldstr, String word){
> if (oldstr.length() > 0 && !oldstr.endsWith(" "))
> oldstr += " ";
> oldstr+=word;
> return oldstr;
> }
>
> String insertWord(String oldstr, String word, int insertpt){
> if (oldstr.length() == 0) {
> oldstr = word;
> return oldstr;
> }
> String part1 = oldstr.substring(0, insertpt);//may have my
> indexes
> messed up...need to check
> String part2 = oldstr.substring((insertpt+1),oldstr.length());
> if (!part1.endsWith(" "))
> part1 += " ";
> if (!part2.beginsWith(" "))
> part2 = " "+part2;
> part1+=word;
> oldstr = part1 + part2
> return oldstr;
> }
>
>
> Dr. Jonathan H. Gutow
> Chemistry Department [email protected]
> UW-Oshkosh Office:
> 920-424-1326
> 800 Algoma Boulevard FAX:920-424-2042
> Oshkosh, WI 54901
> http://www.uwosh.edu/faculty_staff/gutow/
>
>
>
>
>
> ----------------------------------------------------------------------
> --------
> _______________________________________________
> Jmol-developers mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/jmol-developers
>
>
>
> --
> Robert M. Hanson
> Professor of Chemistry
> St. Olaf College
> 1520 St. Olaf Ave.
> Northfield, MN 55057
> http://www.stolaf.edu/people/hansonr
> phone: 507-786-3107
>
>
> If nature does not answer first what we want,
> it is better to take what answer we get.
>
> -- Josiah Willard Gibbs, Lecture XXX, Monday, February 5, 1900
> ----------------------------------------------------------------------
> --------
> _______________________________________________
> Jmol-developers mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/jmol-developers
Dr. Jonathan H. Gutow
Chemistry Department [email protected]
UW-Oshkosh Office:920-424-1326
800 Algoma Boulevard FAX:920-424-2042
Oshkosh, WI 54901
http://www.uwosh.edu/faculty_staff/gutow/
------------------------------------------------------------------------------
_______________________________________________
Jmol-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jmol-developers