String length

Posted By: zazang

String length - 04/03/08 10:43

Hi

I have an inkey code which accepts a name typed by a user into a string of length 30.Lets say I type a name of 20 characters and store it in this string.
Then this string becomes 20 charcaters long automatically which is very annoying because I want it to retain its original length of 30 characters.

In short is there a way to dynamically resize a string to a certain length ?

regards
zazang

Posted By: Excessus

Re: String length - 04/03/08 10:50

Yes, with str_cpy() or str_cat().
Posted By: zazang

Re: String length - 04/03/08 11:01

Infact its the str_cpy function that is shortening my string.Its really annoying because it was much better in c-script.

I tried to use str_cat to resize it by appending blank spaces but it does not help.

This is the resize code I used :-

var length = str_len(my_str);
length = 175 - length;
blank_str = str_create("#length");
str_cat(my_str,blank_str);


I am trying to resize my_str to 175 characters here,but it does not.

regards
zazang






Posted By: Excessus

Re: String length - 04/03/08 11:26

The problem is that blank_str = str_create("#length"); creates a string of only 7 characters long. #, l, e, n, g, t, h = 7. The "#num" syntax only works with string initialization, and only with literals.

You could try:
var length = str_len(my_str);
length = 175 - length;
int i = 0;
for(i = 0; i < length; i++)
{str_cat(my_str, " ");}
Posted By: zazang

Re: String length - 04/03/08 12:02

Worked exactly as the master Excessus says..thanks so much !

regards
zazang
Posted By: fogman

Re: String length - 04/03/08 21:50

while(str_len(szString) < 40){str_cat(szString, " ");}
40 = desired length

I came across the same problem. :-P
© 2024 lite-C Forums