Strings pre-filled with spaces in Dart - dart

A fairly basic question :
I would like to create a string initialized to a dynamically decided number of spaces in dart.
Here's something that worked :
String spaces(n) {
var result = new List<int>.filled(n+1,32);
return new String.fromCharCodes(result);
}
Is there a better way?

Well you can always fill the list with spaces and join them:
String spaces(n) => new List.filled(n + 1, ' ').join();

This seems quite concise and easy to interpret:
''.padRight(32, ' ')
Try it in DartPad

Related

modifing the function to capitalize every word of the given string and return the results

I think I get how to capitalize the string ('how do you do')by possibly using the caps method or title method. but how would I return it ? im confused by that.
would I do return('How Do You Do')[0].lower('How Do You Do') ?
im new to learning the stuff I watched didn't really help explain it to me
I suppose you are using Python. You just have to use .lower() to the string.
print('How Do You Do'.lower())
Result:
how do you do
Alternately, you can make them all caps using .upper()
text = "How Do You Do"
text.upper()
Result:
HOW DO YOU DO
And capitalize only the first letter of each word using .title()
text.title()
Result
How Do You Do
EDIT
def convert_to_title(text):
return text.title()
print(convert_to_title('how do you do?'))
Result:
How Do You Do?

Line breaks are being lost when sending sms from mvc3

