For some reason, I want to get the sentences of my selected text into a range instead of a range collection.
For example, the sample text is: This is hello world. Welcome everyone. Enjoy the time.
In the text, I selected ld. Wel.
What I want is a range of "This is hello world. Welcome everyone." However, context.document.getSelection().getTextRanges([". "], true) returns a range collection.
I got the answer. The key is at the expandTo function.
let rangeCollection = context.document.getSelection().getTextRanges([". "], true);
let range = rangeCollection.items[0].expandTo(rangeCollection.items[rangeCollection.items.length-1]);
Related
I want to split a paragraph with multiple sentences part by part. Specifically, I want to show 1st sentence in a Text widget and then remaining of the sentence in another Text widget. How can I achieve this?
var str = 'Taste the crunchy and spicy goodness of our potato chips! Our chips are sure to tantalize your taste buds, leaving you wanting more! And with their irresistible flavor, you won't be able to resist the temptation to indulge. So why not grab a bag today and experience the deliciousness of our potato chips? You won't regret';
var parts = str.split(' ');
var prefix = parts[0].trim();
var prefix1 = parts[1].trim();
print(prefix); // Taste
print(prefix1); // the
It prints out word by word but not sentence by sentence..
If your sentences are separated by periods (.), you can do something like this:
String str = "first. second. third.";
// get the position of the first period
int index = str.indexOf(".");
// get first sentence
var firstPart = str.substring(0, index + 1).trim();
// get the rest of sentences
var secondPart = str.substring(index + 1).trim();
update:
if you sentences can end with (. or ! or ?), you can use a RegEx as an argument to indexOf method
int index = str.indexOf(RegExp(r'[\.\?\!]'));
I am trying to write a routine that counts the characters in a global.
These are the globals I set and the characters I would like counted.
s ^XA(1)="SYLVESTER STALLONE, BRUCE WILLIS, AND ARNOLD SCHWARZENEGGER WERE DISCUSSING THEIR "
s ^XA(2)="NEXT PROJECT, A BUDDY FILM IN WHICH BAROQUE COMPOSERS TEAM UP TO BATTLE BOX-OFFICE IRRELEVANCE "
s ^XA(3)="EVERY HAD BEEN SETTLED EXCEPT THE CASTING. "
s ^XA(4)="""ARNOLD CAN BE PACHELBEL,"" STALLONE. ""AND I WANT TO PLAY MOZART. """
s ^XA(5)="""NO WAY!"" SAID WILLIS. ""YOU'RE NOT REMOTELY MOZARTISH. """
s ^XA(6)="""I'LL PLAY MOZART. YOU CAN BE HANDEL. """
s ^XA(7)="""YOU BE HANDEL!"" YELLED STALONE. ""I'M PLAYING MOZART! """
s ^XA(8)="FINALLY, ARNOLD SPOKE ""YOU WILL PLAY HANDEL,"" HE SAID TO WILLIS. "
s ^XA(9)="""AND YOU,"" HE SAID TO STALLONE, ""THEN WHO ARE YOU GONNA PLAY? """
s ^XA(10)="""OH YEAH?"" SAID STALLONE, ""THEN WHO ARE YOU GONNA PLAY? """
s ^XA(11)="ARNOLD ROSE FROM THE TABLE AND DONNED A PAIR OF SUNGLASSES. "
s ^XA(12)="I'LL BE MOZART."
If I understood your question correctly, and you just need the total count of all characters in a global, here you go:
set key = ""
for {
set key = $Order(^XA(key))
quit:key=""
for i=1:1:$Length(^XA(key)) {
set char = $Extract(^XA(key), i)
set count(char) = $get(count(char)) + 1
}
}
zwrite count // or just return count
As for your example, this will produce the following output:
count(" ")=112
count("!")=3
count("""")=24
count("'")=4
count(",")=9
count("-")=1
count(".")=11
count("?")=3
count("A")=54
count("B")=12
count("C")=13
count("D")=23
count("E")=60
count("F")=6
count("G")=8
count("H")=20
count("I")=28
count("J")=1
count("K")=1
count("L")=48
count("M")=11
count("N")=39
count("O")=44
count("P")=13
count("Q")=1
count("R")=28
count("S")=29
count("T")=33
count("U")=13
count("V")=3
count("W")=11
count("X")=3
count("Y")=21
count("Z")=6
Hope this helps!
This question already has answers here:
Any way to replace characters on Swift String?
(23 answers)
Closed 7 years ago.
I'm trying to create a simple iOS app that takes user input ( a city ) and searches a website for that city, and then will display the forecasts for that city.
What I'm currently stuck on and unable to find much documentation that isn't overwhelming is how I can be sure that the user input will translate well to a URL if there are more then one words in the name of the city.
aka if a user inputs Salt Lake City into my text field, how can I write an if else statement that determines the amount of spaces, and if the amount of spaces is greater than 0 will convert those spaces to "-".
So far I've tried creating an array out of the string, but still can't figure out how I can append a - to each element in the array. I don't think it's possible.
Does anyone know how I can do what I'm trying to do? Or am I approaching it the incorrect way?
Here's a poor first attempt. I know this doesn't work, but hopefully it explains it a bit more of what I'm trying to accomplish than my text above.
var cityText = "Salt Lake City"
let cityArray = cityText.componentsSeparatedByString(" ")
let combineDashUrl = cityArray[0] + "-" + cityArray[1] + "-" + cityArray[2]
print(combineDashUrl)
Assuming there are never multiple spaces in a row you should be able to use stringByReplacingOccurrencesOfString.
let cityText = "Salt Lake City"
let newCityText = cityText.stringByReplacingOccurrencesOfString(
" ",
withString: "-")
Replacing variable numbers of spaces with a dash would be more complicated. I'd probably use regular expressions for that.
You can use map over the array of characters to transform spaces into hyphens.
let city = "Salt Lake City"
let hyphenatedCity = String(city.characters.map{$0 == " " ? "-" : $0})
I have a word that is being displayed into a label. Could I program it, where it will only show the last 2 characters of the word, or the the first 3 only? How can I do this?
Swift's string APIs can be a little confusing. You get access to the characters of a string via its characters property, on which you can then use prefix() or suffix() to get the substring you want. That subset of characters needs to be converted back to a String:
let str = "Hello, world!"
// first three characters:
let prefixSubstring = String(str.characters.prefix(3))
// last two characters:
let suffixSubstring = String(str.characters.suffix(2))
I agree it is definitely confusing working with String indexing in Swift and they have changed a little bit from Swift 1 to 2 making googling a bit of a challenge but it can actually be quite simple once you get a hang of the methods. You basically need to make it into a two-step process:
1) Find the index you need
2) Advance from there
For example:
let sampleString = "HelloWorld"
let lastThreeindex = sampleString.endIndex.advancedBy(-3)
sampleString.substringFromIndex(lastThreeindex) //prints rld
let secondIndex = sampleString.startIndex.advancedBy(2)
sampleString.substringToIndex(secondIndex) //prints He
Bij splitting a string an array gives me following values back:
araay[1]=2
araay[2]=9
array[3]=2014
My question is how can i make from these 3 numbers a date 2-9-2014.
If i try:
var date = (array[1]-array[2]-array[3])
the return value is -2021 This is 2-9-2014=2021
var date = new Date (array[1]-array[2]-array[3])
the return value is 2
var date = new Date (array[1]-array[2]-array[3])
the reutrun value is 4-4-1908
So who can solve this?
Read basic string or char functionality for your chosen language.
Also this can be put in a class, predefined or not.
First you can try to use the - as chars instead of operators..
date = array[1] + "-" + array[2] + "-" + array[3]
but you should seriously just look at some documentation..
http://en.cppreference.com/w/