MFC: Render string in multiple formats - visual-c++

Using a CDC & CDC::DrawText(Ex), I want to render a string with a sub-string in bold
e.g:
void renderText(CDC *pDC,CString &str,int boldStart,int boldEnd)
{
...
}
example: renderText(pDC,"Test
String",0,3) -> Test String
example: renderText(pDC,"Test
String",5,-1) -> Test String
I assume I'll make 3 CDC::DrawText calls, but how do I know the positions to draw each string?

Use CDC::GetTextExtent to get the number of pixels each piece of the string will take up, and adjust the points you pass into CDC::DrawText accordingly.

Related

How to modify the value of a Hashmap, which has key&value as Strings,and the 'value' is a single string consisting of comma separated values

I am trying to modify a Hashmap .
The 'value' is a single string consisting of comma separated values.
(for e.g.: "aid=abc,bid=def,cid=gh2")
I need to replace particular values from them with a corresponding value from the DB.
(for e.g. changing the bid to 123, which would result in the above 'value' string as: "aid=abc,bid=123,cid=gh2")
And then set the modified 'value' to the corresponding key, so that the hashmap consists of modified values.
I tried to iterate through the keys , and with map(key) which will give the value, I tried to convert that to a list of comma separated values and then iterate through that list, and in each of the string, if I find 'bid'(example string above), then I do the necessary manipulation and then set it back to the Hashmap(which I Wasnt able to do since Strings arent mutable)
for (String name :outputMap.keySet())
List urlList = Arrays.asList(outputMap.get(name).split(",")); for(int i=0;i
expected result:"aid=abc,bid=123,cid=gh2" (post the manipulation)
actual result: unable to do.
I have used Stringbuffer for issues where string had to be modified, but was a little apprehensive to use that here, since this has already multiple conversions going.
The code needs to Java 7 compliant, since this is being deployed in a Client machine still using some legacy environment.(They are scheduled to migrate to java 8 , but thats scheduled for much later)
Any assistance here would be appreciated.
static Map<String, String> replace(Map<String, String> map, String toReplace, String replacement) {
return map.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey,
e -> transformValue(e.getValue(), toReplace, replacement)));
}
static String transformValue(String value, String toReplace, String replacement) {
return Arrays.stream(value.split(","))
.map(pair -> pair.split("="))
.map(sPair -> sPair[0] + "=" + (toReplace.equals(sPair[0]) ? replacement : sPair[1]))
.collect(Collectors.joining(","));
}
this will iterate through all entries of map and create a new map with changed values. Example:
var map = Map.of("key1", "aid=abc,bid=def,cid=gh2");
System.out.println(replace(map, "bid", "123").get("key1"));
will print aid=abc,bid=123,cid=gh2

How to check if a String contains two same characters?

In my case a string contains for example something like 2500.00. Also you input a string in the same format for example 250.0 which would be converted to 250.00. These strings will be converted to float and they will be added or subtracted.
Now I want to check if the string contains two "." somewhere for example 2.50.00 or 250..00. In that case an errormessage should be displayed.
Therefore my question is how am I able to check if a string contains two "." characters at any position of the string?
You may check if a dot appears more than once in a string with a simple method checking if the first index of the char is not equal to the index of the last char occurrence:
boolean containsTwoDots(String str) {
return str.indexOf('.') != str.lastIndexOf('.');
}

I'm having trouble with String.format("%.2f", x)

Where would I insert this in my code? I have a inputs and a message box that I need to get to two decimals but I can't figure out where I should put it.
If you don’t want to print out the String and just want to format it for later use, you can use the static format method of the String class.
It works in exactly the same way as printf as far as formatting is concerned, but it doesn’t print the String, it returns a new formatted String.
String.format "%[argument number] [flags] [width] [.precision] type"
"%" is a special character in formatted String and it denotes start of formatting instruction. String.format() can support multiple formatting instruction with multiple occurrence of "%" character in formatting instruction.
"argument number" is used to specify correct argument in case multiple arguments are available for formatting.
"flags" is another special formatting instruction which is used to print String in some specific format for example you can use flag as "," to print comma on output.
"width" formatting option denotes minimum number or character will be used in output but in case if number is larger than width then full number will be displayed but if its smaller in length then it will be be padded with zero.
"precision" is using for print floating point formatted String, by using precision you can specify till how many decimal a floating point number will be displayed in formatted String.
"type" is the only mandatory formatting option and must always comes last in format String also input String which needs to be formatted must be with same type specified in "type" parameter.
public class StringFromatExample {
public static void main(String[] args) {
String s1 = String.format("%-4.5f %.20f", 35.23429837482,5.2345678901);
System.out.println(s1);
}
}
the output will be
35.23430 5.23456789010000000000

Xcode 6.1 & Swift - textField input string to integer for basic math

I am slowly understanding things in swift, I am coming for a javascript background so it is somewhat familiar.
However variables are urking me.
in JS a variable can be
var varName = 1; //Number
var varName2 = "petey" //String
var conCat = varname + varName2; // 1petey
however in swift String vars and In var are troubling me. All I want to do is capture decimal number from user input from multiple "textField" sources and store that data to variable (which i already have setup) I need to concatinate a few of them with + then do basic math with the data in the variables with * and /.
How do I make the textFeld only capture?int or how do I convert text field strings to numbers, e.g. int?
A UITextField contains a String, not an Int, so you have to convert it, e.g.
let number : Int? = textField.text.toInt()
So, there actually is a method to convert from String to Int built-in in Swift. Beware, that it returns an optional, because the conversion may fail. So you have to check for nil before you use the variable.
For your other question, take a look at e.g. UITextField - Allow only numbers and punctuation input/keypad. Basically, you will have to adapt UITextField and filter it, once there are new entries. On iOS it might be easier, because you can show a numbers-only keyboard.
The data send from the textField is String. There are multiple ways to convert an Int to a String.
var a:String="\(2+3)" //"5"
And to concatenate a String to Int :
var b:String="Hello "+"\(3*4)" //"Hello 12"
And to Convert a String to an Int from a textField:
var b:Int=textField.text.toInt()
Use the function in swift inputmethod.intValue(); if any text is entered then it returns with a 0.

Best way to compare multiple string in java

Suppose I have a string "That question is on the minds of every one.".
I want to compare each word in string with a set of word I.e. (to , is ,on , of) and if those word occurs I want to append some string on the existing string.
Eg.
to = append "Hi";
Is = append "Hello";
And so on.
To be more specific I have used StringTokenizer to get the each word and compared thru if else statement. However we can use Switch also but it is available in Jdk 1.
7.
I don't know if this is what you mean, but:
You could use String.split() to separate the words from your string like
String[] words = myString.split(" ");
and then, for each word, compare it with the given set
for(String s : words)
{
switch(s)
{
case("to"):
[...]
}
}
Or you could just use the String.contains() method without even splitting your string, but I don't know if that's what you wanted.
Use a HashMap<String,String> variable to store your set of words and the replacement words you want. Then split your string with split(), loop through the resulting String[] and for each String in the String[], check whether the HashMap containsKey() that String. Build your output/resulting String in the loop - if the word is contained in the HashMap, replace it with the value of the corresponding key in the HashMap, otherwise use the String you are currently on from the String[].

Resources