print non-printable characters in a given string? - c#

I've a string, In sql I've tested this string contains non-printable characters but I couldn't find which are those.
So can anyone suggest me how can i find and print those non-printable characters in C#
by passing this string?
I ordered a Taurus in August and it hasn't been scheduled, I was
just wondering if there is something wrong with the order? Or if the
queue is just long? Order Number:8028Vehicle Rep:74JTag#:
200L049Description: 2011 TAURUS FWD SELOrdered: 08-AUG- 2010VIN
Assigned:VIN#:Scheduled:In
Production:Produced:Invoiced:Released:Shipped:Ready:Tax
Location: STATE OF MICHIGAN (20 )State Insured:USOB Status:050 -
CLEAN UNSCHEDULED ORDER
As i pasted the string in notepad++ it shows like this.

You can use char.IsControl(c) to test whether a character is a control (non-printable) character or not:
foreach (var c in str)
{
if (char.IsControl(c))
{
Console.WriteLine("Found control character: {0}", (int)c);
}
}

I wrote a program with some help from google and SO.
this program will print the ascii value and the position of character which is non-printable character.
class Program
{
static void Main(string[] args)
{
string text = #"I am an FMCC employee located in the Chicagoland area that currently has 2 available/active ManagementLease tags (non-incremental) and 1 currently utilized Sales Vehicle tag. I have a 2009 Sales vehicle now andhave ordered a replacement for that vehicle (2010 Taurus) per our current direction. I have not had a 2009 Model vehicle for either of my Management Lease tags in 2009. I was married in August of this year andordered a 2010 Flex on one of my Management Lease tags in September.My issue is that my wife's current vehicle is no longer serviceable, and the 2010 Flex has yet to be scheduled tobe built.My exception request is that I be allowed to take delivery and assume payments for a 2010 Taurus that is at alocal dealership and displayed on the ""Vehicles Available Now"" page on an interim basis until my 2010 Flex isdelivered. I appreciate that typically an employee cannot have two vehicles from the same model year on agiven Management Lease tag, but I was hoping an exception could be made due to my recent marriage andthe fact that I did not have a 2009 model year vehicle on any tags.";
for (int i = 0; i < text.Length; i++)
{
if (Char.ConvertToUtf32(text, i) < 32)
{
Console.WriteLine( "position " + i + " " + text[i] + " => " + Char.ConvertToUtf32(text, i));
}
}
Console.ReadLine();
}
}

Related

Regex match multiple Group.Captures with multiline

