This question already has answers here:
split a string on newlines in .NET
(17 answers)
Closed 5 years ago.
If I have a paragraph like this:
MODULE Local (Parent := "Local",
ParentModPortId := 1,
CatalogNumber := "1756-L71S",
Vendor := 1,
ProductType := 14,
ProductCode := 158,
Major := 27,
Minor := 11,
PortLabel := "RxBACKPLANE",
ChassisSize := 13,
Slot := 11,
Mode := 2#0000_0000_0000_0001,
CompatibleModule := 0,
KeyMask := 2#0000_0000_0001_1111,
SafetyNetwork := 16#0000_3acc_033e_6fa0)
END_MODULE
and it is currently saved as just a simple string in my program. Now, I'd like to save each line as an entry in an array, how would I go about doing that?
Use String.Split function
string[] paragraph =yourString.Split(",");
Try to use double double quotes as escape char
String Para = #"MODULE Local (Parent := ""Local"",
ParentModPortId:= 1,
CatalogNumber:= ""1756-L71S"",
Vendor:= 1,
ProductType:= 14,
ProductCode:= 158,
Major:= 27,
Minor:= 11,
PortLabel:= ""RxBACKPLANE"",
ChassisSize:= 13,
Slot:= 11,
Mode:= 2#0000_0000_0000_0001,
CompatibleModule:= 0,
KeyMask:= 2#0000_0000_0001_1111,
SafetyNetwork:= 16#0000_3acc_033e_6fa0)
END_MODULE";
I observe that the format is fairly similar to JSON, albeit with other separators.
Hence, one way to solve it would be to convert it to JSON:
replace all ":=" with "="
replace ( and ) with { and }
replace , with ;
delete the MODULE labels
Then you can parse it using a JSON lib, for instance NewtonSoft, which is on NuGet.
If you know beforehand what string pairs to expect, you can cast it into a defined class. Otherwise, cast it into dynamic (or into a Dictionary if your .NET version is too old for dynamic) and continue on from there.
Related
I'm developing QR-Code for crystal reports using microsoft visual studio 2015 and the project ASP.NET and MVC.
When run the project on local computer and test environment the report working correct without any errors.
but when recompile the project and publish it to the hosting cloud the report not running and getting this error in formula :
A number, currency amount, boolean, date, time, date-time, or string is expected here. Details: errorKind Error in File RPTCCOVIDRESULTS 8264_47192_{BF8E4C5B-05BD-4BC0-9CB7-7DDD1E1042DA}.rpt: Error in formula QrCode: ' ' A number, currency amount, boolean, date, time, date-time, or string is expected here. Details: errorKind
I sent Email to the hosting cloud helpdesk and they replied :
You are using CR13 SP18, please consider installing CR13 SP19, then recompile and publish again.
I installed crystal report 13 SP19 , but still getting same error.
This is the formula code :
stringvar replaceid := Replace(totext({GET_COVID_RESULT_PRINT;1.order number}),",","");
stringvar replaceids := Replace(replaceid,".00","");
stringVar barcodeInput:= "http://www.lab.com/lab/"&""&replaceids;
numberVar minimumSize := 1; // Minimum symbol size. Valid sizes are 1-40.
numberVar errorCorrectionLevel := 1; // Error Correction. Valid values are 1-4 (representing L, M, H, Q)
booleanVar allowSpecialChars := true; // Allows input of special characters in format ^000 where 000 is decimal ASCII code.
numberVar fnc1Mode := 0; // If set to 1, encodes as GS1 QR Code. Input must be in format: (00)000000000(000)000000
numberVar quietZoneWidth := 0; // Size of quiet zone, in number of modules.
numberVar borderWidth := 0; // Border thickness, in number of modules. If non-zero, quietZoneWidth must be 4 or larger.
stringVar textEncoding := "UTF-8"; // Encoding of ASCII > 127 values. For example: UTF-8 | ISO-8859-1 | ISO-8859-3 | ISO-2022-JP-2
numberVar numParts := QrCodeSetOptions (barcodeInput, minimumSize, errorCorrectionLevel, allowSpecialChars,
fnc1Mode, quietZoneWidth, borderWidth, textEncoding);
stringVar fullBarcode := "";
numberVar i := 0;
for i := 0 to numParts-1 do
fullBarcode := fullBarcode + QrCodeGetPart(i);
fullBarcode; // Return the encoded barcode text
Please your help to solve this error ?
I am trying to mimic the C# function Encoding.ASCII.GetBytes in Oracle 12c. I am nearly there but can't figure why I get the following results:
Oracle
declare
l_string varchar2(4000) := 'Test';
begin
dbms_output.put_line(utl_raw.cast_to_raw(l_string));
end;
The output from this is:
54657374
C#
internal static string ConvertTest()
{
var inputString = "Test";
Console.WriteLine(BitConverter.ToString(Encoding.ASCII.GetBytes(inputString)));
}
The output from this is:
54-65-73-74
So I seem to be nearly there but I can't figure out why C# has '-' between each byte and Oracle does not.
Is there an Oracle function to replicate the output from C#?
Thanks.
utl_raw.cast_to_raw does not put '-' in between byte code of each character. I dont think there is a direct way to get series of byte codes in the expected format.
One workaround can be, you loop through each character and append '-' after each iteration.
Sample code -
declare
l_string varchar2(4000) := 'Test';
l_result varchar2(4000) := '';
begin
for idx in 1 .. length(l_string)
loop
l_result := l_result || utl_raw.cast_to_raw(SUBSTR(l_string,idx,1));
if idx != length(l_string) then
l_result := l_result || '-';
end if;
end loop;
dbms_output.put_line(l_result);
end;
I think I was chasing the wrong thing!
utl_raw.cast_to_raw is the equivalent to Encoding.ASCII.GetBytes(). It is BitConverter.ToString that is actually inserting the '-' between each byte.
Thanks for your help!
im looking for a code in fast report 4 which take 2 first alphabet of string and if it was 10, write red for me, if it was 11 right blue for me
another example
{IF started with "ab..." then write "RED"
IF started with "dc..." then write "Blue"}
Something like this?
procedure Page1OnBeforePrint(Sender: TfrxComponent);
begin
if Copy(Memo2.Text,1,2) = 'ab' then
begin
Memo2.Text:= 'Red';
Memo2.Font.Color:= clRed;
end;
if Copy(Memo6.Text,1,2) = 'dc' then
begin
Memo6.Text:= 'Blue';
Memo6.Font.Color:= clBlue;
end;
end;
Try this :
Var Str : String;
begin
Str := 'Your String';
Str := Copy (Str , 1 , 2 );
If Str='Yo' then
ShowMessage('RED')
else
If Str='You' then
ShowMessage('BLUE');
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am converting some legacy VB6 code to C# and this just has me a little baffled. The VB6 code wrote certain data sequentially to a file. This data is always 110 bytes. I can read this file just fine in the converted code, but I'm having trouble with when I write the file from the converted code.
Here is a stripped down sample I wrote real quick in LINQPad:
void Main()
{
int[,] data = new[,]
{
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
},
{
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39
}
};
using ( MemoryStream stream = new MemoryStream() )
{
using ( BinaryWriter writer = new BinaryWriter( stream, Encoding.ASCII, true ) )
{
for( var i = 0; i < 2; i++ )
{
byte[] name = Encoding.ASCII.GetBytes( "Blah" + i.ToString().PadRight( 30, ' ' ) );
writer.Write( name );
for( var x = 0; x < 20; x++ )
{
writer.Write( data[i,x] );
}
}
}
using ( BinaryReader reader = new BinaryReader( stream ) )
{
// Note the extra +4 is because of the problem below.
reader.BaseStream.Seek( 30 + ( 20 * 4 ) + 4, SeekOrigin.Begin );
string name = new string( reader.ReadChars(30) );
Console.WriteLine( name );
// This is the problem..This extra 4 bytes should not be here.
//reader.ReadInt32();
for( var x = 0; x < 20; x++ )
{
Console.WriteLine( reader.ReadInt32() );
}
}
}
}
As you can see, I have a 30 character string written first. The string is NEVER longer than 30 characters and is padded with spaces if it is shorter. After that, twenty 32-bit integers are written. It is always 20 integers. So I know each character in a string is one byte. I know a 32 bit integer is four bytes. So in my reader sample, I should be able to seek 110 bytes ( 30 + (4 * 20) ), read 30 chars, and then read 20 ints and that's my data. However, for some reason, there is an extra 4 bytes being written after the string.
Am I just missing something completely obvious (as is normally the case for myself)? Strings aren't null terminated in .Net and this is four bytes anyway, not just an extra byte? So where is this extra 4 bytes coming from? I'm not directly calling Write(string) so it can't be a prefixed length, which it's obviously not since it's after my string. If you uncomment the ReadInt32(), it produces the desired result.
The extra 4 bytes are from the extra 4 characters you're writing. Change the string you're encoding as ASCII to this:
("Blah" + i.ToString()).PadRight(30, ' ')
That is, pad the string after you've concatenated the prefix and the integer.
Your extra four bytes are whitespace, because you aren't subtracting the length of 'Blah'. You don't know where you are in your stream. So basically, you think you're writing only 30 chars, but you really wrote 34 chars.
I know you didn't ask this - but you're writing garbage data to a file that doesn't need to be there.
Instead of padding your string with whitespace, you should just include a header or pointer that indicates the length of the next field in your file.
For example, say you have a 120 byte file. The first 4 bytes of the file indicate that the length of the following string is 96 bytes. So you read 4 bytes, get the length and then read 96 bytes. The next 4 bytes say that you have a string that's 16 bytes long, so you read the next 16 bytes and get your next string. This is pretty much how every well defined protocol works.
A regular expression is used to parse text that include '=' and put split string into a stringlist like key value pair.
But if value contains '=', it can not use list.add(key3+'='+'number=10');
key1 this's done
key2 that costs 10 dollars
key3 number=10 // invalid data, error prompt.
...
how to solve?
Thank you.
Edit:
Thank you all for help.
If I have to add a string that includes '=' into key, how can I solve it?
for example, the text to be parsed may be like this:
maleConsumer=john 1
maleConsumer=eric 2
femaleConsumer=mary 2
maleConsumer=john 8
...
I use regex reg='\b\S+\b' parse text and to put maleconsumer=john into key of stringlist, so that in stringlist, john's record will be:
maleConsumer=john 9 // maleconsumer=john is key, 9 is value
In such case, how can I do it?
Thank you all for your help again.
This works fine in Delphi
var
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.Add('key1=this''s done');
sl.Add('key2=that costs 10 dollars');
sl.Add('key3=number=10');
ShowMessage(sl.Values['key3']); // Displays number=10
finally
sl.Free;
end;
end;
This is better and still works
var
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.Values['key1'] := 'this''s done';
sl.Values['key2'] := 'that costs 10 dollars';
sl.Values['key3'] := 'number=10';
ShowMessage(sl.Values['key3']); // Displays number=10
finally
sl.Free;
end;
end;
BTW, you can specify the separator with TStringList.NameValueSeparator
Using NameValueSeparator to allow = in key
var
sl: TStringList;
begin
sl := TStringList.Create;
try
// Select a separater you are sure will never be used
sl.NameValueSeparator := '|';
sl.Values['maleConsumer=john'] := '1';
sl.Values['maleConsumer=eric'] := '2';
sl.Values['femaleConsumer=mary'] := '2';
sl.Values['maleConsumer=john'] := '8';
ShowMessage(sl.Values['maleConsumer=john']); // Displays 8
finally
sl.Free;
end;
end;
Check if the value contains '=':
if(value.indexOf('=') != -1){
//error prompt
}
Java:
you can use: String.contains() method.
For Delphi; you can set Delimiter and QuoteChar for your strings.
Example:
cars := TStringList.Create;
// Now add some cars to our list - using the DelimitedText property
// with overriden control variables
cars.Delimiter := ' '; // Each list item will be blank separated
cars.QuoteChar := '|'; // And each item will be quoted with |'s
cars.DelimitedText := '|Honda Jazz| |Ford Mondeo| |Jaguar "E-type"|';
look here for detail.
Delphi:
var
LStringList: TStringList;
LStrValue: string;
begin
LStringList := TStringList.Create;
try
// set the value of a key
LStringList.Values['a key'] := 'a value';
// get the value of a key
LStrValue := LStringList.Values['a key'];
finally
FreeAndNil(LStringList);
end;// trye
end;
If you are using Delphi 2009 or later, use TDictionary instead of TStringList. That way you avoid all these hacks required to get TStringList to work properly.