incorrect right to left concatenation english and Arabic - c#

I'm trying to concatenate an English string with Arabic string
string followUpFormula = "FIF";
string renewAbbreviation = "ع.ت" ;
string abbreviation = followUpFormula +"-"+ renewAbbreviation;
var result = 10 + "/" + abbreviation + "/" + 2016;
the result is 10/FIF-ع.ت/2016
but i want to display them like this: 10/FIF-ع.ت/
2016
how can I do that?
thanks

Couple of additions to your code
string followUpFormula = "FIF";
string renewAbbreviation = "ع.ت" ;
string abbreviation = followUpFormula +"-"+ renewAbbreviation;
var lefttoright = ((Char)0x200E).ToString();
var result = 10 + "/" + abbreviation + lefttoright + "/" + 2016;
Char 0x200E is a special character that tells the following text to read left to right see here for more information on the character.
Char 0x200F switches to a right to left format.

This has to do with the way that unicode process rules about mixing LTR and RTL text. You can override the default behaviour by explicitly using special characters that indicate an intention to directly embed RTL or LTR text :
private const char LTR_EMBED = '\u202A';
private const char POP_DIRECTIONAL = '\u202C';
private string ForceLTR(string inputStr)
{
return LTR_EMBED + inputStr + POP_DIRECTIONAL;
}
private void Form1_Load(object sender, EventArgs e)
{
string followUpFormula = "FIF";
string renewAbbreviation = "ع.ت";
string abbreviation = ForceLTR(followUpFormula + "-" + renewAbbreviation);
textBox1.Text = 10 + "/" + abbreviation + "/" + 2016;
}
This places an embedded Left-To-Right character (U+202A) before the string and follows it with a Pop-Directional-Formatting (U+202C) character. The latter removes the embedded directional formatting cue and returns the text direction to whatever it was in the previous context. The returned string, therefore, is safe to use in either an RTL or LTR context.
The rules for parsing LTR and RTL text in various contexts are extensive and complex. For reference you can find the bidirectional algorithm specification here. Certain characters are classified as "weak" or "strong" in terms of their affinity for LTR or RTL contexts. Things like / and - are weak so you have to be explicit when mixing them about which text direction and layout you wish these characters to respect.

Related

Add double quotes to a list to display in a label

Morning folks,
I have an ASP.Net C# page that pulls in a list of servers from a SQL box and displays the list of servers in a label. ("srv1,srv2,srv3"). I need to add double quotes around each of the servers names. ("srv1","srv2","srv3",)
Any help would be greatly appreached.
If you have string
string str = "srv1,srv2,srv3";
Then you can simply do
str = "\"" + str.Replace(",", "\",\"") + "\"";
Now str contains "srv1","srv2","srv3"
As far as I can understand, you are trying to use double quotes in a string.
If you want to use such,
you can use escape character:
("\"srv1\",\"srv2\",\"srv3\"",)
for the sake of simplicity, you can even convert it to a function:
private string quoteString(string serverName){
return "\"" + serverName + "\"";
}
Also, if you have already "srv1,srv2,srv3" format, find ',' characters in the string and add " before and after comma. Also, notice that you should add to first index and last index ".

Convert UTF8 apostrophe and quotes to ASCII for VB6 app

I have a VB6 desktop app and iOS and Android smartphone apps. When someone enters a name such as O'Hara in the iOS app it comes through to the VB6 desktop app as unreadable characters. I assume this is a UTF8 to ASCII conversion issue? I would like to intercept this data server side and change the ' and " to the proper character that will read properly in VB6. How should I do this in C# .NET?
You can replace the problematic characters with a version that fits in the character set used by VB 6:
string value = "The problem string";
value = value.Replace("\u2022", "-");
value = value.Replace("\u2013", "-");
value = value.Replace("\u2014", "-");
value = value.Replace("\u2015", "-");
value = value.Replace("\u2017", "_");
value = value.Replace("\u2018", "'");
value = value.Replace("\u2019", "'");
value = value.Replace("\u201a", ",");
value = value.Replace("\u201b", "'");
value = value.Replace("\u201c", "\"");
value = value.Replace("\u201d", "\"");
value = value.Replace("\u201e", "\"");
value = value.Replace("\u2026", "...");
value = value.Replace("\u2032", "'");
value = value.Replace("\u2033", "\"");
This is not an exhaustive list of all Unicode characters, of course. You may end up finding you need to replace more, or alarm on them in some way.

