remove space in string

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
reelstuff
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 254
Joined: Mon Apr 16, 2007 12:06 am
Location: USA
Contact:

remove space in string

Post by reelstuff » Fri Dec 04, 2009 10:55 pm

What is the best method of removing unwanted space in a fld result

Code: Select all

 put fld "Field_baseURL" into myVar
   put fld "Field_keyCode" after myVar
this produces, a space between the two.

like this

a b

The result needs to be ab

any idea or suggestion is always good, thanks in advance.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9785
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: remove space in string

Post by dunbarx » Fri Dec 04, 2009 11:23 pm

I don't know if you have other information in the field that contains the two strings you want to put together.

If you do not, you can use the "replace" command:

replace space with empty in yourString.

If you do though, note that this will destroy all the spaces between all the other words.

So you have to target the strings you wish to concatenate:

put firstString & secondString into newString.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9785
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: remove space in string

Post by dunbarx » Fri Dec 04, 2009 11:25 pm

Looking at your example, it seems you must have a space after the first field, or before the second. You can always just delete those spaces, or better, try to find out why they are included when the field is populated.

reelstuff
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 254
Joined: Mon Apr 16, 2007 12:06 am
Location: USA
Contact:

Re: remove space in string

Post by reelstuff » Fri Dec 04, 2009 11:28 pm

Thanks for your reply, that gives me a couple of different ways to look at a solution.

reelstuff
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 254
Joined: Mon Apr 16, 2007 12:06 am
Location: USA
Contact:

Re: remove space in string

Post by reelstuff » Sat Dec 05, 2009 12:59 am

dunbarx wrote:Looking at your example, it seems you must have a space after the first field, or before the second. You can always just delete those spaces, or better, try to find out why they are included when the field is populated.
Good point, I have done that, apparently it is a result of decoding some encrypted text, so some other option is needed to get rid of that extra space.

Code: Select all

 put field "keytext" into kkey
   put the text of field "codet" into codetext
   put Blowfish_Decode_string(codetext, kkey) into field "decode"
for some reason this produces space at the end of the text in fld decode

Peter Wood
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 92
Joined: Mon Jul 06, 2009 4:53 am
Location: Bamboo River

Re: remove space in string

Post by Peter Wood » Sat Dec 05, 2009 5:58 am

You could use a simple "regex" to remove the last space at the end of the first string before adding the second string

Code: Select all

put the replaceText ("a    ", " $", "") into myVar   -- a is followed by four spaces
put "b" after myVar
put myVar

Result
a   b    -- only 3 spaces between a and b

reelstuff
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 254
Joined: Mon Apr 16, 2007 12:06 am
Location: USA
Contact:

Re: remove space in string

Post by reelstuff » Sat Dec 05, 2009 1:17 pm

Thank you for your post, I will take a look at that, at this point I am looking into the function, for some reason there is an iteration issue as well,

thanks for participating, all is appreciated.

T
i
m

dickey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 114
Joined: Wed Apr 08, 2009 11:54 pm
Location: Shellharbour, Australia

Re: remove space in string

Post by dickey » Sat Dec 05, 2009 2:22 pm

Hello realstuff,

Whether preceding or trailing spaces (spaces at the beginning or end of your variables, fields, or strings) the best way to trim them I know is code similar to this.

Code: Select all

  put word 1 to -1 of fld "theFirstField" into fld "theSecondField"
the string " realstuff rocks " will trim to "realstuff rocks";
the string "realstuff rocks " will trim to "realstuff rocks";
the string " realstuff rocks" will again trim to "realstuff rocks".

It will trim multiple whitespace chars like spacespace.

I hope that helps.

Kind regards, Andrew

reelstuff
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 254
Joined: Mon Apr 16, 2007 12:06 am
Location: USA
Contact:

Re: remove space in string

Post by reelstuff » Sat Dec 05, 2009 3:32 pm

dickey wrote:Hello realstuff,

Whether preceding or trailing spaces (spaces at the beginning or end of your variables, fields, or strings) the best way to trim them I know is code similar to this.

Code: Select all

  put word 1 to -1 of fld "theFirstField" into fld "theSecondField"
the string " realstuff rocks " will trim to "realstuff rocks";
the string "realstuff rocks " will trim to "realstuff rocks";
the string " realstuff rocks" will again trim to "realstuff rocks".

It will trim multiple whitespace chars like spacespace.

I hope that helps.

Kind regards, Andrew
Interesting, thanks for your post, very useful, as always all input is much appreciated, :)

I did manage to remove the space, by this manner, but it assumes that there is no intentional space,

such as sometimes may be present in a URL,

The main premiss would be to preserve the original intent of the input, i.e. http ://www.somewebsite.com/somedirectory/some file.php

instead we see a six character space, which may indeed be related to the iteration of the encoding,

which results in this,

http ://www.somewebsite.com/somedirectory/some file.php

So working in the function, may be the best way but that is sadly my weak spot,

here is example,

Code: Select all

function Blowfish_decode_string   t, key
   # Given the codetext message t and the decoding/encoding key, the original plaintext is reconstructed
   
   If Blowfish_lastkey <> key then Blowfish_Init  key -- caller wants a change of keys, so re-initialize the S[] and PP[] arrays
   
   put the length of t into tle -- how many character bytes does t hold?
   
   # Determine how many round of decoding need to be done
   put tle div 16 into full -- how many chunks of 16-byte hex codes are to be processed
   put tle mod 16 into partly -- rest of the character bytes
   put full - 1 into final
   if partly > 0 then put full into final
   
   put "" into rresultat -- rresultat will hold the plaintext message
   
   repeat with block = 0 to final
      put block * 16 into tle
      put (character tle +1 to tle + 8 of t) into inter -- intermediate storage
      put hex_to_decimal(inter) into dl
      put (character tle + 9 to tle + 16 of t) into inter
      put hex_to_decimal(inter) into dr
      put BFdec(dl, dr) into inter -- decoded plaintext
      
      put inter[0] into dl
      put inter[1] into dr
      put decimal_to_four_letters(dl) into dl
      put decimal_to_four_letters(dr) into dr
      
      put rresultat & dl & dr into rresultat
   end repeat
   
   return rresultat   
