Concatenate a String with Integer using special format

I’ve done this before years ago using FileMaker. I’m not exactly sure how to do this with Xojo.

I have a counter that is read off a preference file. The counter increments by 1 and is updated in the prefs file - all good.

What I need is to combine a string, ie. “TEST” with a numeric value, ie 42. That’s easy enough to do, ie. TEST42.

What I really want is TEST0042 giving an ultimate 9999 numbers before it clicks over.

Hopefully this makes sense.

Cheers.

Format is the instruction to use:

http://documentation.xojo.com/index.php/Format

Edit:
9999: you haveto check that using If for example… or inside the read oop or…

Thanks Emile - what do you mean about using a loop?

http://documentation.xojo.com/index.php/Format

Ok, thanks. I’ll check out the Format link. I’ve used it to format text fields but not with creating my own variable.

You are talking about reading a file, I supposed you can use a loop to read that file line after line (or bunch of bytes after bunch of byte).

Edit:

Combined_Var = “TEST” + Format(“000”, 42) // Will (normally) gives “TEST042”

Thanks Emile.

Reading the file is all good and working as required. It’s formatting the value that I need to learn about.

The link posted above should give me the info that I need.

Test+format(myInteger, "0000")

Should do it.

It might be better to use the Str function instead of Format.

dim num As Integer = 1234 dim test As String = "TEST" + Str(num, "0000")
would give Test = “TEST1234”

format with no decimals, thousands, or currency signs does the same as str without those markers

where they differ is format will use locale aware markers
str doesn’t - it uses “US” (actually C style , and . for thousands and decimals)

Steve, when someone provides the answer to your question, it is customary (and polite) to click “This answers my question”, in the upper right corner of his post.

Thanks all, this appears to being working as required, although I’ve just tested in a basic manner and will need to check carefully when fully implemented.

Dearest Michel. What answer was provided? How was it solved? Regardless of who says what, It’s only ever “really” solved when “I” actually test and confirm it for myself.

After my last post, I went to bed, slept, got up and worked a 9hr day, got home, prepared a meal, sat down at the computer, read the replies, did some basic tests and have come to the conclusion that your method above works:

Test+format(myInteger, "0000")

Thank you.

Steve, take your pick or Michel or Emile’s responses — I don’t give a rat’s. The aim is that anyone searching for forums in future won’t be forced to scan the list to find the answer. Only YOU can choose the best response as the answer.

Indeed, checking an answer is very important for people who will search for the same question in the future. It is a courtesy to other members needing the same solution.

Thanks Michel & David. If I didn’t already know - then I certainly do now :slight_smile:

As an addition to this, I think it’s worth noting that the code can be changed to ONLY show the last 4 digits (or whatever number). The numbers are truncated at left. Therefore 1234567 becomes 4567, or in this case TEST4567 :

TEST + Right(Format(myInteger, "0000"),4)

As it is, for my purposes, the original code works fine. Over 9999 - then it shows 10000, and so it should.

The reason I wanted to know how to do this, is because I want my saved filenames to be preceded by: TEST0001, TEST0002 etc. That way, the user can view the files by name and therefore chronologically without relying on an internal date/time stamp.

It’s also far easier to review files this way because my software is used for recording analytical tests and therefore a number is more appropriate and easier to deal with, in the majority of cases.

Ultimately this is the type of filename I’m talking about:

TEST0007_TESTNAME_Wed, 28 Jun 2017 @ 7.37 PM

Also, it’s important to mention that the filename is automatically created, ie. no user intervention, no save dialog box. The user can enter a TESTNAME, but that’s all.

It may seem insane, complicated, bad programming, unnecessary or ALL of those — but really simple if you know why.

Cheers.

I assume you know you can also do this:

TEST + Right(Format(myInteger, "########0000"),4)

[quote=338062:@Steve Kelepouris]As an addition to this, I think it’s worth noting that the code can be changed to ONLY show the last 4 digits (or whatever number). The numbers are truncated at left. Therefore 1234567 becomes 4567, or in this case TEST4567 :

TEST + Right(Format(myInteger, "0000"),4)

As it is, for my purposes, the original code works fine. Over 9999 - then it shows 10000, and so it should.

