HatHelper
|
  |
 |
| Joined: 02 Mar 2009 |
| Total Posts: 46305 |
|
|
| 17 Jul 2011 10:08 PM |
a = "abc123456789"
How would I find if 'a' has string 'c1', and print the next 10 characters? |
|
|
| Report Abuse |
|
|
swmaniac
|
  |
| Joined: 28 Jun 2008 |
| Total Posts: 15773 |
|
|
| 17 Jul 2011 10:11 PM |
local start, end = string.find(a, "c1") string.sub(a, start, (end-start)+10) |
|
|
| Report Abuse |
|
|
L2000
|
  |
| Joined: 03 Apr 2008 |
| Total Posts: 77448 |
|
|
| 17 Jul 2011 10:12 PM |
I'd use 'for i = 1,#a do' and check if the current string is c and 1. If it is, then print the next 10 characters.
for i = 1,#a do if string.sub(a, i, i)=="c" then if string.sub(a, i+1, i+1)=="1"then print(string.sub(a, i+2, i+12)) end end end
|
|
|
| Report Abuse |
|
|
|
| 17 Jul 2011 10:13 PM |
for i=1, string.len(a) do if ((string.sub(a),i,i+1)=="c1") then print(string.sub(a),i+2) end end |
|
|
| Report Abuse |
|
|
HatHelper
|
  |
 |
| Joined: 02 Mar 2009 |
| Total Posts: 46305 |
|
|
| 17 Jul 2011 10:13 PM |
| L2000 that's extremely inefficient. It what I would have done a year ago :I |
|
|
| Report Abuse |
|
|
swmaniac
|
  |
| Joined: 28 Jun 2008 |
| Total Posts: 15773 |
|
|
| 17 Jul 2011 10:18 PM |
*Facepalm*
Excuse me, I need to reread the string functions part of the wiki.
local start, endf = string.find(a, "c1") string.sub(a, start, endf+10)
Would work correctly, I believe. |
|
|
| Report Abuse |
|
|
HatHelper
|
  |
 |
| Joined: 02 Mar 2009 |
| Total Posts: 46305 |
|
| |
|
|
| 17 Jul 2011 10:22 PM |
a = "abc123456789" print(a:match("c1(..........)"))
But this method will print nil if there aren't 10 chars in front. xP |
|
|
| Report Abuse |
|
|
|
| 17 Jul 2011 10:26 PM |
@swmanic
I completely forgot about string.find! Thank you for not only helping him but reminding me of a very useful string function XD. |
|
|
| Report Abuse |
|
|
HatHelper
|
  |
 |
| Joined: 02 Mar 2009 |
| Total Posts: 46305 |
|
|
| 17 Jul 2011 10:30 PM |
Thats even better ElectricAxel.
|
|
|
| Report Abuse |
|
|
HatHelper
|
  |
 |
| Joined: 02 Mar 2009 |
| Total Posts: 46305 |
|
|
| 17 Jul 2011 10:32 PM |
| I don't remember how string.gsub worked in the way I wanted. How do I remove all characters other than numbers from a string? |
|
|
| Report Abuse |
|
|
| |
|
|
| 17 Jul 2011 11:04 PM |
My bad, %D (capital meants NOT)
string.gsub(1, "%D", "") |
|
|
| Report Abuse |
|
|