Transforming a string into a escaped char

The goal is to have a string input (coming from the frontend), and this string should be transformed to act as a escaped char in the backend.
In the following example I want the user to write "\" + "t", and the backend should interpret it as "\t" (= tab char):
var inputStr = #"\t"; // The input is a string written by a user: "\t" (backslash char + t char == #"\t" != "\t")
var outputStr = SomeOperation(inputStr); // ???
Console.WriteLine("A" + outputStr + "B <= should be tab separated");
I have tried:
var outputStr = inputStr.Replace("\", "");
This isn't something that is built in. Ultimately, "\t" == (a string of length 1 containing a tab character) is implemented by the C# compiler, not the runtime. There isn't a pre-existing implementation of this in the runtime, in part because each language (VB.NET, C#, F#, etc) can have their own rules.
You would need to write your own implementation with your own definitions of escape characters. Fortunately, it is mostly an exercise in .Replace(...). There are some edge cases to think about - in particular for ordering - though; for example, if \\ becomes \ and \n becomes newline; does \\n become \n? or does it become \(newline)? done naively, it can end up as just (newline) - i.e. foo.Replace(#"\\",#"\").Replace(#"\n","\n")
You can do something like this:
void Main()
{
Debug.Assert(ReplaceChar("hello\tworld", #"\t") == "helloworld"); // passed
}
string ReplaceChar(string str, string userInput)
{
switch (userInput)
{
case #"\t":
return str.Replace("\t","");
}
return str;
}
I have finally found an easy way to do it:
Regex.Unescape(inputStr);
See the documentation of the Regex.Unescape function for more details.
Example:
var ouptutStr = Regex.Unescape("\\t");
// ✓ Result: outputStr == "\t"
var outputStr = Char.Parse("\t").ToString();
gives
A B <= should be tab separated
It isn't seen here but in console it looks properly.

%u200E appears on my link when i dont want it to appear c#

I have to make a part of my webisite link into arabic language so i have t use leftToRight Char to set the direction of the url but after i used it it placed on the Url %u200E when i dont need it because the url will fail to redirect to my correct path
I used this code it an Example from the internet
string followUpFormula = "FIF";
string renewAbbreviation = "ع.ت" ;
var lefttoright = ((Char)0x200E).ToString();
var result = 10 + "/" + abbreviation + lefttoright + "/" + 2016;
lefttoright is placing %u200E into my url how to avoid placing it on the url ?

Extract sub-string between two certain words right to left side

Example String
This is an important example about regex for my work.
I can extract important example about regex with this (?<=an).*?(?=for) snippet. Reference
But i would like to extract to string right to left side. According to this question's example; first position must be (for) second position must be (an).
I mean extracting process works back ways.
I tried what i want do as below codes in else İf case, but it doesn't work.
public string FnExtractString(string _QsString, string _QsStart, string _QsEnd, string _QsWay = "LR")
{
if (_QsWay == "LR")
return Regex.Match(_QsString, #"(?<=" + _QsStart + ").*?(?=" + _QsEnd + ")").Value;
else if (_QsWay == "RL")
return Regex.Match(_QsString, #"(?=" + _QsStart + ").*?(<=" + _QsEnd + ")").Value;
else
return _QsString;
}
Thanks in advance.
EDIT
My real example as below
#Var|First String|ID_303#Var|Second String|ID_304#Var|Third String|DI_t55
When i pass two string to my method (for example "|ID_304" and "#Var|") I would like to extract "Second String" but this example is little peace of my real string and my string is changeable.
No need for forward or backward lookahead! You could just:
(.*)\san\s.*\sfor\s
The \s demands whitespace, so you don't match an import*an*t.
One potential problem in your current solution is that the string passed in contains special characters, which needs to be escaped with Regex.Escape before concatenation:
return Regex.Match(_QsString, #"(?<=" + Regex.Escape(_QsStart) + ").*?(?=" + Regex.Escape(_QsEnd) + ")").Value;
For your other requirement of matching RL, I don't understand your requirement.

Categories