Insert WhiteSpaces and remove last characters from string - vb.net

I need a prepared string for my Visual Studio 2010 macro. The string should be the document name (document.Name) but without the file extension (for example .cs) and after each upper case should be a white space.
Example:
document.Name = TestFileName.cs
How can I get this:
"Test File Name"

For trivial cases (non consecutive upper-case):
file = IO.Path.GetFileNameWithoutExtension(file)
file = System.Text.RegularExpressions.Regex.Replace(file, "([a-z0-9])([A-Z])", "$1 $2")

Here is a basic framework
String PreString = "getAllItemsByID";
System.Text.StringBuilder SB = new System.Text.StringBuilder();
foreach (Char C in PreString)
{
if (Char.IsUpper(C))
SB.Append(' ');
SB.Append(C);
}
Response.Write(SB.ToString());
You may need to add a few checks:-When the very first char is Uppercase not to add a space.-When a word like ID is encountered, remove the last space.
[OR try this]
This will find each occurance of a lower case character followed by an upper case character, and insert a space between them:
s = s.replace(/([a-z])([A-Z])/g, '$1 $2')

Related

ALIGN_JUSTIFIED for iText list item

I want to set alignment to list items by writing this code -
ListItem alignJustifiedListItem =
new ListItem(bundle.getString(PrintKeys.AckProcess), normalFont8);
alignJustifiedListItem.setAlignment(Element.ALIGN_JUSTIFIED);
I see this doesn't make any change on alignment (defaulted as left aligned). Changing it to
alignJustifiedListItem.setAlignment(Element.ALIGN_JUSTIFIED_ALL); is actually working but then the last line of the content also expands (as mentioned in doc, as well)
I dont understand when ListItem extends Paragraph, how setAlignment() behaviour can change. I don't see any overriding as well.
Please take a look at the ListAlignment example.
In this example, I create a list with three list items of which I set the alignment to ALIGN_JUSTIFIED:
List list = new List(List.UNORDERED);
ListItem item = new ListItem(text);
item.setAlignment(Element.ALIGN_JUSTIFIED);
list.add(item);
text = "a b c align ";
for (int i = 0; i < 5; i++) {
text = text + text;
}
item = new ListItem(text);
item.setAlignment(Element.ALIGN_JUSTIFIED);
list.add(item);
text = "supercalifragilisticexpialidocious ";
for (int i = 0; i < 3; i++) {
text = text + text;
}
item = new ListItem(text);
item.setAlignment(Element.ALIGN_JUSTIFIED);
list.add(item);
document.add(list);
If you look at the result, you can see that the alignment works as expected:
I deliberately introduced a very long word such as "supercalifragilisticexpialidocious" to show you that all lines but the last are indeed justified.
Update:
In a comment, you claim that the alignment is wrong when you introduce the \ character, and you want me to fix iText. However, there is nothing to fix.
I have adapted the original example like this:
text = "a b c align ";
for (int i = 0; i < 5; i++) {
text = text + "\\" + text;
}
item = new ListItem(text);
item.setAlignment(Element.ALIGN_JUSTIFIED);
list.add(item);
text = "supercalifragilisticexpialidocious ";
text = text + text;
text = text + text;
text = text + "\n" + text;
item = new ListItem(text);
item.setAlignment(Element.ALIGN_JUSTIFIED);
list.add(item);
In the first case, I have introduce the \ character. This didn't change anything to the behavior of the ListItem. In the second case, I introduce a newline character. The result was as expected: a newline character was introduced and the last line of every "paragraph" that was defined by the newline character was indeed not justified. That is what one would normally expect. I would introduce a bug if I would change this.
This is the screen shot of the result:
The introduction of the '\' character in the lines with "a b c align " doesn't have any effect on the alignment. The introduction of the newline half way the "supercalifragilisticexpialidocious " part breaks the list item in two parts. The final line of each part is not justified, which is the desired behavior.
If you do not want this desired behavior, you have to parse the content first and remove all newlines characters (carriage return and line feed).
Update:
In a new comment, you mention the '\' character as an escape character for the ''' character (actually the \' character). I have adapted the original example once more:
text = "a b c\' align ";
for (int i = 0; i < 5; i++) {
text = text + text;
}
item = new ListItem(text);
item.setAlignment(Element.ALIGN_JUSTIFIED);
list.add(item);
The result looks like this:
The text is justified correctly. However, I can imagine that problems can occur if you handle Strings with escape characters incorrectly. In this case, the '\'' character was hardcoded. If you obtain the String from a database and you read that String incorrectly, then you can have strange results. Especially from my days as a PHP developer, I remember instances where a single quote ended up to be stored like this '\\\'' in a database if you didn't watch out.

Printing newline in TextBlock in windows 8

I want to assign the TextBlock's Text in my code behind and display it on the screen. It might contain new line character also. But somehow the TextBlock is not printing that character. I have used the following combinations in my text to print the new line character
\n
\r\n
Has anyone done this? can you help me?
In XAML you can do like this
<TextBlock>Hello how are you?<LineBreak/>I'm fine</TextBlock>
In code you can do like this
textBlock.Text = "Hello how are you?\nI'm fine.";
Both are working for me.
Edited
For your scenario you can do this
string str = #"Hello how are you?\nI'm fine.";//This is your actual string containing \n as character
or in your case
string str = _arr[index];
str = str.Replace(#"\n", "\n");
Replace "\n" string with new line character.
P.S. It will create problem where you actually want to show \n string instead of new line character.

Is there a built-in function to extract all characters in a string up until the first occurrence of a space?

Is there a built-in function to extract all characters in a string up until the first occurrence of a space?
Say the string is:
Methicillin-resistant staphylococcus aureus
I want to be able to get the substring:
Methicillin-resistant
You can do it in two functions:
newstring = mystring.Substring(0, mystring.IndexOf(" "))
Although that will fail if there's no space in mystring.
So you could pull out mystring.IndexOf(" ") into a variable and check whether it's -1 (no space found) before you try to use it in Substring.
The first solution you can use is a simple IndexOf
string GetFirstWord(string source)
{
int index = source.IndexOf(" ");
if (index == -1) return source;
else return source.Substring(0, index);
}
The second solution can be used if you want to keep all words into a string array.
string[] GetWords(string source)
{
return source.Split(' ');
}
if you only want the first word, you can use it like this :
string word = GetWords("Methicillin-resistant staphylococcus aureus")[0];
And a VB.NET solution. No, it can't be done with one built-in method; you need two:
Left(myString, InStr(myString, " ") - 1)
And like the other solutions you need to check InStr doesn't return 0 if myString may not contain a space.

How to bulk insert from CSV when some fields have new line character?

I have a CSV dump from another DB that looks like this (id, name, notes):
1001,John Smith,15 Main Street
1002,Jane Smith,"2010 Rockliffe Dr.
Pleasantville, IL
USA"
1003,Bill Karr,2820 West Ave.
The last field may contain carriage returns and commas, in which case it is surrounded by double quotes. And I need to preserve those returns and commas.
I use this code to import CSV into my table:
BULK INSERT CSVTest
FROM 'c:\csvfile.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
SQL Server 2005 bulk insert cannot figure out that carriage returns inside quotes are not row terminators.
How to overcome?
UPDATE:
Looks like the only way to keep line breaks inside a field is to use different row separator. So, I want to mark all row separating line breaks by putting a pipe in front of them. How can I change my CSV to look like this?
1001,John Smith,15 Main Street|
1002,Jane Smith,"2010 Rockliffe Dr.
Pleasantville, IL
USA"|
1003,Bill Karr,2820 West Ave.|
Bulk operations on SQL Server do not specifically support CSV even though they can import them if the files are carefully formatted. My suggestion would be to enclose all field values in quotes. BULK INSERT might then allow the carriage returns within a field value. If it does not, then your next solution might be an Integration Services package.
See Preparing Data for Bulk Export or Import for more.
you can massage these line breaks into one line with a script, eg you can use GNU sed to remove line breaks. eg
$ more file
1001,John Smith,15 Main Street
1002,Jane Smith,"2010 Rockliffe Dr.
Pleasantville, IL
USA"
1003,Bill Karr,"2820
West Ave"
$ sed '/"/!s/$/|/;/.*\".*[^"]$/{ :a;N };/"$/ { s/$/|/ }' file
1001,John Smith,15 Main Street|
1002,Jane Smith,"2010 Rockliffe Dr.
Pleasantville, IL
USA"|
1003,Bill Karr,"2820
West Ave"|
then you can bulk insert.
Edit:
Save this :/"/!s/$/|/;/.*\".*[^"]$/{ :a;N };/"$/ { s/$/|/ } in a file , say myformat.sed. then do this on the command line
c:\test> sed.exe -f myformat.sed myfile
According to the source of all knowledge (Wikipedia), csv uses new lines to separate records. So what you have is not valid csv.
My suggestion is that you write a perl program to process your file and add each record to the db.
If you're not a perl person, then you could use a programming site or see if some kind SO person will write the parsing section of the program for you.
Added:
Possible Solution
Since the OP states that he can change the input file, I'd change all the new lines that do not follow a " to be a reserved char sequence, eg XXX
This can be an automated replacement in many editors. In Windows, UltraEdit includes regexp find/replace functionality
Then import into the dbms since you'll no longer have the embedded new lines.
Then use SQL Replace to change the XXX occurrences back into new lines.
If you have control over the contents of the CSV file, you could replace the in-field line breaks (CRLF) with a non-linebreak character (perhaps just CR or LF), then run a script after the import to replace them with CRLF again.
This is how MS Office products (Excel, Access) deal with this problem.
OK, here's a small Java program that I end up writing to solve the problem.
Comments, corrections and optimizations are welcome.
import java.io.*;
public class PreBulkInsert
{
public static void main(String[] args)
{
if (args.length < 3)
{
System.out.println ("Usage:");
System.out.println (" java PreBulkInsert input_file output_file separator_character");
System.exit(0);
}
try
{
boolean firstQuoteFound = false;
int fromIndex;
int lineCounter = 0;
String str;
BufferedReader in = new BufferedReader(new FileReader(args[0]));
BufferedWriter out = new BufferedWriter(new FileWriter(args[1]));
String newRowSeparator = args[2];
while ((str = in.readLine()) != null)
{
fromIndex = -1;
do
{
fromIndex = str.indexOf('"', fromIndex + 1);
if (fromIndex > -1)
firstQuoteFound = !firstQuoteFound;
} while (fromIndex > -1);
if (!firstQuoteFound)
out.write(str + newRowSeparator + "\r\n");
else
out.write(str + "\r\n");
lineCounter++;
}
out.close();
in.close();
System.out.println("Done! Total of " + lineCounter + " lines were processed.");
}
catch (IOException e)
{
System.out.println(e.getMessage());
System.exit(1);
}
}
}
You cannot import this unless the CSV is in valid format. So, you have to either fix the dump or manually using search & replace fix the unwanted new line characters.

How to filter out some vulnerability causing characters in query string?

I need to filter out characters like /?-^%{}[];$=*`#|&#'\"<>()+,\. I need replace this with empty string if it is there in the query string. Please help me out. I am using this in ASP pages.
Best idea would be to use a function something along the lines of:
Public Function MakeSQLSafe(ByVal sql As String) As String
'first i'd avoid putting quote chars in as they might be valid? just double them up.
Dim strIllegalChars As String = "/?-^%{}[];$=*`#|&#\<>()+,\"
'replace single quotes with double so they don't cause escape character
If sql.Contains("'") Then
sql = sql.Replace("'", "''")
End If
'need to double up double quotes from what I remember to get them through
If sql.Contains("""") Then
sql = sql.Replace("""", """""")
End If
'remove illegal chars
For Each c As Char In strIllegalChars
If sql.Contains(c.ToString) Then
sql = sql.Replace(c.ToString, "")
End If
Next
Return sql
End Function
This hasn't been tested and it could probably be made more efficient, but it should get you going. Wherever you execute your sql in your app, just wrap the sql in this function to clean the string before execution:
ExecuteSQL(MakeSQLSafe(strSQL))
Hope that helps
As with any string sanitisation, you're much better off working with a whitelist that dictates which characters are allowed, rather than a blacklist of characters that aren't.
This question about filtering HTML tags resulted in an accepted answer suggesting the use of a regular expression to match against a whitelist: How do I filter all HTML tags except a certain whitelist? - I suggest you do something very similar.
I'm using URL Routing and I found this works well, pass each part of your URL to this function. It's more than you need as it converts characters like "&" to "and", but you can modify it to suit:
public static string CleanUrl(this string urlpart) {
// convert accented characters to regular ones
string cleaned = urlpart.Trim().anglicized();
// do some pretty conversions
cleaned = Regex.Replace(cleaned, " ", "-");
cleaned = Regex.Replace(cleaned, "#", "no.");
cleaned = Regex.Replace(cleaned, "&", "and");
cleaned = Regex.Replace(cleaned, "%", "percent");
cleaned = Regex.Replace(cleaned, "#", "at");
// strip all illegal characters like punctuation
cleaned = Regex.Replace(cleaned, "[^A-Za-z0-9- ]", "");
// convert spaces to dashes
cleaned = Regex.Replace(cleaned, " +", "-");
// If we're left with nothing after everything is stripped and cleaned
if (cleaned.Length == 0)
cleaned = "no-description";
// return lowercased string
return cleaned.ToLower();
}
// Convert accented characters to standardized ones
private static string anglicized(this string urlpart) {
string beforeConversion = "àÀâÂäÄáÁéÉèÈêÊëËìÌîÎïÏòÒôÔöÖùÙûÛüÜçÇ’ñ";
string afterConversion = "aAaAaAaAeEeEeEeEiIiIiIoOoOoOuUuUuUcC'n";
string cleaned = urlpart;
for (int i = 0; i < beforeConversion.Length; i++) {
cleaned = Regex.Replace(urlpart, afterConversion[i].ToString(), afterConversion[i].ToString());
}
return cleaned;
// Spanish : ÁÉÍÑÓÚÜ¡¿áéíñóúü"
}