How to check for and remove a newline (\n) in the first line of a text field in actionscript? - apache-flex

In the script, sometimes a newline is added in the beginning of the text field (am using a textArea in adobe flex 3), and later on that newline might need to be removed (after other text has been added). I was wondering how to check if there is a newline at the beginning of the text field and then how to remove it. Thanks in advance.

How about
private function lTrimTextArea(ta:TextArea) {
ta.text = ta.text.replace(/^\n*/,'');
}

To remove all line breaks from the start of a string, regardless of whether they are Windows (CRLF) or UNIX (LF only) line breaks, use:
ta.text = ta.text.replace(/^[\r\n]+/,'');
You should use + in the regex instead of * so that the regex only makes a replacement if there is actually a line break at the start of the string. If you use ^\n* as Robusto suggested the regex will find a zero-length match at the start of the string if the string does not start with a line break, and replace that with nothing. Replacing nothing with nothing is a waste of CPU cycles. It may not matter in this situation, but avoiding unintended zero-length matches is a very good habit when working with regular expressions. In other situations they will bite you.

If you particularly want to check first character here is solution:
if(ta.text.charAt(0) == "\n" || ta.text.charAt(0) == "\r")
{
ta.text.slice(1,ta.text.length-1);
}
slice method will slice that first character and gives your text from second character.

If you want to simply disable the ability to create a carriage return / new line, then all you need to do is disable multiline for that TextField...
(exampleTextField as TextField).multiline = false;
This will still trigger KEY_DOWN and KEY_UP events, however will not append the text with the carriage return.

Related

Move file pointer to line with a known string in Python

I'm reading from a data file and I know my data begins after a line that contains '[Data]'.
I would like to search through the file until it reaches this line and then stops so I can begin to format the data. This is my attempt:
fid='myData.dat'
f=open(fid,'r')
fline = 'string'
while fline != '[Data]':
fline=f.readline()
print fline
However, it reads through every line in the document without stopping. I know for a fact that the line I want only contains [Data] and no other spaces or characters.
I'm sure there is a better way of doing this and I am open to going about this in any other way.
Probably '[Data]' is followed by a '\n' because it's the end fo the line, so you may try while fline != '[Data]\n':
If you are using Windows the newline characters will be "\r\n", '\n' is for Unix based systems.
The readline method return line with line endings:
while fline != '[Data]\n':
fline=f.readline()
You can strip white characters:
while fline.strip() != '[Data]':
fline=f.readline()
If you want no space before "[", you could remove them only on right side with .rstrip(). You can remove only line break with .rstrip("\r\n").

Add a newline to a string in Scilab

Create a simple string in Scilab containing a newline.
Seems simple enough, but Scilab only seems to interpret escape sequences through printf style functions and msprintf / sprintf splits the string into a vector of strings at the newline!
The only way I can see to achieve this is to actually write a newline out to a file and read it back in again. Surely there is a simpler way to do this!
Ok, found it. The ascii function will do the job, a newline can be added via its ascii decimal -
str = 'hello' + ascii(10) + 'world'

TextPad: Find all the lines not starting with a pattern and replace with a back space

I want to introduce a backspace character at the beginning of the line where a particular pattern is not found. Please advise.
Thanks,
Sagar
If you mean that you want to "remove the first character" then you can do this:
1) Write your regex pattern of what you want to find. For example, if you want to match Remove me at the start of the line, use:
^R\(emove me\)
Here we use ^ to assert the position to the start of the string. We also capture everything apart from the string we wish to keep in a backreference so it can be used later.
2) Replace the matches we find with whatever we grabbed in our backreference, in this case emove me, in effect backspacing the first character.
3) Make sure regular expression is checked and the cursor is at the start of the file, and hit Replace All.
Before
After:

Removing the Default Wrap Character From all records

I am using BizTalk 2009 and I have a flat file that is similar to the following
"0162892172","TIM ","LastName ","760 "," ","COMANCHE ","LN "
"0143248282","GEORGE ","LastName ","625 "," ","ENID ","AVE "
When I parse it and start mapping it I need to get rid of the quotation marks. I have marked the Wrap Character attribute for the schema as a quotation mark but it doesn't remove it when BizTalk is parsing the file.
Is there an easy way to specify the removal of a wrap character or am I going to have to run it through a script functiod every time? Also I would like to be able to remove the trailing spaces as well, if at all possible.
If you're still seeing the quotes after parsing, it likely means you set the wrap character property incorrectly. Are you sure you also set Wrap Character Type == Character?
As for the extra spaces, those will be hard to get rid of during parsing, because the quotes would specifically tell bts that they were intentional, so yeah, your best bet is to probably remove those during mapping or whatever.
this page seems to suggest that removing the trailing spaces can be done with:
> Pad Character Type = Hexadecimal
> Pad Character = 0x20

Don't want spaces in the text, but this regex is passing not sure why

I am using the following regex
/[a-zA-Z0-9]+/i.test(value)
If I enter a space in the word, it passes.
I don't see where spaces are aloud in the regex, why is it passing?
You need to set the beginning and end bounderies so that the entire string must match the regular expression, otherwise it'll look for any match (which in this case is one or more of the characters specified).
Try this:
/^[a-zA-Z0-9]+$/i.test(value)
Because you haven't anchored it.
For these sorts of tests, it's typically safer to make sure you don't have the negated character class:
/[^a-zA-Z0-9]/

Resources