Does Ada have support for something similar to an Obj-C variable in a string?
NSLog(#"This is text, here's a variable %f", floatvar);
I'd like to be writing nice one-liners, like:
put_line("The answer is %v", answer);
instead of
put_line("The answer is ");
put(answer);
You might like the Ada FAQ, specifically part 9.9. For completeness, I quote it here:
While the standard package Text_IO provides many features, the
request for a printf-like function is not unusual.
(solution based on a suggestion by Tucker Taft)
It is possible to produce a printf-like capability by overloading
the "&" operator to take an object of type Format and an object of
some type and return the Format, properly advanced, after having
performed the appropriate output. The remaining format can be
converted back to a string--e.g. to examine what is left at the end
of the format string-- or simply printed to display whatever
remains at the end. For example:
with Text_IO;
package Formatted_Output is
type Format is
limited private;
function Fmt (Str : String)
return Format;
function "&" (Left : Format; Right : Integer)
return Format;
function "&" (Left : Format; Right : Float)
return Format;
function "&" (Left : Format; Right : String)
return Format;
... -- other overloadings of "&"
procedure Print (Fmt : Format);
function To_String (Fmt : Format)
return String;
private
...
end Formatted_Output;
with Formatted_Output; use Formatted_Output;
procedure Test is
X, Y : Float;
begin
Print (Fmt("%d * %d = %d\n") & X & Y & X*Y);
end Test;
The private part and body of Formatted_Output are left as an
exercise for the reader ;-).
A "File : File_Type" parameter could be added to an overloading of
Fmt if desired (to create something analogous to fprintf).
This capability is analogous to that provided by the "<<" stream
operator of C++.
Assuming you have F : Float;, you can say
Put_Line (“the answer is “ & Float’Image (F));
This doesn’t work so well if you want neat formatting, because the format output by ’Image is fixed as specified in the ARM (that link’s actually to ’Wide_Wide_Image, not ’Image, but the format’s the same).
If you’re using GNAT, you could write the above as
Put_Line (“the answer is “ & F'Img);
which saves (a) characters and (b) remembering the type concerned, but that’s not portable.
Related
I want to convert a string into a xstring. I know that there is a function module named "SCMS_STRING_TO_XSTRING"
But since it is not a good habit to use function modules anymore, a class based solution would be my prefered way to go.
I know that there is a class
cl_abap_conv_in_ce
but I can only validate, that this class can convert xstrings into string. I wand to have the reverse case. Does anyone have experience on how to do that class based?
Meanwhile, I found the solution on my own. For people who might be interested:
DATA(lo_conv) = cl_abap_conv_out_ce=>create( ).
lo_conv->write( data = lv_content ).
DATA(lv_xstring) = lo_conv->get_buffer( ).
The help text for XSTRING provides a nice functional method for this:
cl_abap_codepage=>convert_to( )
Firstly, you need to decide how you want it encoded. UTF-8? UTF-16? Just plain HEX?
For UTF-8 You can do the following using system calls (instead of function calls):
First do a global once-off initialization:
STATICS: g_conv_utf8 TYPE xstring. " used for conversion
DATA: l_flags TYPE c LENGTH 1.
system-call convert id 20
srcenc 'SET LOCALE LANGUAGE'
dstenc 'UTF-8'
replacement '#'
type l_flags
cinfo g_conv_utf8.
And then do subsequent calls: l_string -> l_xstring (+ l_len)
SYSTEM-CALL CONVERT ID 24
DATA l_string
ENDIAN ' '
IGNORE_CERR 'X'
N -1
BUFFER l_xstring
LEN l_length
CINFO g_conv_utf_8.
This is the essence of what cl_abap_codepage=>convert_to( ) does internally.
I am working in an ABAP program and I have a question.
For example in C# when we have a String variable: string name; , and we want this to be filled with some data from a textbox but also add some ohter text.
For example:
string name = "Hello: " + textBox1.text;,
And I want to ask you how can I do this in ABAP ??? How to add text plus the text written from a Parameter type C?
CONCATENATE and the concatenate operator && will do it as answered by Jagger and vwegert. To do it with string expressions, you use the below where name is the screen field or whatever that has the name in it (it doesn't need to be a field-symbol):
greeting = |Hello: { <name> }|.
String expressions are extremely useful as they can be used to build up complex values without declaring extra variables - e.g. they can passed as directly as function module or method parameters without first assigning to a local variable.
You can either use the CONCATENATE keyword or -- in newer releases -- string expressions. Be sure to check the online documentation and sample programs available using the transaction ABAPDOCU, it will save you a ton of seemingly basic questions.
The equivalent operator is &&.
So in your case it would be:
name = 'Hello: ' && textBox1->text.
Question: Can I assume that Str(myString) will always return the same result as Str(CDbl(myString)) (assuming that myString is statically typed as a string)?
Context:
I am trying to understand VBA's implicit conversions. So far, it appears to me that Str(myString)
implicitly parses myString into a double (culture-sensitive) and then
converts the result into a culture-insensitive string.
For example, using a German locale (i.e. using , as the decimal separator), it holds that
" 1.2" = Str(1.2) = Str("1,2") = Str(CDbl("1,2"))
Since these implicit conversions contain a lot of "magic" to me, I am trying to rewrite a procedure that uses an implicit conversion (Str(myString)) to one using explicit conversion without changing the behavior.
Unfortunately, the documentation is wrong and, thus, useless. (The documentation claims that the argument to Str is interpreted as a Long, which is obviously rubbish: If that were the case Str(1.2) could never yield " 1.2".)
Your statement is true. Str(x) and Str(Cdbl(x)) give identical result provided that x is String data type and contains a valid number.
I made a small test to get convinced.
I used Excel, but it holds the same with Access.
Public Function myStr(txt As String) As String
myStr = Str(txt)
End Function
Public Function myStrCDbl(txt As String) As String
myStrCDbl = Str(CDbl(txt))
End Function
I tried with some key values (0, 1.2, 1E+307, 1E-307, ...) : result of myStr and myStrCDbl are always identical.
I also agree with you that the documentation is wrong. If Str() argument would be interpreted as Long, then Str(1.2) would give "1", because Long is an integer type.
In the mean time, I've found the VBA language specification and can confirm that the spec also answers the question with "yes":
CDbl, when receiving a string, performs a Let-coercion to Double:
If the value of Expression is not an Error data value return the Double data value that is the result of Expression being Let-coerced to Double.
Str, when receiving a string, first performs a Let-coercion to Double and then applies Str:
[If Number is a String,] the returned value is the result of the Str function applied to the result of Let-coercing Number to Double.
I've been looking around but having great difficulty finding the answer to this question as the thing I'm looking for is so unspecific.
I've seen a lot of code which uses {0} in it, and I still can't work out what it's doing. Here's an example:
Dim literal As String = "CatDogFence"
Dim substring As String = literal.Substring(6)
Console.WriteLine("Substring: {0}", substring)
Console.WriteLine("Substring: {0}", substring)
Is the same as
Console.WriteLine("Substring: " & substring)
When using Console.WriteLine, {n} will insert the nth argument into the string, then write it.
A more complex example can be seen here:
Console.WriteLine("{0} {1}{2}", "Stack", "Over", "flow")
It will print Stack Overflow.
Console.WriteLine() and String.Format() use that syntax.
It allows you to inject a variable into a string, for example:
dim name = "james"
String.Format("Hello {0}", name)
That string will be "Hello james"
Using Console.Writeline:
Console.WriteLine("Hello {0}",name)
That will write "Hello james"
It's a placeholder. Beginning at the second parameter (substring in your case), they are included in the given string in the given order. This way you avoid long string concatenations using + operator and can do easier language localization, because you can pull the compete string including the placeholders to some external resource file etc.
It is called composite formatting and is supported by many methods, Console.WriteLine being one. Besides indexed placeholders there are other features available. Here is a link to the documentation that shows some of the other features of composite formatting.
Composite Formatting
what is the meaning of the dollar sign after a method name in vb.net
like this:
Replace$("EG000000", "0", "")
Old type notifier - see this
Some other old ones:
& -> Long
% -> Integer
# -> Double
! -> Single
# -> Decimal
$ -> String
Still exist in VB.Net for the sake of backward compatibility...
In "classic" VB, there were two versions of the built in-string functions. Let me use Left as an example:
Left(s, length) takes a variant as the first parameter and returns a variant.
Left$(s, length) takes a string as the first parameter and returns a string.
This distinction still exists in modern-day VBA.
I suspect that the reason behind this is that strings in VBA cannot be Null (note that Null <> ""). Thus, when dealing with nullable database fields, you had to use variant variables. Variant variables can take any value, including all of the integral values (strings, integers, ...) as well as some special values such as Null, Empty or Missing. The non-$ functions allowed you to use variants as input and get variants as output. For example, Left(Null, ...) returns Null.
In VB.NET, this distinction is no longer necessary: The non-$ functions do exactly the same as the $ functions, which are kept only for backwards compatibility with old code.
What Heinzi said and to clear up the type character business
Dim s$ = "FooBar" 'dim s as String = "FooBar"
Dim r As String
Stop
r = Replace$(s, "Bar", "")
'.Net equivalent
r = s.Replace("Bar", "")