Re: Character strings

From: Jonathan Vigh <vigh_at_nyahnyahspammersnyahnyah>
Date: Thu, 09 Jul 2009 16:52:01 +0000

Hi James,
   The new NCL (5.1.1) has some great string functions, but I don't
think any of them will remove ALL the whitespace in a string
(str_squeeze replaces multiple whitespaces with a single whitespace
character). But the function below should remove all non-whitespace,
tab, and null characters.

Jonathan

;----------------------------------------------------------------------
; This function removes all white space from a string.
;
; Author: Jonathan Vigh
;----------------------------------------------------------------------
undef("remove_whitespace")
function remove_whitespace(tStr[1]:string)
local space,tab,nullChar,tStr,tChr,nchars,ic,ix,output_Chr,output

begin
  space = inttochar(32)
  tab = inttochar(9)
  nullChar = inttochar(0)

  tChr = stringtochar(tStr)
  nchars = dimsizes(tChr)-1

  output_Chr = new(nchars+1,"character",character_FillValue)
  ix = 0
  do ic = 0, nchars-1
     if ( (tChr(ic) .ne. space) .and. (tChr(ic) .ne. tab) .and.
(tChr(ic) .ne. nullChar) ) then
        output_Chr(ix) = tChr(ic)
    ix = ix + 1
     end if
  end do

  output = chartostring(output_Chr)
    
  return(output)
end

Correia, James wrote:
> Is there a function which can trim white space out of a character string?
>
> For example I am reading in a file with model abbreviations and ensemble
> numbers like this:
> aom_1
> cgcm31t47_1
>
> Then using the names to construct a file name.
>
> However I am using asciiread to read the data string back into ncl and then
> use the array to open a new file. The above model names are read in using 10
> ascii elements to accommodate the whole list.
>
> So the file name ends up looking like:
> aom_1 _y8.dat
> Rather than
> aom_1_y8.dat
>
> So can the character string be trimmed down to non white space characters?
>
> I am using ncl v5.1.0
>
> jimmyc
>
>
> James Correia Jr., PhD
> Climate Physics Group
> Post. Doc.
> Pacific Northwest National Lab
>
> "Wisdom. Strength. Courage. Generosity. Each of us are born with one of
> these. It is up to us to find the other three inside of us."
> -Into the West
>
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu Jul 09 2009 - 10:52:01 MDT

This archive was generated by hypermail 2.2.0 : Mon Jul 13 2009 - 20:56:19 MDT