taking a character out of a string

BCS ao at pathlink.com
Mon Aug 18 15:59:27 PDT 2008


Reply to Michael P.,

> Okay, so what I'm trying to do is read in 2 phrases, then use 2
> foreach loops to check for spaces and remove them.
> 
> Example:
> 
> foreach( character; phraseOne )
> {
> if ( character == ' ' )
> {
> take the character out of the string ( and resize it??? )
> }
> }
> Also, is there anyway I can use only 1 foreach loop to do those 2
> things? I'm pretty sure when I repeat code like that there must be a
> better way.
> 
> And 1 thing about readln and readf: I realized that I can't use readf
> to read words with spaces. And when there is a space entered with
> readf, the other words stay in the input buffer.(input buffer is what
> it is, right?) Then when I use readln (maybe readf too, I didn't check
> that), it automatically enters what was left over. How do I flush the
> input buffer?
> 
> Thanks.
> 
> -Michael P.
> 

define your own function

char[] dumpSpace(char[]  phrase)
{
 phrase =  phrase.dup;
 int i = 0;
 foreach( character; phrase)
 {
  if ( character != ' ' )
    phrase[i++] = character;
 }
 return phrase[0..i];
}

phraseOne =  phraseOneTwo.dumpSpace;
phraseTwo =  phraseTwo.dumpSpace;




More information about the Digitalmars-d-learn mailing list