end Blowfish_decode_string

Peter Wood
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 92
Joined: Mon Jul 06, 2009 4:53 am
Location: Bamboo River

Re: remove space in string

Post by Peter Wood » Sat Dec 05, 2009 5:35 pm

The additional characters at the end of the decrypted plain text would seem to stem from the padding characters that will have been added to the source plain text. I couldn't see where you remove any padding in your code.

The most common padding method uses the following pattern (all in hexadecimal)

Hex "01" - 1 byte of padding
Hex "0202" - 2 bytes of padding
.
.
Hex "07070707070707" - 7 bytes of padding.

Hope this helps.

Peter

reelstuff
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 254
Joined: Mon Apr 16, 2007 12:06 am
Location: USA
Contact:

Re: remove space in string

Post by reelstuff » Sat Dec 05, 2009 6:07 pm

Peter Wood wrote:The additional characters at the end of the decrypted plain text would seem to stem from the padding characters that will have been added to the source plain text. I couldn't see where you remove any padding in your code.

The most common padding method uses the following pattern (all in hexadecimal)

Hex "01" - 1 byte of padding
Hex "0202" - 2 bytes of padding
.
.
Hex "07070707070707" - 7 bytes of padding.

Hope this helps.

Peter
Thank you, yes, I can see, that at least I think I do, so I need to examine the Hex values,

But just for the record, this is code that was downloaded from the Run Rev resource center, so I cant claim authorship, but some day when I grow up... :)

ukimiku
Posts: 101
Joined: Thu Oct 22, 2009 10:45 am

Re: remove space in string

Post by ukimiku » Sun Dec 06, 2009 9:55 pm

Hi Reelstuff,

the Blowfish code was written by me. If the length of your plaintext (the text that your are encoding) is not a precise multiple of 8, space characters are appended at the end so as to make the length a multiple of 8. If you want to encode a text and don't want additional appended space characters, save the original length of the original plaintext string and after decrypting, you know how many spaces have to be eliminated from the reconstructed plaintext.

You could prepend the length of the plaintext as a string (followed by a letter, say "a", to set the length apart from the plaintext) to the plaintext message and encrpt the whole thing. Try something like this:

Encoding:

Code: Select all

   put "Hello" into tPlain
   put ("" & the length of tPlain) into lenPlaintext
   put lenPlaintext & "a" & tPlain into tPlain
   put Blowfish_encode_string(tPlain, "somekey") into tCiphertext
   
   answer "Hello" & return & tPlain & return & tCiphertext
   put "" into tPlain
Decoding:

Code: Select all

 put "" into tPlain -- forget the old plaintext
put Blowfish_decode_string(tCiphertext, "somekey") into tPlain
put matchChunk(tPlain, "(a)", firstOccurence, lastOccurence) into pFound
put (char 1 to  (firstOccurence - 1)  of tPlain) * 1 into lenPlaintext
put (char (firstOccurence + 1) to the length of tPlain of tPlain) into tPlain
if the length of tPlain > lenPlaintext then put (char 1 to lenPlaintext of tPlain) into tPlain

answer tCiphertext & return & lenPlaintext & return & tPlain
Of course, I could have implemented that into the library, but I wanted it to stay compatible with other Blowfish libraries so that you can use the ciphertext and have a Javascript decode it, and vice versa...

Be aware, though, that if an attacker knew of this particular format (length + "a"+ plaintext), s/he could exploit that, and your encryption would be significantly weaker.

Does this help?

Regards,

ukimiku
Posts: 101
Joined: Thu Oct 22, 2009 10:45 am

Re: remove space in string

Post by ukimiku » Mon Dec 07, 2009 3:39 pm

In addition, I think that you, the programming user of the Blowfish library, should take provisions so as to make the length of the string to be encoded a precise multiple of 8. This way, you know what there will be as padding bytes, and can react accordingly. This way, the security of the Blowfish algorithm would not be compromised.

Regards

reelstuff
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 254
Joined: Mon Apr 16, 2007 12:06 am
Location: USA
Contact:

Re: remove space in string

Post by reelstuff » Tue Dec 08, 2009 1:22 pm

Thank you for taking time to post, I will have to study it for a time to completely understand it, but I certainly appreciate your taking time
to reply, I am sure it helped a lot of others besides me that were following along.

I was using a different library, but it kept appending errors in the text, so far your code has been excellent in that regard.

thanks again for your reply.

Tim
:)

ukimiku
Posts: 101
Joined: Thu Oct 22, 2009 10:45 am

Re: remove space in string

Post by ukimiku » Tue Dec 08, 2009 6:08 pm

Tim, please don't hesitate to ask if you should need help. If you're new to Runtime Revolution, you will probably need a lot of input from the forum, just as I received it when I got here. I think you will find that most (if not all) of the folks here are very friendly and helpful.

That said, what can I help you with? Should be easy enough, especially since I wrote the code... :)

I really wouldn't prepend the length of the plaintext to the plaintext and then encrypt. It will definitely weaken the encryption. Why not store the length in a variable and save that somewhere close, say, in the same file, or transmit it independently?

Kind regards,

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”