I want get all captures from group by 5 lines + empty line between each capture.
I was trying that way, but receive only first capture. When I deleting first capture from test string I receive next capture and so on, so seems my regexp is right to match.
What am I missed?
static void Main(string[] args)
{
var strBackups = #"wbadmin 1.0 - Backup command-line tool
(C) Copyright 2013 Microsoft Corporation. All rights reserved.
Backup time: 01.09.2015 11:51
Backup target: 1394/USB Disk labeled BIGGER2(F:)
Version identifier: 09/01/2015-06:51
Can recover: Volume(s), File(s), Application(s), Bare Metal Recovery, System State
Snapshot ID: {060e3b44-7b80-49bf-97c4-3f3b9908dec6}
Backup time: 06.09.2015 10:36
Backup target: 1394/USB Disk labeled BIGGER2(F:)
Version identifier: 09/06/2015-05:36
Can recover: Volume(s), File(s), Application(s), Bare Metal Recovery, System State
Snapshot ID: {64af3693-362d-42dc-ae5f-566b3f2d40be}
Backup time: 06.09.2015 11:00
Backup target: 1394/USB Disk labeled BIGGER2(F:)
Version identifier: 09/06/2015-06:00
Can recover: Volume(s), File(s), Application(s), Bare Metal Recovery, System State
Snapshot ID: {d9d50a01-6907-40a1-9c57-1f45de76b9ec}
";
var regBackups = new System.Text.RegularExpressions.Regex(".+\r\n.+\r\n\r\n(.+\r\n.+\r\n.+\r\n.+\r\n.+\r\n)+",
System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.Multiline
);
var match = regBackups.Match(strBackups);
if (match.Success)
{
for (var i = 1; i < match.Groups.Count; i++)
{
foreach (var c in match.Groups[i].Captures)
{
Console.WriteLine("=============================");
Console.WriteLine(c);
Console.WriteLine("=============================");
}
}
}
else
Console.WriteLine("<not matched>");
}
Sorry for broken format by multiline strings
Code looks like that without broke formatting:
Split
If you don't need to validate there are 5 consecutive lines, you could simply split by empty lines:
var regBackups = new System.Text.RegularExpressions.Regex("(?:\r\n){2}");
var result = regBackups.Split(strBackups);
foreach (var c in result)
{
Console.WriteLine("=============================");
Console.WriteLine(c);
Console.WriteLine("=============================");
}
This is by far the preferred option.
Example
Match
If you must validate that the text blocks have 5 consecutive lines, you can use the following approach:
var regBackups = new Regex(#"\r\n((?>\r\n.+){5})(?!\r\n.)",
RegexOptions.Compiled
);
foreach (Match m in regBackups.Matches(strBackups))
{
Console.WriteLine("=============================");
Console.WriteLine(m.Groups[1].Value);
Console.WriteLine("=============================");
}
Example
The expression \r\n((?>\r\n.+){5})(?!\r\n.) matches:
\r\n a cr+lf, followed by
\r\n.+ a cr+lf and a line with at least 1 character.
The (?> ... ){5} is to repeat the previous 5 times. It's an atomic group (more efficient in this case) with the quantifier at the end.
(?!\r\n.) not followed by a cr+lf and a character (ie. not followed by another line).

listBox is not displaying manual string inside it, but displaying the 'str' value

Firstly What the hell is that, I think there is no any expression for it but I am asking you specialists:
string onUserName = msj.Substring(3);
lstMsjClient.Items.Add(onUserName + "is online now.");
Here the onUserName can be displayed inside the lstMsjClient but the "is online now" can not be displayed inside the lstMsjClient.
So is not that funny, what is the reason ?
That's soooo interesting.
if you use
string msj = "One";
string onUserName = msj.Substring(3);
lstMsjClient.Items.Add(onUserName + "is online now.");
//O is 0
//n is 1
//e is 2
//after blank 3. index
Output : "is online now."
because Substring(3) mean index start 3.
if you want first 3 chars you must use Substring(0,3)
try this code
string msj = "OneTwoThree";
string onUserName = msj.Substring(3);
listBox1.Items.Add(onUserName + "is online now.");
Output : "TwoThreeis online now." ok?

How to extract address components from a string?

