How to use a string variable in a directory path - vb.net

How can I join two strings in a directory path?
E.g.
Image.Image = Image.FromFile("MY STRING\image.png")
The MYSTRING variable contains a string.

You should use Path.Combine() when dealing with string paths, You can do this as follows:
Image.Image = Image.FromFile(Path.Combine(MYSTRING, "image.png"))

There are a number of ways to concatenate Strings:
As you have a white space in MY STRING, I'm guessing it's a literal String.
Try one of these:
If MY STRING is a literal String:
Plus Operator
Image.Image = Image.FromFile("MY STRING" + "\image.png")
String.Concat
Image.Image = Image.FromFile(String.Concat("MY STRING", "\image.png"))
If MYSTRING is a variable:
Plus Operator
Image.Image = Image.FromFile(MYSTRING + "\image.png")
String.Concat
Image.Image = Image.FromFile(String.Concat(MYSTRING, "\image.png"))
Please see:
https://www.dotnetperls.com/string-concat-vbnet

Related

How to replace string characters that are not in a reference list in kotlin

I have a reference string on which the allowed characters are listed. Then I also have input strings, from which not allowed characters should be replaced with a fixed character, in this example "0".
I can use filter but it removes the characters altogether, does not offer a replacement. Please note that it is not about being alphanumerical, there are ALLOWED non-alphanumerical characters and there are not allowed alphanumerical characters, referenceStr happens to be arbitrary.
var referenceStr = "abcdefg"
var inputStr = "abcqwyzt"
inputStr = inputStr.filter{it in referenceStr}
This yields:
"abc"
But I need:
"abc00000"
I also considered replace but it looks more like when you have a complete reference list of characters that are NOT allowed. My case is the other way around.
Given:
val referenceStr = "abcd][efg"
val replacementChar = '0'
val inputStr = "abcqwyzt[]"
You can do this with a regex [^<referenceStr>], where <referenceStr> should be replaced with referenceStr:
val result = inputStr.replace("[^${Regex.escape(referenceStr)}]".toRegex(), replacementChar.toString())
println(result)
Note that Regex.escape is used to make sure that the characters in referenceStr are all interpreted literally.
Alternatively, use map:
val result = inputStr.map {
if (it !in referenceStr) replacementChar else it
}.joinToString(separator = "")
In the lambda decide whether the current char "it" should be transformed to replacementChar, or itself. map creates a List<Char>, so you need to use joinToString to make the result a String again.

Adding back slash for quotes in Swift

I have the following code in Objective-C:
NSString *someString = #"Hello World";
I need a Swift Code with the following output: \"Hello World\"
Including the double checkmarks and the backslash.
According to your comment, you'll need this:
var mySuperFancyDynamicString: String = "whaaaazzuppp"
let someString: String = "\\\"\(mySuperFancyDynamicString)\\\""
print(someString)
print is \"whaaaazzuppp\" (well... was Hello World, before the edit)
Or to your "text box" Comment:
var mySuperFancyDynamicString: String = textBox.text
let someString: String = "\\\"\(mySuperFancyDynamicString)\\\""
print(someString)
Print will be : \"textBoxContent\"
Apparently you want to convert from Objective-c to swift, if yes then don't add backslash to your double quotes, String in swift are just double quoted text.
You should end with
someString = "Hello World"
If You don't want to convert to swift then what are you doing ? Cause the code you are providing (NSString *someString = #\"Hello World\";) will throw an error.
EDIT :
If you really need that specific output, you must create a string like that :
someString = "\\\"Hello World\\\""

creating text from NSWindowsCP1252StringEncoding text

