View Full Version : tokenizing \n

10 October 2006, 04:32 PM
hey everyone,
i've got a string that goes "string\n" so that when i try to append 2 strings together, it makes the result be like


instead of "stringappended"

i've used the encodestring command to make the \n part of the string, and then by using

tokenize($encodedClass, "\\n", $buffer);

i can get the \n to be truncated and left with the string i want. the problem that i have now is that if the $encodedClass string has an "n" character in it, it also truncates that. does anyone know how i can successfully truncate the \n new line character while still leaving the plain old "n" characters in my strings?


10 October 2006, 05:47 PM
Try this:

string $text = "my text\n";
$text = `match "^[^(\r\n)]*" $text`;
print $text;

10 October 2006, 06:04 PM
You could also use "substring" command.

string $test="Hello\n";
$test=`substring $test 1 (size($test)-1)`;

$test will now have only "Hello";

10 October 2006, 06:04 PM
perfect blackhwk, that does exactly what i need. thanks so much.

could you explain exactly what the ^ character is doing? i read in the match command documentation but i'm afraid i don't totally get it. is ^(\r\n) searching for any characters that match either \r or \n?

10 October 2006, 05:42 PM
Here's a shorter version that does the same thing:

string $text = "my text\n";
string $newText = `match "[^\n]*" $text`;

"If the first character following the opening "[ " is a "^" any character not enclosed is matched."

The [^\n] is saying anything that's not \n gets passed through and the * at the end is telling it to search through the whole string. This match stuff can get confusing and I'm not sure if that explanation is totally accurate, but that's how I think of it in my head.

CGTalk Moderation
10 October 2006, 05:42 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.