I have a Xamarin Forms application that uses Xamarin. Mobile on the platforms to get the current location and then ascertain the current address. The address is returned in string format with line breaks.
The address can look like this:
111 Mandurah Tce
Mandurah WA 6210
Australia
or
The Glades
222 Mandurah Tce
Mandurah WA 6210
Australia
I have this code to break it down into the street address (including number), suburb, state and postcode (not very elegant, but it works)
string[] lines = address.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
List<string> addyList = new List<string>(lines);
int count = addyList.Count;
string lineToSplit = addyList.ElementAt(count - 2);
string[] splitLine = lineToSplit.Split(null);
List<string> splitList = new List<string>(splitLine);
string streetAddress = addyList.ElementAt (count - 3).ToString ();
string postCode = splitList.ElementAt(2);
string state = splitList.ElementAt(1);
string suburb = splitList.ElementAt(0);
I would like to extract the street number, and in the previous examples this would be easy, but what is the best way to do it, taking into account the number might be Lot 111 (only need to capture the 111, not the word LOT), or 123A or 8/123 - and sometimes something like 111-113 is also returned
I know that I can use regex and look for every possible combo, but is there an elegant built-in type solution, before I go writing any more messy code (and I know that the above code isn't particularly robust)?
These simple regular expressions will account for many types of address formats, but have you considered all the possible variations, such as:
PO Box 123 suburb state post_code
Unit, Apt, Flat, Villa, Shop X Y street name
7C/94 ALISON ROAD RANDWICK NSW 2031
and that is just to get the number. You will also have to deal with all the possible types of streets such as Lane, Road, Place, Av, Parkway.
Then there are street types such as:
12 Grand Ridge Road suburb_name
This could be interpreted as street = "Grand Ridge" and suburb = "Road suburb_name", as Ridge is also a valid street type.
I have done a lot of work in this area and found the huge number of valid address patterns meant simple regexs didn't solve the problem on large amounts of data.
I ended up develpping this parser http://search.cpan.org/~kimryan/Lingua-EN-AddressParse-1.20/lib/Lingua/EN/AddressParse.pm to solve the problem. It was originally written for Australian addresses so should work well for you.
Regex can capture the parts of a match into groups. Each parentheses () defines a group.
([^\d]*)(\d*)(.*)
For "Lot 222 Mandurah Tce" this returns the following groups
Group 0: "Lot 222 Mandurah Tce" (the input string)
Group 1: "Lot "
Group 2: "222"
Group 3: " Mandurah Tce"
Explanation:
[^\d]* Any number (including 0) of any character except digits.
\d* Any number (including 0) of digits.
.* Any number (including 0) of any character.
string input = "Lot 222 Mandurah Tce";
Match match = Regex.Match(input, #"([^\d]*)(\d*)(.*)");
string beforeNumber = match.Groups[1].Value; // --> "Lot "
string number = match.Groups[2].Value; // --> "222"
string afterNumber = match.Groups[3].Value; // --> " Mandurah Tce"
If a group finds no match, match.Groups[i] will return an empty string ("") for that group.
You could check if the content starts with a number for each entry in the splitLine.
string[] splitLine = lineToSplit.Split(addresseLine);
var streetNumber = string.empty;
foreach(var s in splitLine)
{
//Get the first digit value
if (Regex.IsMatch(s, #"^\d"))
{
streetNumber = s;
break;
}
}
//Deal with empty value another way
Console.WriteLine("My streetnumber is " + s)
Yea I think you have to identify what will work.
If:
it is always in the address line and it must always start with a Digit
nothing else in that line can start with a digit (or if something else does you know which always comes in what order, ie the code below will always work if the street number is always first)
you want every contiguous character to the digit that isn't whitespace (the - and \ examples suggest that to me)
Then it could be as simple as:
var regx = new Regex(#"(?:\s|^)\d[^\s]*");
var mtch = reg.Match(addressline);
You would sort of have to sift and see if any of those assumptions are broken.

Format a string into columns

Is there a cool way to take something like this:
Customer Name - City, State - ID
Bob Whiley - Howesville, TN - 322
Marley Winchester - Old Towne, CA - 5653
and format it to something like this:
Customer Name - City, State - ID
Bob Whiley - Howesville, TN - 322
Marley Winchester - Old Towne, CA - 5653
Using string format commands?
I am not too hung up on what to do if one is very long. For example this would be ok by me:
Customer Name - City, State - ID
Bob Whiley - Howesville, TN - 322
Marley Winchester - Old Towne, CA - 5653
Super Town person - Long Town Name, WA- 45648
To provide some context. I have a drop down box that shows info very similar to this. Right now my code to create the item in the drop down looks like this:
public partial class CustomerDataContract
{
public string DropDownDisplay
{
get
{
return Name + " - " + City + ", " + State + " - " + ID;
}
}
}
I am looking for a way to format this better. Any ideas?
This is what I ended up with:
HttpContext.Current.Server.HtmlDecode(
String.Format("{0,-27} - {1,-15}, {2, 2} - {3,5}",
Name, City, State, ID)
.Replace(" ", " "));
The HtmlDecode changes the to a space that can withstand the space removing formatting of the dropdown list.
You can specify the number of columns occupied by the text as well as alignment using Console.WriteLine or using String.Format:
// Prints "--123 --"
Console.WriteLine("--{0,-10}--", 123);
// Prints "-- 123--"
Console.WriteLine("--{0,10}--", 123);
The number specifies the number of columns you want to use and the sign specifies alignment (- for left alignment, + for right alignment). So, if you know the number of columns available, you could write for example something like this:
public string DropDownDisplay {
get {
return String.Format("{0,-10} - {1,-10}, {2, 10} - {3,5}"),
Name, City, State, ID);
}
}
If you'd like to calculate the number of columns based on the entire list (e.g. the longest name), then you'll need to get that number in advance and pass it as a parameter to your DropDownDisplay - there is no way to do this automatically.
In addition to Tomas's answer I just want to point out that string interpolation can be used in C# 6 or newer.
// with string format
var columnHeaders1 = string.Format($"|{0,-30}|{1,-4}|{2,-15}|{3,-30}|{4,-30}|{5,-30}|{6,-30}", "ColumnA", "ColumnB", "ColumnC", "ColumnD", "ColumnE", "ColumnF", "ColumnG");
// with string interpolation
var columnHeaders2 = $"|{"ColumnA",-30}|{"ColumnB",-4}|{"ColumnC",-15}|{"ColumnD",-30}|{"ColumnE",-30}|{"ColumnF",-30}|{"ColumnG",-30}";
I am unable to add a comment above, but in the accepted answer it was stated:
If you'd like to calculate the number of columns based on the entire list (e.g. the longest name), then you'll need to get that number in advance and pass it as a parameter to your DropDownDisplay - there is no way to do this automatically.
This can in fact be done programmatically at runtime by creating the format string 'on the fly':
string p0 = "first";
string p1 = "separated by alignment value x";
int x = n * 10; // calculate the alignment x as needed
// now use x to give something like: {0,-20}, {1}
string fmt = "{0,-" + x + "},{1}"; // or whatever formatting expression you want
// then use the fmt string
string str = string.Format(fmt, p0, p1)
// with n = 2 this would give us
"first ,separated by alignment value x"

HOW TO SElect line number in TextBox Multiline

I have large text in System.Windows.Forms.TextBox control in my form (winforms), vs 2008.
I want find a text, and select the line number where I've found that text.
Sample,
I have fat big text, and I find "ERROR en línea", and I want select the line number in textbox multiline.
string textoLogDeFuenteSQL = #"SQL*Plus: Release 10.1.0.4.2 - Production on Mar Jun 1 14:35:43 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
******** MORE TEXT ************
Conectado a:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, Data Mining and Real Application Testing options
WHERE LAVECODIGO = 'CO_PREANUL'
ERROR en línea 2:
ORA-00904: ""LAVECODIGO"": identificador no v?lido
INSERT INTO COM_CODIGOS
ERROR en línea 1:
ORA-00001: restricción única (XACO.INX_COM_CODIGOS_PK) violada";
******** MORE TEXT ************
Any sample code about it ?
You might want to look at TextBoxBase.GetLineFromCharIndex method. This method retrieves the line number of character position within the textbox.
string str = textBox2.Text;
int index = textBox1.Text.IndexOf(str);
if (index !=-1)
{
int lineNo = textBox1.GetLineFromCharIndex(index);
}
"This method enables you to determine the line number based on the character index specified in the index parameter of the method. The first line of text in the control returns the value zero. The GetLineFromCharIndex method returns the physical line number where the indexed character is located within the control."
EDIT: This only finds the occurrences of the searched text. To compute the line numbers use Fredrik's answer.
using System.Text.RegularExpressions;
public static void FindErrorInText(string input)
{
Regex rgx = new Regex("ERROR en linea \d*", RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(input);
if (matches.Count > 0)
{
Console.WriteLine("{0} ({1} matches):", input, matches.Count);
foreach (Match match in matches)
Console.WriteLine(" " + match.Value);
}
}

Categories

Resources