I have text below,
{\rtf1\ansi\ansicpg1252\cocoartf1138\cocoasubrtf510
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\
\f0\fs24 \cf0 \'83}
In the above text \'83 corresponds to RTF file format and is in NSWindowsCP1252StringEncoding. Now my problem is how to convert to actual representation in
83 corresponds to string ƒ in NSWindowsCP1252StringEncoding.
NSString string with the encoding say using the api
stringWithCString:encoding. I have called the api like below.
NSString* str = [NSString stringWithCString:#"\'83" encoding:NSWindowsCP1252StringEncoding];
but it does not give the text. please let me know how to convert the value to particular text.
Regards,
Lenin
#"\'83" is not a c string, so you shouldn't be passing it to stringWithCString:encoding:. Even a C string version, "\'83", is just ascii characters so using the encoding NSWdindowsCP1252StringEncoding will not produce a string with any special characters.
Instead you need a c string with non-ascii values in order for NSWindowsCP1252StringEncoding to result in a non-ascii character like 'ƒ'.
NSString *str = [NSString stringWithCString:"\x83" encoding:NSWindowsCP1252StringEncoding];

Convert quoted strings in a CSV to an NSMutableArray without the quotes

I have a question about the objective C. I have the following NSString *name shown below:
"First Name","Second Name","Last Name";
Actually, name is the header of the CSV and receive from the URL. And I use the follow statement to break the statement to array.
NSMutableArray *csvTitleArray;
csvTitleArray = [[name componentsSeparatedByString:#","];
The result of the array is
[0] = "First Name" // the " is part of the string, it means the first char of [0] is ", not F
[1] = "Second Name"
[2] = "Last Name"
However, I want to cancel the " in the begin and end of the string (the " is part of the string. Can anyone help me? Thank you.
Have a look at parsing csv data, the General CSV section (code example) handles your case.
See writing parser using nsscanner - csv for usefull more generic pointers about parsing data.
Have you thought of using stringByReplacingOccurrencesOfString:withString: and then splitting the string out into an array?

How to filter out some vulnerability causing characters in query string?

I need to filter out characters like /?-^%{}[];$=*`#|&#'\"<>()+,\. I need replace this with empty string if it is there in the query string. Please help me out. I am using this in ASP pages.
Best idea would be to use a function something along the lines of:
Public Function MakeSQLSafe(ByVal sql As String) As String
'first i'd avoid putting quote chars in as they might be valid? just double them up.
Dim strIllegalChars As String = "/?-^%{}[];$=*`#|&#\<>()+,\"
'replace single quotes with double so they don't cause escape character
If sql.Contains("'") Then
sql = sql.Replace("'", "''")
End If
'need to double up double quotes from what I remember to get them through
If sql.Contains("""") Then
sql = sql.Replace("""", """""")
End If
'remove illegal chars
For Each c As Char In strIllegalChars
If sql.Contains(c.ToString) Then
sql = sql.Replace(c.ToString, "")
End If
Next
Return sql
End Function
This hasn't been tested and it could probably be made more efficient, but it should get you going. Wherever you execute your sql in your app, just wrap the sql in this function to clean the string before execution:
ExecuteSQL(MakeSQLSafe(strSQL))
Hope that helps
As with any string sanitisation, you're much better off working with a whitelist that dictates which characters are allowed, rather than a blacklist of characters that aren't.
This question about filtering HTML tags resulted in an accepted answer suggesting the use of a regular expression to match against a whitelist: How do I filter all HTML tags except a certain whitelist? - I suggest you do something very similar.
I'm using URL Routing and I found this works well, pass each part of your URL to this function. It's more than you need as it converts characters like "&" to "and", but you can modify it to suit:
public static string CleanUrl(this string urlpart) {
// convert accented characters to regular ones
string cleaned = urlpart.Trim().anglicized();
// do some pretty conversions
cleaned = Regex.Replace(cleaned, " ", "-");
cleaned = Regex.Replace(cleaned, "#", "no.");
cleaned = Regex.Replace(cleaned, "&", "and");
cleaned = Regex.Replace(cleaned, "%", "percent");
cleaned = Regex.Replace(cleaned, "#", "at");
// strip all illegal characters like punctuation
cleaned = Regex.Replace(cleaned, "[^A-Za-z0-9- ]", "");
// convert spaces to dashes
cleaned = Regex.Replace(cleaned, " +", "-");
// If we're left with nothing after everything is stripped and cleaned
if (cleaned.Length == 0)
cleaned = "no-description";
// return lowercased string
return cleaned.ToLower();
}
// Convert accented characters to standardized ones
private static string anglicized(this string urlpart) {
string beforeConversion = "àÀâÂäÄáÁéÉèÈêÊëËìÌîÎïÏòÒôÔöÖùÙûÛüÜçÇ’ñ";
string afterConversion = "aAaAaAaAeEeEeEeEiIiIiIoOoOoOuUuUuUcC'n";
string cleaned = urlpart;
for (int i = 0; i < beforeConversion.Length; i++) {
cleaned = Regex.Replace(urlpart, afterConversion[i].ToString(), afterConversion[i].ToString());
}
return cleaned;
// Spanish : ÁÉÍÑÓÚÜ¡¿áéíñóúü"
}