Parse a string with specific condition - ios

How can I trim my string which is in this form:
https://xxx.kflslfsk.com/kjjfkskfjksf/v1/files/media/93939393hhs8.jpeg
to this?
media/93939393hhs8.jpeg
I want to remove all the characters before the second to last slash /.
I can use stringByTrimmingCharactersInSet but I don't know how to specify the condition that I want:
let trimmedString = myString.stringByTrimmingCharactersInSet(
NSCharacterSet.whitespaceAndNewlineCharacterSet() // what here in my case ??
)
The above is for removing the white spaces, but that is not the case here.

Since the string is an URL get the path components, remove anything but the last 2 items and join the items with the slash separator.
if let url = NSURL(string:"https://xxx.kflslfsk.com/kjjfkskfjksf/v1/files/media/93939393hhs8.jpeg"), pathComponents = url.pathComponents {
let trimmedString = pathComponents.suffix(2).joinWithSeparator("/")
print(trimmedString)
}

You're not trimming, you're parsing.
There's no single call that will do what you want. I suggest writing a block of code that uses componentsSeparatedByString("\n") to break it into lines (one URL per line), then parse each line separately.
You could use componentsSeparatedByString("/") on each line to break it into the fragments between your slashes, and then assemble the last 2 fragments together.
(I'm deliberately not writing out the code for you. You should do that for yourself. I'm just pointing you in the right direction.)
You might also be able to use NSURLComponents to treat each line as a URL, but I'm not sure how you'd get the last part of URL before the filename (e.g. "media " or "lego") with that method.

Related

regular expression for removing empty lines produces wrong results

Can someone help me solve the problem I'm having with a regular expression? I have a file containing the following code:
I'm using a visit to find matches and replace them so that I can remove the empty lines. The result is, however, not what I'm expecting. The code is as follows:
str content = readFile(location);
// Remove empty lines
content = visit (content) {
case /^[ \t\f\v]*?$(?:\r?\n)*/sm => ""
}
This regular expression also removes non empty lines resulting in an output equal to:
Can someone explain what I'm doing wrong with the regular expression as well as the one shown below? I can't seem to figure out why it's not working.
str content = readFile(location);
// Remove empty lines
content = visit (content) {
case /^\s+^/m => ""
}
Kind regards,
Bob
I think the big issue here is that in the context of visit, the ^ anchor does not mean what you think it does. See this example:
rascal>visit ("aaa") { case /^a/ : println("yes!"); }
yes!
yes!
yes!
visit matches the regex at every postfix of the string, so the ^ is relative for every postfix.
first it starts at "aaa", then at "aa" and then at "a".
In your example visit, what will happen is that empty postfixes of lines will also match your regex, and substitute those by empty strings. I think an additional effect is that the carriage return is not eaten up eagerly.
To fix this, simply not use a visit but a for loop or while, with a := match as the condition.

Xcode is throwing me an error in swift

I'm following a tutorial(http://youtube.com/watch?v=xvvsG9Cl4HA 19 min 20sec) and to make his code look neat he puts some on a ew line like this
if let myPlacement = myPlacements?.first
{
let myAddress = "\(myPlacement.locality) \
(myPlacement.country) \
(myPlacement.postalCode)"
}
. But when I try I get an error
unterminated string literal
and
consecutive statements on a line must be seperated by a ';'
but the guy in the tutorial has done it the exact same way. What's going on?
I'm using the latest swift and and latest xcode 7.2 any help would be apreciated
if I write everything on the same line like this
if let myPlacement = myPlacements?.first
{
let myAddress = "\(myPlacement.locality) \(myPlacement.country) \(myPlacement.postalCode)"
}
it works fine though
if I write everything on the same line like this
Well, there's your answer. You are not permitted to break up a string literal into multiple lines as you are doing in your first example. There are languages that permit this, but Swift is not one of them. This is not legal:
let s = "hello
there"
There is no magic line-continuation character which, placed at the end of the first line, would make that legal.
If the window is narrower than the line, the editor may wrap the line, for display purposes; but you cannot put actual line breaks inside a string literal.
You can work around this by combining (concatenating) multiple string literals, if you think that makes for greater legibility. This, for example, is legal:
let myAddress = "\(myPlacement.locality) " +
"\(myPlacement.country) " +
"\(myPlacement.postalCode)"
I look your video tutorial carefully. You have a misunderstanding here.
You must pay attention to the video, the code in this picture is not break lines because he add a return here, it is because his screen is too narrow.
So, the real code is
let myAddress = "\(myPlacement.locality) \(myPlacement.country) \(myPlacement.postalCode)"
Please watch it carefully.
And you may need know first, \ in \(myPlacement.locality) is a escape character, it means to get the value of myPlacement.locality and put in the string.

Remove space from query params in url dynamically

I want to remove the space in query params in the request url in ruby
Here is my sample request url:-
URL = 'www.test.com/a?q1=john&q2=US&q3= 92832832&q4=test&q5= foo'
I want to my output as below:-
URL = 'www.test.com/a?q1=john&q2=US&q3=92832832&q4=test&q5=foo'
I suggest trimming the white space. This can be achieved as stated by Joel:
If you want to remove only leading and trailing whitespace (like PHP's trim) you can use .strip, but if you want to remove all whitespace, you can use .gsub(/\s+/, "") instead.
(Ruby function to remove all white spaces?)
To remove whitespace you can use the following on the string
URL = 'www.test.com/a?q1=john&q2=US&q3= 92832832&q4=test&q5= foo'.gsub(/\s+/, "")
url = 'www.test.com/a?q1=john&q2=US&q3= 92832832&q4=test&q5= foo'.gsub!(/\s+/, "")

Generated URL by Html.RouteLink with special characters

I have the following line
#Html.RouteLink(type.Description, "ListingsWithTypeSpecified", new { country = Model.CountryCode, state = Model.State, city = Model.CurrentCity.Name.ToLower(), description = type.Description.ToLower(), id = type.TypeID })
which produces
http://some.com:9609/ca/on/london/physiotherapy%20%20%26%20acupuncture/2
and
http://some.com:9609/ca/on/london/health%20department/19
first one has spaces and a &, second one just has a space
for the first one, I would like to still show
http://localhost:9609/ca/on/london/physiotherapy and acupuncture/2
for this one I understand replacing the & with "and" will work, however I still do not want %20 as spaces instead I would like to have a clean url.
Which method should I be using to properly have friendly url shown by what Html.RouteLink creates?
Replace the space with a -
e.g.
type.Description.ToLower().Replace(" ", "-")

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.

Resources