line break in string in c# - c#-4.0

I'm loading a text from the resource loader which includes '\n' to indicate a new line. A textblock takes this text and displays it, but I can't see the line break? I'm also trying to replace every '\n' by Environment.NewLine, but nothing happens. What can I do?
Here is a little bit code:
TextBlock text = new TextBlock();
text.FontSize = 18;
var str = loader.GetString("AboutText");
//str.Replace("\n", Environment.NewLine);
text.Text = str;
text.TextAlignment = TextAlignment.Justify;
text.TextWrapping = TextWrapping.Wrap;
text.Margin = new Thickness(10, 10, 10, 10);

It looks like the Resource file is escaping \n to \\n, this means that there are basically 2 solutions to solve this.
you can either
var str = Regex.Unescape(loader.GetString("AboutText"));
or in your resx file you can replace \n with normal break line by pressing Shift Enter.

Try "\r\n". It works for me.
TextBlock text = new TextBlock();
text.FontSize = 18;
var str = "Hello\r\nWorld";
text.Text = str;
text.TextAlignment = TextAlignment.Justify;
text.TextWrapping = TextWrapping.Wrap;
text.Margin = new Thickness(10, 10, 10, 10);
layoutRoot.Children.Add(text);

Related

How in Scriptui (Extendscript) can I use subscripts or superscripts in button title?

How in Scriptui (Extendscript) can I use subscripts or superscripts in button title? I'm using buttonTitle as an example for any character string that is displayed on the dialog/palette.
In this example:
var win = new Window("dialog");
var buttonTitle = "Button2";
win.aButton = win.add("button", undefined, buttonTitle);
win.show();
how do I code buttonTitle so it is "ButtonX" where X is either a superscript or subscript 2? Or maybe letter Y? Numbers are available in some fonts but letters probably aren't. I would like a general solution.
I appreciate your time.
Thanks,
RONC
This is a screen plot of scriptui problem:
RONC
Just saw this today, so sorry it took so long.
You can try using unicode codes for this. See the tables on this page:
https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts
But those characters sure are tiny! There are codes for letter characters available, but I didn't test any. Here's some code to test, with notes:
function buildUI(this_obj_) {
var win = (this_obj_ instanceof Panel)
? this_obj_
: new Window('palette', 'Script Window',[147,196,528,330]);
// \u00B2 for super 2, \u00B3 for super 3; the rest are \u2070 for super zero, \u2071 for super one, \u2074 for super four, \u2075 for super five, etc...
win.xui_ui_button5 = win.add('button', [49,24,177,54], 'Super\u00B2');
win.xui_ui_button5.onClick = function () {this.parent.close(1);}
// \u2081 for sub 1, \u2082 for sub 2, \u2083 for sub 3, etc...
win.xui_ui_button6 = win.add('button', [49,65,177,95], 'Sub\u2082');
win.xui_ui_button6.onClick = function () {this.parent.close(1);}
return win
}
var w = buildUI(this);
if (w.toString() == "[object Panel]") {
w;
} else {
w.show();
}
[EDIT: Here's a screenshot of the palette window this code works to create in After Effects]
[EDIT 2: Here's code that works in Photoshop using a modal dialog (and I'm testing on an old version of PS on this machine, but it should work, but look different on the latest]
var win = new Window('dialog', 'Script Window',[670,456,1160,598]);
var w = buildUI();
if (w != null) {
w.show();
}
function buildUI() {
if (win != null) {
win.xui_ui_button5 = win.add('button', [64,11,231,51], 'Super\u00B2');
win.xui_ui_button5.onClick = function () {this.parent.close(1);}
win.xui_ui_button6 = win.add('button', [264,63,431,103], 'Sub\u2082');
win.xui_ui_button6.onClick = function () {this.parent.close(1);}
}
return win
}
Here's super x, y and z working in PS, using codes \u02E3, \u02B8 and \u1DBB:

How to delete Duplicate Characters from a given region in String?(given that we have the buffer from which we read the string from)

I am currently doing, my assignment from programming at first year of studies. We are given variables var buffer, marker, cursor, paste and our task is to read a certain area of the buffer, and in that area remove any duplicate characters, and paste that area into buffer again so that it is updated.
Bear in mind that I am performing tests(J-Unit), on my program and depending on how many it passes I get adequate mark. This function currently did not pass any of the tests. Here is the code:
**class** Buffer(s: String) {
import scala.collection.mutable.StringBuilder
private var buffer: StringBuilder = new StringBuilder(s)
private var cursor: Int = 0 private var marker: Int = 0
private var paste: String = ""
private def end: Int = buffer.length
private def lwr: Int = Math.min(marker, cursor)
private def upr: Int = Math.max(marker, cursor)
def dd() {
var MarkerToCursor = ""
var CursorToMarker = ""
var x = ""
var y = ""
//function whcih higlists a certain and then given that reads in characters of the buffer
if(marker < cursor ){
for(x <- marker until cursor)
MarkerToCursor = MarkerToCursor + buffer.charAt(x)
MarkerToCursor.toString
x = MarkerToCursor.distinct
//inserting the highlated area with the string into the buffer and updating it
buffer = new StringBuilder(getString.substring(0, marker) + y+ getString.substring(cursor, end))
cursor = marker + x.length
}
else{
// the same function as the one above except the region consists of charcyters from marker up to cursor.Eveyrything else is the same
for(x <- cursor until marker)
CursorToMarker = CursorToMarker + buffer.charAt(x)
CursorToMarker.toString
y = CursorToMarker.distinct
buffer = new StringBuilder(getString.substring(0, cursor) +y + getString.substring(marker, end))
marker = cursor + y.length }
}
}
}
As #Dima says in the comments, the logic for this is straightforward.
def removeDuplicatesInRange(str: String, start: Int, end: Int): String =
str.take(start) + str.slice(start, end).distinct + str.drop(end)
Note that this creates a new string from the old, rather than using var or updating data in-place. Avoiding mutable data like this is one of the key elements of functional programming, which is the main focus of the Scala language.

