Correct text direction on pdf file with c# - c#

I have pdf file that all the text come like that:
instead "hello my world" write "dlrow ym olleh"...
how can i convert the text to the correct please?
For example , the url
http://www.textreverse.com/
do it well,
I need to do it on c#
thanks!!

You need to reverse a string. Here is a way to do it:
1. Split your string to an array of chars:
char[] Chars = PdfString.ToCharArray();
2. Reverse the array:
Array.Reverse(Chars);
3. Convert a string out of this array:
string ReversedPdfString = new string(Chars);

Related

Convert byte array to string has special characters C#

I am going to convert byte array into string using below method.
string data = Encoding.Default.GetString(e.Data);
But this string has the following special characters which I needed to avoid from the string itself.
As you can see in the data I am getting space before the '7h' and some '\rL' getting added after the text. I used trim() function. But it didn't work for the following case.
Please advice.
If you know exactly symbols you whant remove, just replace it:
var data = "⏩Some⏬String";
var clearedData = data.Replace("⏩", string.Empty).Replace("⏬", string.Empty);
result:
SomeString

Getting if my array has a word that appears on my string C#

I have a file with common compliments like this:
Hello
Hi
Hey
And in every line there is a new word. I got those words onto an array and now if my string has "hi", I want to know if there is "hi" in the array for example:
string Message = Hello people;
string[] Compliments = ReadAllLinesFromXFile();
if (message.contains(compliments)){
do x function
}
this is in C# and any help I would apreciate
You could do it using LINQ's Any() method:
if (compliments.Any(message.Contains))
// do something
This iterates over the compliments array and calls message.Contains() for each of the compliments until one of them is found in the message string.

Get only numbers from line in file

So I have this file with a number that I want to use.
This line is as follows:
TimeAcquired=1433293042
I only want to use the number part, but not the part that explains what it is.
So the output is:
1433293042
I just need the numbers.
Is there any way to do this?
Follow these steps:
read the complete line
split the line at the = character using string.Split()
extract second field of the string array
convert string to integer using int.Parse() or int.TryParse()
There is a very simple way to do this and that is to call Split() on the string and take the last part. Like so if you want to keep it as a string:
var myValue = theLineString.Split('=').Last();
If you need this as an integer:
int myValue = 0;
var numberPart = theLineString.Split('=').Last();
int.TryParse(numberPart, out myValue);
string setting=sr.ReadLine();
int start = setting.IndexOf('=');
setting = setting.Substring(start + 1, setting.Length - start);
A good approach to Extract Numbers Only anywhere they are found would be to:
var MyNumbers = "TimeAcquired=1433293042".Where(x=> char.IsDigit(x)).ToArray();
var NumberString = new String(MyNumbers);
This is good when the FORMAT of the string is not known. For instance you do not know how numbers have been separated from the letters.
you can do it using split() function as given below
string theLineString="your string";
string[] collection=theLineString.Split('=');
so your string gets divided in two parts,
i.e.
1) the part before "="
2) the part after "=".
so thus you can access the part by their index.
if you want to access numeric one then simply do this
string answer=collection[1];
try
string t = "TimeAcquired=1433293042";
t= t.replace("TimeAcquired=",String.empty);
After just parse.
int mrt= int.parse(t);

Converting letters to dashes

I have been asked to make a game at work where I have to change the letters to dashes. I have been told to use a multi dimensional array but I get the full word out of a text file. This is the code i have so far
Console.WriteLine("Enter file path:");
string filePath= Console.ReadLine();
// read all the lines from the file
string[] lines = File.ReadAllLines(readFilePath);
// get a random number between 0 and less than the number of lines in the file
Random rand = new Random();
int chosenLineIndex = rand.Next(lines.Length);
// choose the line at the line number
string chosenLine = lines[chosenLineIndex];
// write the line to the Console
Console.WriteLine(chosenLine);
// make an array containing the only the chosen line
string[] chosenLines = new string[] { chosenLine };
So does anyone know how to write the letters separately to the array instead of the full word?
I believe you are looking for String.ToCharArray
This will give you an array that contains each character separately as a string.
string[] chosenLines = chosenLine.Select(x => x.ToString()).ToArray();
You can also use a char[] array instead.Then you won't need Select, you can just use String.ToCharArray method.

how to convert string of unicodes to the char?

i have a text file in which sets of Unicodes are written as
"'\u0641'","'\u064A','\u0649','\u0642','\u0625','\u0644','\u0627','\u0647','\u0631','\u062A','\u0643','\u0645','\u0639','\u0648','\u0623','\u0646','\u0636','\u0635','\u0633','\u0641','\u062D','\u0628','\u0650','\u064E','\u062C','\u0626"
"'\u0622'","'\u062E','\u0644','\u064A','\u0645".
I opened the file and started reading of file by using readline method. I got the above line shown as a line now i want to convert all Unicode to char so that i could get a readable string. i tried some logic but that doesn't worked i stuck with converting string "'\u00641'" to char.
You can extract strings containing individual numbers (using Regex for example), apply Int16.Parse to each and then convert it to a char.
string num = "0641"; // replace it with extracting logic of your preference
char c = (char)Int16.Parse(num, System.Globalization.NumberStyles.HexNumber);
You could parse the line to get each unicode char. To convert unicode to readable character you could do
char MyChar = '\u0058';
Hope this help
What if you do something like this:
string codePoints = "\u0641 \u064A \u0649 \u0642 \u0625";
UnicodeEncoding uEnc = new UnicodeEncoding();
byte[] bytesToWrite = uEnc.GetBytes(codePoints);
System.IO.File.WriteAllBytes(#"yadda.txt", bytesToWrite);
byte[] readBytes = System.IO.File.ReadAllBytes(#"yadda.txt");
string val = uEnc.GetString(readBytes);
//daniel

Categories

Resources