For some reasons the line breaks when send SMS from MVC, not working.
I am using code like,
Constants.cs
public struct SmsBody
{
public const string SMSPostResume=
"[ORG_NAME]"+
"[CONTACT_NUMBER]"+
"[ORG_NAME]"+
"[CONTACT_PERSON]"+
"[EMAIL]"+
"[MOBILE_NUMBER]";
}
Then I call these variables at controller like,
SmsHelper.Sendsms(
Constants.SmsSender.UserId,
Constants.SmsSender.Password,
Constants.SmsBody.SMSPostResume
.Replace("[NAME],",candidate.Name)
.Replace("[EMAIL],",candidate.Email) etc......
My Issue is when i get sms these all things are same line. no spacing.
MY OUTPUT
Dearxxxxyyy#gmail.com0000000000[QUALIFICATION][FUNCTION][DESIGNATION][PRESENT_SALARY][LOCATION][DOB][TOTAL_EXPERIENCE][GENDER] like that.
How to give space between these? Anyone know help me...
Putting the string parts on separate lines, and concatenating them is not a line break... The parts will end up exactly after one another. You should try putting a \n (line break escaped sequence) at each place you want a line break:
public const string SMSPostResume=
"[ORG_NAME]\n"+
"[CONTACT_NUMBER]\n"+
"[ORG_NAME]\n"+
"[CONTACT_PERSON]\n"+
"[EMAIL]\n"+
"[MOBILE_NUMBER]\n";
Also a note based on #finman's comment:
Depending on the service it might be \r\n instead of \n though
So you should look up int he API docs which one would work.
Also there is another error: you try to match string constants with a , at their ends, and the original ones don't have that...
SmsHelper.Sendsms(
Constants.SmsSender.UserId,
Constants.SmsSender.Password,
Constants.SmsBody.SMSPostResume
.Replace("[NAME],",candidate.Name) // <- this line!
.Replace("[EMAIL],",candidate.Email) // <- this line!
You should rewrite either the format string to include, or the replaces to exclude the ,:
SmsHelper.Sendsms(
Constants.SmsSender.UserId,
Constants.SmsSender.Password,
Constants.SmsBody.SMSPostResume
.Replace("[NAME]",candidate.Name) // <- no "," this time
.Replace("[EMAIL]",candidate.Email) // <- no "," this time
//...etc
public const string SMSPostResume=
"[ORG_NAME]"+
"\r[CONTACT_NUMBER]"+
"\r[ORG_NAME]"+
"\r[CONTACT_PERSON]"+
"\r[EMAIL]"+
"\r[MOBILE_NUMBER]";
Also, in
Replace("[NAME],",candidate.Name)
are you sure you want the comma after [NAME] ? If it's not in the string, don't try to replace it.

jQuery Mobile Filtered List - only match beginning of string

Im using the jQuery mobile search filter list:
http://jquerymobile.com/test/docs/lists/lists-performance.html
Im having somer performance issues, my list is a little slow to filter on some phones. To try and aid performance I want to change the search so only items starting with the search text are returned.
So 'aris' currently finds the result 'paris' but I want this changed. I can see its possible from the documentation below but I dont know how to implement the code.
http://jquerymobile.com/test/docs/lists/docs-lists.html
$("document").ready( function (){
$(".ui-listview").listview('option', 'filterCallback', yourFilterFunction)
});
This seems to demonstrate how you write and call your own function, but ive no idea how to write it! Thanks
http://blog.safaribooksonline.com/2012/02/14/jquery-mobile-tip-write-your-own-list-view-filter-function/
UPDATE - Ive tried the following in a seperate js file:
$("document").ready( function (){
function beginsWith( text, pattern) {
text= text.toLowerCase();
pattern = pattern.toLowerCase();
return pattern == text.substr( 0, pattern.length );
}
$(".ui-listview").listview('option', 'filterCallback', beginsWith)
});
might look something like this:
function beginsWith( text, pattern) {
text= text.toLowerCase();
pattern = pattern.toLowerCase();
return pattern == text.substr( 0, pattern.length );
}
Basically you compare from 0 to "length" of what you're matching to the source. So if you pass in "test","tester" it will see you're passing in a string of length 4 and then substr "tester" from 0,4, which gives you "test". Then "test" is equal to "test"... so return true. Lowercase them to make it case insensitive.
Another trick to improve filter performance, only filter once they've entered more than 1 character.
edit it appears jQueryMobile's filter function expects that "true" means it was not found... so it needs to be backwards. return pattern != text.substr( 0, pattern.length );
This worked for me. I am using regular expression here so sort of different way to achieve the same thing.
But the reason why my code didn't work initially was that the list item had a lot of spaces at the beginning and at the end (found that it got added on it's own while debugging).
So I do a trim on the text before doing the match. I have a feeling Jonathan Rowny's implementation will also work if we do text.trim() before matching.
$(".ui-listview").listview('option', 'filterCallback', function (text, searchValue) {
var matcher = new RegExp("^" + searchValue, "i");
return !matcher.test(text.trim());
});

Ruby gsub function

I'm trying to create a BBcode [code] tag for my rails forum, and I have a problem with the expression:
param_string.gsub!( /\[code\](.*?)\[\/code\]/im, '<pre>\1</pre>' )
How do I get what the regex match returns (the text inbetween the [code][/code] tags), and escape all the html and some other characters in it?
I've tried this:
param_string.gsub!( /\[code\](.*?)\[\/code\]/im, '<pre>' + my_escape_function('\1') + '</pre>' )
but it didn't work. It just passes "\1" as a string to the function.
You should take care of the greedy behavior of the regular expressions. So the correct code looks like this:
html.gsub!(/\[(\S*?)\](.*?)\[\/\1\]/) { |m| escape_method($1, $2) }
The escape_method then looks like this:
def escape_method( type, string )
case type.downcase
when 'code'
"<pre>#{string}</pre>"
when 'bold'
"<b>#{string}</b>"
else
string
end
end
Someone here posted an answer, but they've deleted it.
I've tried their suggestion, and made it work with a small change. Whoever you are, thanks! :)
Here it is
param_string.gsub!( /\[code\](.*?)\[\/code\]/im ) {|s| '<pre>' + my_escape_function(s) + '</pre>' }
You can simply use "<pre>#{$1}</pre>" for your replacement value.

How to parse a remote website and create a link on every single word for a dictionary tooltip?

I want to parse a random website, modify the content so that every word is a link (for a dictionary tooltip) and then display the website in an iframe.
I'm not looking for a complete solution, but for a hint or a possible strategy. The linking is my problem, parsing the website and displaying it in an iframe is quite simple. So basically I have a String with all the html content. I'm not even sure if it's better to do it serverside or after the page is loaded with JS.
I'm working with Ruby on Rails, jQuery, jRails.
Note: The content of the href tag depends on the word.
Clarification:
I tried a regexp and it already kind of works:
#site.gsub!(/[A-Za-z]+(?:['-][A-Za-z]+)?|\\d+(?:[,.]\\d+)?/) {|word| '' + word + ''}
But the problem is to only replace words in the text and leave the HTML as it is. So I guess it is a regex problem...
Thanks for any ideas.
I don't think a regexp is going to work for this - or, at least, it will always be brittle. A better way is to parse the page using Hpricot or Nokogiri, then go through it and modify the nodes that are plain text.
It sounds like you have it mostly planned out already.
Split the content into words and then for each word, create a link, such as whatever
EDIT (based on your comment):
Ahh ... I recommend you search around for screen scraping techniques. Most of them should start with removing anything between < and > characters, and replacing <br> and <p> with newlines.
I would use Nokogiri to remove the HTML structure before you use the regex.
no_html = Nokogiri::HTML(html_as_string).text
Simple. Hash the HTML, run your regex, then unhash the HTML.
<?php
class ht
{
static $hashes = array();
# hashes everything that matches $pattern and saves matches for later unhashing
function hash($text, $pattern) {
return preg_replace_callback($pattern, array(self,'push'), $text);
}
# hashes all html tags and saves them
function hash_html($html) {
return self::hash($html, '`<[^>]+>`');
}
# hashes and saves $value, returns key
function push($value) {
if(is_array($value)) $value = $value[0];
static $i = 0;
$key = "\x05".++$i."\x06";
self::$hashes[$key] = $value;
return $key;
}
# unhashes all saved values found in $text
function unhash($text) {
return str_replace(array_keys(self::$hashes), self::$hashes, $text);
}
function get($key) {
return self::$hashes[$key];
}
function clear() {
self::$hashes = array();
}
}
?>
Example usage:
ht::hash_html($your_html);
// your word->href converter here
ht::unhash($your_formatted_html);
Oh... right, I wrote this in PHP. Guess you'll have to convert it to ruby or js, but the idea is the same.

Resources