How to add string that includes '=' into stringlist? - c#

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.

Related

Getting error in File RPTCCOVIDRESULTS 8264_47192_{BF8E4C5B-05BD-4BC0-9CB7-7DDD1E1042DA}.rpt

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 ?

Oracle 12c equivalent to C# Encoding.ASCII.GetBytes

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!

How to parse a paragraph into an array line by line? [duplicate]

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.

fast report expression matter take part of string and replace it

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');

Need to convert oracle function into c# code

I have one function in oracle i need to convert it into c# code
please help or tell me any links to do it.
create or replace
FUNCTION "GETSEPARATEDSTRING"
( pString IN VARCHAR2
,pSeparator IN VARCHAR2
,pReturnNumber IN PLS_INTEGER
)
RETURN VARCHAR2
IS
l_SearchString_FinPos PLS_INTEGER :=0;
l_SearchString_StartPos PLS_INTEGER :=0;
l_SearchString_Length PLS_INTEGER :=0;
l_SearchString_CurrentPos PLS_INTEGER :=0;
l_Return VARCHAR2(4000);
BEGIN
-- expecting values as String Seperator String Seperator
-- so if pReturnNumber = 2 then where are
-- looking for seperators 2 and 1. If there is no seperator
-- at the end of the string it is added before comparison,
-- Will return a null if:
-- The length of pString is > 4000
-- The pSeparator has not been specified
-- The pReturnNumber IS <= 0
-- The pReturnNumber IS greater than the number of pSeparator + 1 and therefore we can't pick up a string
-- There was an empty string at the position requested
-- Strings are returned without pSeparator
IF LENGTH( pString || pSeparator ) <= 4000
AND pSeparator IS NOT NULL
AND pReturnNumber > 0
THEN
l_SearchString_FinPos := pReturnNumber;
l_SearchString_StartPos := pReturnNumber - 1;
-- Concat a seperator at the end of the string so at least we
-- know there is one
IF INSTR( pString, pSeparator, -1, 1) != ( LENGTH( RTRIM( pString )) - LENGTH( pSeparator ) + 1 )
THEN
-- There isn't one at the end so add it
l_Return := pString || pSeparator;
--DBMS_OUTPUT.PUT_LINE('Did not find seperator - ADDING');
ELSE
l_Return := pString;
--DBMS_OUTPUT.PUT_LINE('FOUND seperator');
END IF;
-- Set the start position of where we will check to the
-- the last position we found a pSeparator value.
l_SearchString_CurrentPos := l_SearchString_FinPos;
-- Search for the next pSeparator position
l_SearchString_FinPos := INSTR( l_Return, pSeparator, 1, l_SearchString_CurrentPos );
IF l_SearchString_FinPos != 0
THEN
IF l_SearchString_StartPos != 0
THEN
l_SearchString_CurrentPos := l_SearchString_StartPos;
l_SearchString_StartPos := INSTR( l_Return, pSeparator, 1, l_SearchString_CurrentPos ) + 1;
ELSE
-- If we are looking for the first value then StartPos will = 0
-- and cause INSTR to fail
l_SearchString_CurrentPos := 1;
END IF;
l_SearchString_Length := l_SearchString_FinPos - l_SearchString_StartPos;
l_Return := RTRIM( SUBSTR( l_Return, l_SearchString_StartPos, l_SearchString_Length ), pSeparator );
ELSE
l_Return := NULL;
END IF;
END IF;
RETURN l_Return;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE( 'FUNCTION GetSeperatedString Captured Error: ' || SQLERRM );
RETURN NULL;
END;
I don't see why you can't convert it yourself? What is the problem? You even got a comment describing exactly what the function is doing.
Do something like this:
Convert the method signature and all local variables to C# ones.
Lookup the documentation of INSTR to see if uses a zero-based index as string.IndexOf
Do a straight conversion by looking at one line at a time
Test the function
Rename all variables to have C# names
Refactor to take advantage of C#

Categories

Resources