Inserting points (pixels) between elements of NSString in a UILabel - nsstring

I am designing a UILabel. Its Text is an NSString that is made up of 3 different variables such as:
NSString *myString = [NSString stringWithFormat:#"%# %# %#", string1, string2, string 3];
What is the best way to separate string1, string2 and string 3 in term of points and not character spaces?

Related

Counting instances of a character in a string

I need to be able to count the instances of a period in a string. (I need to capture the decimal point in a number, but discard the other periods in the name or title.) I know NUM-ENTRIES essentially counts the number of entries around that character, but I want to do the opposite. My broader problem is to parse a decimal number out of a string, where occasionally the string has other periods in the string.
What syntax can I use to determine the number of periods in this string? See my pretend "NUM-PERIODDS" pretend Progess function below.
Erin L. Halpin (33.333%)
Mr. Thomas Q. Smith 66.6%
I have an algorith to take everything in front of the "%" sign, then I process through all the numbers, but if I find a second "." then I need to skip that character. (If there are better ways to do my algorithm, would love suggestions on that, too.)
//takes in the full joint name and sees if there is a percentage value in it
//then finds whatever is in front of the % sign
IF INDEX (full_name_percentage, "%") GT 0 THEN DO:
cBeforePercentageStr=
SUBSTRING(full_name_percentage,1,INDEX(full_name_percentage,"%") - 1).
IF LENGTH (cBeforePercentageStr) GT 0 THEN DO:
//MESSAGE full_name_percentage VIEW-AS ALERT-BOX.
cThisChar = "".
DO iTemp = 1 TO LENGTH(cBeforePercentageStr):
cThisChar = SUBSTRING(cBeforePercentageStr,iTemp,1).
IF fnIsNumericOrPeriod(cThisChar) THEN
cPercentage = cPercentage + cThisChar.
END.
//need to account for if there are two decimal points
IF **NUM-PERIODS** (cPercentage, ".") GT 1 THEN DO:
MESSAGE "cPercentage value " + cPercentage VIEW-AS ALERT-BOX.
cPercentage = SUBSTRING(cPercentage,2,LENGTH(cPercentage)).
END.
dPercent = TRUNCAT (DECIMAL(cPercentage),2).
END.
END.
There is no need for loops. The number of characters in a string is equal to the length of the string minus the length of that same string without those characters.
define variable p as integer no-undo.
define variable myString as character no-undo.
myString = 'Erin L. Halpin (33.333%) Mr. Thomas Q. Smith 66.6%'.
p = length( myString )
- length( replace( myString, '.', '' ) )
.
message 'there are' p 'periods in the string'.
Golfing on you can also just return the number of entries with "." as delimiter minus one but with a floor of 0.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE cStr AS CHARACTER NO-UNDO.
cStr = 'Erin L. Halpin (33.333%) Mr. Thomas Q. Smith 66.6%'.
i = MAX(NUM-ENTRIES(cStr, ".") - 1,0) .
MESSAGE SUBSTITUTE("There are &1 periods in the string", i) VIEW-AS ALERT-BOX.
I like Stefan's idea (and it's pretty foolproof) but for a bit of code golf fun, you can loop without looping all the characters ...
DEFINE VARIABLE str AS CHARACTER NO-UNDO.
DEFINE VARIABLE findChr AS CHARACTER NO-UNDO.
DEFINE VARIABLE cnt AS INTEGER NO-UNDO.
DEFINE VARIABLE pos AS INTEGER NO-UNDO.
DEFINE VARIABLE startPos AS INTEGER NO-UNDO.
str = "Erin L. Halpin (33.333%) Mr. Thomas Q. Smith 66.6%".
findChr = ".".
startPos = 1.
pos = INDEX(str, findChr, startPos).
DO while pos > 0:
cnt = cnt + 1.
startpos = pos + 1.
pos = INDEX(str, findChr, startPos).
eND.
MESSAGE
cnt
VIEW-AS ALERT-BOX.
This is kind of crude and ugly but it should work just fine:
define variable i as integer no-undo.
define variable n as integer no-undo.
define variable p as integer no-undo.
define variable myString as character no-undo.
myString = "Erin L. Halpin (33.333%) Mr. Thomas Q. Smith 66.6%".
n = length( myString ).
do i = 1 to n:
if substring( myString, i, 1 ) = "." then p = p + 1.
end.
message "there are" p "periods in the string".

Converting a list of integers to a map of vertices containing the elements coordinates