Insert Charc into String in a Range where character X exist at Index - swift 2.0

Please could somebody help me.
I am trying to add a new line (\n) into an existing string.
Lets say the string is 20+ Characters long, I want to find a space " " between the range of 15 and 20 then inset a new line (\n) just after the index to where the char " " (space) is
I hope that makes sense :F
Code i have for this so far is as follows
var newString = string
newString[newString.startIndex..< newString.startIndex.advancedBy(16)]
/* let startIndex = newString.startIndex.advancedBy(16)
let endIndex = newString.endIndex
let newRange = startIndex ..< endIndex
print("start index = \(newRange)")*/
let range: Range<String.Index> = newString.rangeOfString(" ")!
let index: Int = newString.startIndex.distanceTo(range.startIndex)
newString.insert("\n", atIndex: newString.startIndex.advancedBy(index))
label.text = newString
if I try the following
let newIndex = name.startIndex.advancedBy(19).distanceTo(range.endIndex)
I get the error message
fatal error: can not increment endIndex
Ive got a feeling I'm on the right tracks but the above will inset a new line at the index where space first appears in the string and not between the index of e.g. 15 and 20
Thanks for your help in advance
Thomas
The following finds the first space in the range 15..<END_OF_YOUR_STRING, and replaces it with a new line (\n). In your question you stated you explicitly wanted to look for a space in range 15...20, and also insert a new line after the space. Below I have assumed that you actually want:
To replace the space by a new line, since you'll otherwise have a trailing space on the line following the line break.
To search for the first space starting at index 15, but continuing until you find one (otherwise: if you find no space within range 15...20, no line break should be inserted?).
Both of these deviations from your question can be quite easily reverted, so tell me if you'd prefer me to follow your instructions to specifically to the point (rather than including my own reason), and I'll update this answer.
Solution as follows:
var foo = "This is my somewhat long test string"
let bar = 15 /* find first space " " starting from index 'bar' */
if let replaceAtIndex = foo[foo.startIndex.advancedBy(bar)..<foo.endIndex]
.rangeOfString(" ")?.startIndex.advancedBy(bar) {
foo = foo.stringByReplacingCharactersInRange(
replaceAtIndex...replaceAtIndex, withString: "\n")
}
print(foo)
/* This is my somewhat
long test string */
Note that there is a off-by-one difference between finding a space in the range of 15 to 20 and the 15:th to 20:th character (the latter is in the range 14...19). Above, we search for the first space starting at the 16th character (index 15).
Thanks to dfri for pointing me in the right direction. Although his answer was correct for my problem specifically I've provided the following code to help
if string.characters.count >= 20
{
let bar = 15 // where to split the code to begin looking for the character
let beginString = string.substringWithRange(name.startIndex..<name.startIndex.advancedBy(bar))
var endString = string[string.startIndex.advancedBy(bar)..<name.endIndex]
if endString.containsString(" ")
{
let range = endString.rangeOfString(" ")
if let i = range
{
endString.insert("\n", atIndex:i.startIndex.advancedBy(1) )
let newString = "\(beginString)\(endString)"
label.text = newString
}
}
}

Read specific string from each line of text file using BufferedReader in java

my text file :
3.456 5.234 Saturday 4.15am
2.341 6.4556 Saturday 6.08am
At first line, I want to read 3.456 and 5.234 only.
At second line, I want to read 2.341 and 6.4556 only.
Same goes to following line if any.
Here's my code so far :
InputStream instream = openFileInput("myfilename.txt");
if (instream != null) {
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line=null;
while (( line = buffreader.readLine()) != null) {
}
}
Thanks for showing some effort. Try this
while (( line = buffreader.readLine()) != null) {
String[] parts = line.split(" ");
double x = Double.parseDouble(parts[0]);
double y = Double.parseDouble(parts[1]);
}
I typed this from memory, so there might be syntax errors.
int linenumber = 1;
while((line = buffreader.readLine()) != null){
String [] parts = line.split(Pattern.quote(" "));
System.out.println("Line "+linenumber+"-> First Double: "+parts[0]+" Second Double:"
+parts[1]);
linenumber++;
}
The code of Bilbert is almost right. You should use a Pattern and call quote() for the split. This removes all whitespace from the array. Your problem would be, that you have a whitespace after every split in your array if you do it without pattern. Also i added a Linenumber to my output, so you can see which line contains what. It should work fine

ActionScript: Insert text in string at random places?

I'm building an ActionScript program in which I need to insert text into another string at random positions.
I have the text which strings will be inserted into; and I have the strings which will be inserted as an array.
However, I don't know how to go about inserting the elements of this array into the other string at random positions.
Any help will be highly appreciated.
The answer to your modified question:
var stringsToInsert:Array = ["abc", "def", "ghi"];
var text:String = "here is some text"
var textArr:Array = text.split(" ");
while(stringsToInsert.length)
{
var randomPosition:uint = Math.round( Math.random() * textArr.length );
textArr.splice(randomPosition, 0, stringsToInsert.pop());
}
text = textArr.join(" ");
trace(text);
while(arrayOftringsToInsert.length)
{
var randomPosition:uint = Math.round( Math.random() * text.length )
text = text.slice(0, randomPosition) + arrayOftringsToInsert.pop() + text.slice(randomPosition + 1, text.length)
}

Resources