Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing a string for imbedded blanks 1

Status
Not open for further replies.

RPrinceton

Programmer
Jan 8, 2003
86
US
Hi All,
Let's say a field contains "abc def". I want to test this field for imbedded blanks...in this case the blank is contained in position 4. I suppose I could use a "for" construct, but I suspect in Perl there is an easier method. Please advise. Thx in advance.
Regards,
Randall Princeton
 
the index function is your friend
Code:
if (index $mystr, " " != -1) {
  print "space contained in $mystr";
}

--Paul

cigless ...
 
Or use a regex:
Code:
$mystr =~ /\s+/ && print "space contained in $mystr";
 
Ruby:
Code:
mystr = 'abc def'
puts "Space contained in mystr" if mystr =~ /\s/
 
If by 'blanks', you mean ordinary spaces, PaulTEG's code will do that. If you also want to take into account other 'blanks' such as tabs and newline characters, go with mikevh's, since I suspect that you're probably unwilling to break into Ruby in mid-program.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top