Printing newline in TextBlock in windows 8 - 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.

Related

JavaFx Label - how to force a new line when displaying data from sqlite?

I understand that in JavaFX we can force a new line in a Label by a code like this:
label.setText("Line one \n Line two");
My issue is this:
I want to display some poetry from an SQLite database. So I tried having the text like this in the database:
Roses are red \n violets are blue
Unfortunately, when I run the program, it simply displays the text including the \n and does not force a new line.
Could tell me how I make it work?
You shouldn't have used \n in your DB rows. E.g. the following code works without the need of special handling of line breaks:
String url = "jdbc:sqlite:poetryDB.db";
try (Connection connection = DriverManager.getConnection(url)) {
try (Statement s = connection.createStatement()) {
s.execute("CREATE TABLE IF NOT EXISTS poem(text TEXT NOT NULL)");
}
// insert poem containing newline
try (PreparedStatement ps = connection.prepareStatement("INSERT INTO poem (text) VALUES (?)")) {
ps.setString(1, "Roses are red \n violets are blue");
ps.executeUpdate();
}
// print content of the db table
try (Statement s = connection.createStatement()) {
ResultSet rs = s.executeQuery("SELECT text FROM POEM");
while (rs.next()) {
System.out.println(rs.getString(1).replace("\\n", "\n"));
System.out.println("----------------");
}
}
}
If you indeed want to store newlines as \n in the db, you need to replace \n with newlines after retrieving the data from the db:
String value = resultSet.getString(index).replace("\\n", "\n");
If you want to fix the db however, all you need to do is run a update that replaces the occurances of \n:
try (Statement s = connection.createStatement()) {
s.executeUpdate("UPDATE poem SET text = REPLACE(text, '\\n', '\n')");
}

Select everything from //

I'm making like a "mini programming language" in visual basic.
Mostly just for practice and for fun.
I just have one problem. I want to make a commenting system.
I got an idea how it would work, but i don't know how to do it.
So this is what i want to do:
I want to start to select all text from //
So for example, if i write:
print = "Hello World!"; //This is a comment!
it will select everything from the // so it will select
//This is a comment!
Then i would just replace the selected text with nothing.
You can use String.IndexOf + Substring:
Dim code = "Dim print = ""Hello World!""; //This is a comment!"
Dim indexOfComment = code.IndexOf("//")
Dim comment As String = Nothing
If indexOfComment >= 0 Then comment = code.Substring(indexOfComment)
If you want the part before the comment dont use String.Replace but also Substring or Remove:
code.Substring(0, indexOfComment)

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.

Insert WhiteSpaces and remove last characters from string

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

vim variable declaration and breaks

Normally I use "\n" to add extra information in an inputdialog window.
p.e.
let myvar = "How many characters do you want to add? \n (max. 10)"
let f = inputdialog(myvar)
The break works fine.
but when I add a variable in the text, the break doesn't work anymore:
let myvar = 'How many ' .a:type. ' do you want to add? \n (max. 10)'
let f = inputdialog(myvar)
The variable is a:type is inserted well but the break doesn't work. Why?
Neither of you examples work: you should have been using double strokes instead of single.