This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Techman749's profile picture

How to read the contents of a string and pick out certain characters

Started by Techman749, 09 November 2014 - 09:02 PM
Techman749 #1
Posted 09 November 2014 - 10:02 PM
Hello, ComputerCraft Community!

I've been writing the Settings/Control panel app for my OS recently, and I've been working on the user accounts part of the app, and I'm having a bit of a problem.

I'm wanting to make the program take what ever the user enters from a text box, and make it look for certain characters. The reason why I'm wanting to do this is so I don't have users putting spaces in their passwords or usernames, because the API I'm using panics if it finds a space.

Here is the code I have so far:

local userName = program:GetObject("EnterUsernameBox").Text
local passWord = program:GetObject("EnterPasswordBox").Text
if passWord == "" then
		program:DisplayAlertWindow('Password can not be blank!', 'You must enter a password!', {'Ok'}, function(value)end)
end

In summary, I'm wanting to know how to make the program look at the string the user gave it and if it finds, a space or what ever character I want, I want it to tell the user it can't use a space.

If you need more info, or code, just ask!

Thanks!
valithor #2
Posted 09 November 2014 - 10:14 PM
Hello, ComputerCraft Community!

I've been writing the Settings/Control panel app for my OS recently, and I've been working on the user accounts part of the app, and I'm having a bit of a problem.

I'm wanting to make the program take what ever the user enters from a text box, and make it look for certain characters. The reason why I'm wanting to do this is so I don't have users putting spaces in their passwords or usernames, because the API I'm using panics if it finds a space.

Here is the code I have so far:

local userName = program:GetObject("EnterUsernameBox").Text
local passWord = program:GetObject("EnterPasswordBox").Text
if passWord == "" then
		program:DisplayAlertWindow('Password can not be blank!', 'You must enter a password!', {'Ok'}, function(value)end)
end

In summary, I'm wanting to know how to make the program look at the string the user gave it and if it finds, a space or what ever character I want, I want it to tell the user it can't use a space.

If you need more info, or code, just ask!

Thanks!

If you just want to see if a string has a space in it you could use something like this

tstring = "this is a string"
if string.match(tstring," ") then
  print("yes")
end

That will check the string for a single space, and then print yes if there is one or print nothing if there is not one.

This is the first way that I thought of there is probably a better way to do it.
Dragon53535 #3
Posted 10 November 2014 - 07:53 AM
If you just want to see if a string has a space in it you could use something like this

tstring = "this is a string"
if string.match(tstring," ") then
  print("yes")
end

That will check the string for a single space, and then print yes if there is one or print nothing if there is not one.

This is the first way that I thought of there is probably a better way to do it.

That's not the first way i would do it, however it is the more efficient of the two. Soooooo I'll write a tiny function and if statement for him finding spaces and other characters!

local function findChar(str,char)
  if string.match(str,char) then
	return true
  else
	return false
end

--#And then after getting passwords/usernames, just do a check!

if findChar(userName," ") or findChar(userName,"!") then --# You can expand this more
  print("Oh no you can't use those characters!")
end
Edited on 10 November 2014 - 10:30 AM
theoriginalbit #4
Posted 10 November 2014 - 10:55 AM
That's not the first way i would do it, however it is the more efficient of the two. Soooooo I'll write a tiny function and if statement for him finding spaces and other characters!

local function findChar(str,char)
  if string.match(str,char)
	return true
  else
	return false
end

--#And then after getting passwords/usernames, just do a check!

if findChar(userName," ") or findChar(userName,"!") then --# You can expand this more
  print("Oh no you can't use those characters!")
end
firstly, please test all code you post, if the advice is buggy it's not useful to the poster!

secondly, an optimisation would be to do the following

local function findChar(str, char)
  return str:match(char) ~= nil
end

However a simpler method would be the following

local disallowedChars = {" ", ":", "!"} --# a table of characters that aren't allowed

local function allowedInput(input)
  for i,char in pairs(disallowedChars) do --# go through each disallowed character
	if input:match(char) then --# check for the character
	  return false --# the character is present, reject the input
	end
  end
  return true --# no disallowed characters present, valid input
end

local input
repeat
  write("Enter password: ")
  input = read()
until allowedInput(input) --# repeat until the input is valid
Edited on 10 November 2014 - 09:56 AM