This is what i have at the moment
(string -> int list)
let read filename = ....
this is working as intended, returning a list of integers from a textfile looking like this:
530070000
600195000
098000060
800600003
400803001
700020006
060000280
000419005
000080079
Yes you are correct, it is a sudoku board. This is what i have to work with:
type vertex = int * int (*Cells in the sudoku board*)
type gamma = int (*representing colors 1-9*)
(* [Vertex = Map.Make(Vertex)] *)
module Vertex = Map.Make(struct
type t = vertex
let compare = Stdlib.compare
end)
(* [Gamma = Set.Make(Gamma)] *)
module Gamma = Set.Make(struct
type t = gamma
let compare = Stdlib.compare
end)
The gamma set is for solving the sudoku board using graph coloring. I need help understanding how i can convert the list of integers to a suitable map for this kind of task. According to the structure i provided, so i can access each element in the map using it coordinates (x, y). Hope you understand, otherwise i will try to provide more info. I'm reaaally bad at OCaml but trying to learn. I'm sorry for body errors etc, first time posting here.
As far as I can understand your task, the text file contains a grid of digits with the initial disposition for sudoku. So you shouldn't interpret a line in the file as a single integer but rather as a list of integers. You can either change your read function so that it returns int list list instead of int list and then use List.fold_left over the list that will also count the position of an element in the list, but it is tedious. It is much easier to read the grid directly from the file, e.g.,
let read_matrix chan =
let rec loop i j grid =
match input_char chan with
| exception End_of_file -> grid
| '\n' -> loop (i+1) 0 grid
| '0'..'9' as c ->
loop i (j+1) ##
Vertex.add (i,j) (ascii_digit c) grid
| _ -> invalid_arg "invalid input" in
loop 0 0 Vertex.empty
where ascii_digit is defined as,
let ascii_digit c = Char.code c - Char.code '0'
The read_matrix function takes the channel as input so to read the grid from a file you can define,
let matrix_from_file file =
let chan = open_in file in
let r = read_matrix chan in
close_in chan;
r
Hint: you probably also don't want to include positions with 0 in your grid. It is easy to achieve, just add another case to the pattern in the loop function that will skip it, e.g.,
...
| '0' -> loop i (j+1) grid
...

How to show only the whole numbers in Y number aexis in stackedbarchart/barchart

Y axis tick label should only show non decimal values / whole numbers as as series . if i set set TickUnit to 1 it should be 1,2,3,4,5,.. if i set Unit Ticks to 2 ..2,4,6,8,.. if i set to 5 5,10,15,20,25.
i set the Unit Ticks to 1 still it sometimes adding the decimal values also and showing 2.5 ,5.0,7.5,10.0,12.5......how to prevent this and show only whole numbers(Non decimal Numbers).?
option 1. store the number as an int, int num = (int)Math.floor(myDouble);
option 2. in your method make the parameter a double and inside the method cast it to an int
this will allow you to use the method with both a double and a int. Please keep in mind that this is C# code but java should be very similar.
private List<int> numberSeries(double aDouble)
{
List<int> number = new List<int>();
int base = (int)Math.floor(aDouble);
for(int x = 1;x++ < 10) //change 10 to whatever you want
{
number.Add(aDouble * x);
}
return number;
}

QT Spim Mips programming - inputting a character into a string

How would I add on a letter into a string. For instance, I have:
str: .asciiz "abcdefghijklmnopqrstuvwxyz"
I want to add the letter a to the end of the string to make it display "abcdefghijklmnopqrstuvwxyza" when I print out the string.
I've tried
la $t0, str
lb $t1, 0($t0)
add $t0, $t0, 25
sb $t2, 1($t0) # assume that $t2 contains the character a
Any help is greatly appreciated!
You can't just grow a string without having space for it to grow into. In this case you'd be overwriting the NULL terminator with an 'a', which can cause all sorts of problems later on.
You could allocate some extra space following the string:
str: .asciiz "abcdefghijklmnopqrstuvwxyz"
extra_space: .space 32
And then adding an 'a' at the end could be done like so:
la $t0,str+26
li $t1,'a'
sb $t1,($t0) # append an 'a' to the string
sb $zero,1($t0) # add a NULL terminator after the 'a'

What does these mean "{0,2:X2}" in statement String.Format("{0,2:X2}", b);

Code:
SHA1 sha = new SHA1CryptoServiceProvider();
string hashedValue = string.Empty;
//hash the data
byte[] hashedData = sha.ComputeHash(Encoding.Unicode.GetBytes(str));
//loop through each byte in the byte array
foreach (byte b in hashedData)
{
//convert each byte and append
hashedValue += String.Format("{0,2:X2}", b);
}
I searched for arguments passed to String.Format() but didnt able to understand it exactly.
Thanks in advance!
Formatting the string in hexadecimal format...
X = Hexadecimal format
2 = 2 characters
It's basically just formatting the string in uppercase hexadecimal format - see the docs.
The hexadecimal ("X") format specifier converts a number to a string of hexadecimal digits. The case of the format specifier indicates whether to use uppercase or lowercase characters for hexadecimal digits that are greater than 9.
This particular format is known as Composite Formatting, so to break it down:
{0 = parameterIndex, 2 = alignment :X2 = formatString}

Resources