SRL Archive - Help and Support

a procedure to swap spaces with underscores


Shuttleu

2008-09-02 14:47:27

i was wondering if there was a procedure or function that could swap spaces with underscores in a bit of text that you have put in

example
i have a function called SwapSpaces which will switch the spaces for underscores and i use it like this
Text:= SwapSpaces('hello i own u');
and then text would equal hello_i_own_u

if you dont understand i will try to explain more clearly

~shut

Hugolord

2008-09-02 14:55:08

I don't understand, why do you need this?

Shuttleu

2008-09-02 14:58:55

i need it for a little project for my brother

i am creating a script and it is nearly finished but if the user puts in text with spaces then hte script will mess up

and as he said he might release it he cnat be bothered to tell everyone to use underscores instead of spaces :)

if there isnt one how hard would it be to create one because i have noidea how to do it :s

~shut

Narcle

2008-09-02 15:00:35


function SwapSpaces(str: string): string;
var
i, L: integer;
begin
L := length(str);
for i := 1 to L do
if str[i] = ' ' then
result := result + '_'
else
result := result + str[i];
end;

Nava2

2008-09-02 15:01:59

function SwapSpaces(Text : String): String;

var
P : Integer;

begin
Result := Text;
repeat
P := Pos(' ', Result);
if P = 0 then
Break;
Delete(Result, P, 1);
Insert('_', Result, P);
until false;
end;



begin
Writeln(SwapSpaces('Why hello there my good friend!'));
end.

Like that, champ? :)

Test it, it works!

E: Narcle beat me too it, but mine will be faster as its run off of delphi.

Shuttleu

2008-09-02 15:03:39

From: Narcle

function SwapSpaces(str: string): string;
var
i, L: integer;
begin
L := length(str);
for i := 1 to L do
if str[i] = ' ' then
result := result + '_'
else
result := result + str[i];
end;

ty i didnt expect it to be that simple :p
rep++

~shut

nielsie95

2008-09-03 09:48:58


begin
Writeln(Replace('Hello this is a test', ' ', '_'));
end.

Narcle

2008-09-03 10:02:38

From: nielsie95

begin
Writeln(Replace('Hello this is a test', ' ', '_'));
end.


Yeah… after I posted what I had I'm like… wait isn't there a "Replace" function. lol

But didn't bother posting back.

Waddo

2008-09-03 17:49:42

lol i was gunna say that i thought someone might have said that.

Daniel

2008-09-05 21:55:15

Ever heard of ReplaceRegex?

begin
Writeln(ReplaceRegex('This is the sample text', ' ', '_'));
end.

?

bullzeye95

2008-09-05 23:41:40

[QUOTE=Dan's The Man;460937]Ever heard of ReplaceRegex?

[scar]begin
Writeln(ReplaceRegex('This is the sample text', ' ', '_'));
end.

?

Yes, but regex isn't needed here.

Daniel

2008-09-06 02:54:50

From: bullzeye95
Yes, but regex isn't needed here.

It pretty much does the same thing <_>

nielsie95

2008-09-06 03:55:25

That's why it isn't needed.

Daniel

2008-09-06 04:07:15

From: nielsie95
That's why it isn't needed.

What's the difference?

nielsie95

2008-09-06 04:36:54

Speed.

Daniel

2008-09-06 05:44:56

From: nielsie95
Speed.

lol.

Nava2

2008-09-06 12:42:58

Spam.

He is finished being helped :)

Bobarkinator

2008-09-07 22:54:52

Why would you use ReplaceRegex when no regex is involved?

Daniel

2008-09-09 02:57:36

From: Bobarkinator
Why would you use ReplaceRegex when no regex is involved?

I don't even know what a regex is :p But it does the same thing as the original user requested ;)

Wizzup?

2008-09-09 03:05:57

[QUOTE=Dan's The Man;462676]I don't even know what a regex is :p But it does the same thing as the original user requested ;)

Regular Expression.

Bobarkinator

2008-09-09 08:38:51

It's for when you might have several strings and you want them to match certain cases.