The reason I wanted to know how to do this, is because I want my saved filenames to be preceded by: TEST0001, TEST0002 etc. That way, the user can view the files by name and therefore chronologically without relying on an internal date/time stamp.

It’s also far easier to review files this way because my software is used for recording analytical tests and therefore a number is more appropriate and easier to deal with, in the majority of cases.

Ultimately this is the type of filename I’m talking about:

TEST0007_TESTNAME_Wed, 28 Jun 2017 @ 7.37 PM

Also, it’s important to mention that the filename is automatically created, ie. no user intervention, no save dialog box. The user can enter a TESTNAME, but that’s all.

It may seem insane, complicated, bad programming, unnecessary or ALL of those — but really simple if you know why.

Cheers.[/quote]

I’d think you’d name them

TESTNAME_TEST0007_Wed, 28 Jun 2017 @ 7.37 PM

so they all group together by test name

Say a user did 4 or 5 different runs of tests and they each had several tests then you end up with files group together like

TEST0001_BAR_Wed, 28 Jun 2017 @ 7.37 PM
TEST0001_FOO_Wed, 28 Jun 2017 @ 7.37 PM
TEST0001_TESTNAME_Wed, 28 Jun 2017 @ 7.37 PM
TEST0002_BAR_Wed, 28 Jun 2017 @ 7.37 PM
TEST0002_FOO_Wed, 28 Jun 2017 @ 7.37 PM
TEST0002_TESTNAME_Wed, 28 Jun 2017 @ 7.37 PM

but if you named them by the users testname first then you’d get
BAR_TEST0001_Wed, 28 Jun 2017 @ 7.37 PM
BAR_TEST0002_Wed, 28 Jun 2017 @ 7.37 PM
FOO_TEST0001_Wed, 28 Jun 2017 @ 7.37 PM
FOO_TEST0002_Wed, 28 Jun 2017 @ 7.37 PM
TESTNAME_TEST0001_Wed, 28 Jun 2017 @ 7.37 PM
TESTNAME_TEST0002_Wed, 28 Jun 2017 @ 7.37 PM

[quote=338109:@David Cox]I assume you know you can also do this:

TEST + Right(Format(myInteger, "########0000"),4)

Thanks for that Dave - It’s nice to know there are those options using Format.

[quote=338111:@Norman Palardy]I’d think you’d name them

TESTNAME_TEST0007_Wed, 28 Jun 2017 @ 7.37 PM

so they all group together by test name

Say a user did 4 or 5 different runs of tests and they each had several tests then you end up with files group together like

TEST0001_BAR_Wed, 28 Jun 2017 @ 7.37 PM
TEST0001_FOO_Wed, 28 Jun 2017 @ 7.37 PM
TEST0001_TESTNAME_Wed, 28 Jun 2017 @ 7.37 PM
TEST0002_BAR_Wed, 28 Jun 2017 @ 7.37 PM
TEST0002_FOO_Wed, 28 Jun 2017 @ 7.37 PM
TEST0002_TESTNAME_Wed, 28 Jun 2017 @ 7.37 PM

but if you named them by the users testname first then you’d get
BAR_TEST0001_Wed, 28 Jun 2017 @ 7.37 PM
BAR_TEST0002_Wed, 28 Jun 2017 @ 7.37 PM
FOO_TEST0001_Wed, 28 Jun 2017 @ 7.37 PM
FOO_TEST0002_Wed, 28 Jun 2017 @ 7.37 PM
TESTNAME_TEST0001_Wed, 28 Jun 2017 @ 7.37 PM
TESTNAME_TEST0002_Wed, 28 Jun 2017 @ 7.37 PM[/quote]

Well Norman, that has certainly opened up a can of wormed, and stirred nested hornets.

That is to say, looking at the last half of your post: “but if you named them by the users testname”. Yes, that does seem like a better solution. I’ve spoken to my only UI Alpha Tester (my Brother) and he agrees with what you have put forward. It’s been a big point of discussion in the last few days and I’ve had a good think about it.

However, there are some issues with that particular way of going about it.

TEST0001 etc. is incremental in perpetuity and exclusive. Therefore in my world: BAR_TEST0001 and FOO_TEST0001 cannot ever exist together.

Obviously this discussion has gone beyond my initial question. Should I start another thread - or just keep it going?

Then use your original naming if the test names cant ever be replicated