I have a NSString. This NSString is split in different parts with ";".
I split this NSString in 2 substrings (with [componentsSeparatedByString:#";"])
Now, I have a substring, with [componentsSeparatedByString:#";"], in a NSArray.
In this substring I have (sometimes but not always !) a ",".
When I have a "," I want to spilt my substring in two "sub-substrings" and use this two sub-subtrings...
How can I do that ?
Thanks for your answers.
EDIT :
Hi #Alladinian, thanks for ur answer.
That's a loop I need, I think. I want to add new contact to iPhone address book (First name and Last name) with QRCode.
My NSString looks like :
NSString *_name = [NSString stringWithFormat:#"%#", code.contact];
My substring looks like:
NSArray *subStrings = [code.contact componentsSeparatedByString:#";"];
In my NSString, I have (perhaps but not always) a "," I need two different outputs : one for first name and one for last name.
I know how to add first name and last name separated by "," but I don't know what to do if I have only a first name. Have only a first name crash my app...
For now, to skirt problem, I send Fist name and Last name in Fist name field... But it's not perfect for my sake.
Ok, here's some code you can use. You can't just use componentsSeparatedByString for the name because there are 4 cases:
no comma: assume just first name "first"
comma, but no last name ", first"
comma, but no first name "last, "
comma, both: "last, first"
code:
NSString * mecardString = ...your string...
if ( [ mecardString hasPrefix:#"MECARD:" ] ) // is it really a card string? (starts with 'MECARD:')
{
mecardString = [ mecardString substringFromIndex:[ #"MECARD:" length ] ] ; // remove MECARD: from start
NSString * firstName = nil ;
NSString * lastName = nil ;
NSArray * components = [ mecardString componentsSeparatedByString:#";" ] ;
for( NSString * component in components ) // process all parts of MECARD string
{
NSString * lcString = [ component lowercaseString ] ;
if ( [ lcString hasPrefix:#"n:" ] )
{
// handle name ("N:")
NSRange commaRange = [ lcString rangeOfString:#"," ] ;
if ( commaRange.location == NSNotFound )
{
firstName = lcString ;
}
else
{
firstName = [ lcString substringFromIndex:NSMaxRange( commaRange ) ] ;
lastName = [ lcString substringToIndex:commaRange.location ] ;
}
NSCharacterSet * whitespaceCharSet = [ NSCharacterSet whitespaceAndNewlineCharacterSet ] ;
firstName = [ firstName stringByTrimmingCharactersInSet:whitespaceCharSet ] ;
lastName = [ firstName stringByTrimmingCharactersInSet:whitespaceCharSet ] ;
}
else if ( lcString hasPrefix:#"sound:" )
{
// handle name ("SOUND:")
}
// ... write code handle other parts of MECARD... (NICKNAME, BDAY, URL, etc)
else
{
// handle unknown case here
}
}
// you have names here
}
Related
My column value is a string set, how do i filter string set with string that contain a substring
For example
entry1 :
{
ss : [
"TRUE_item",
"FALSE_item"
]
}
entry2 :
{
ss : [
"FASE_item",
"FALSE_item"
]
}
How to i filter entry wihich contain ss that have an element contain TRUE, which in this case should return entry 1?
You cannot. You must give the entire value to match a string within a set.
I'm passing a JSON object to jq and want to add extra objects to an inner array ('accessories') if its parent array ('platforms') matches a certain name.
Here's my source JSON:
{
"bridge": {
"name": "Homebridge",
"port": 51395
},
"accessories": [],
"platforms": [
{
"name": "Config",
"port": 8581,
"platform": "config"
},
{
"platform": "homebridge-cbus.CBus",
"name": "CBus",
"client_ip_address": "127.0.0.1",
"accessories": [
{
"values": "existing"
}
]
}
]
}
This works beautifully:
jq '.platforms[1].accessories += [{ "values" : "NEW" }]'
... but of course it's poor form to expect platforms[1] to always the be array I want to append to, so I set about trying to form the right syntax for a search or if/then/else to only act on the .name of the appropriate one.
I thought this was my solution:
jq '.platforms[] | if ( .name=="CBus" ) then .accessories += [{ "values" : "NEW" }] else . end'
... until I realised it was only passing the 'platforms' through and eating the 'bridge' object and empty outer 'accessories' array, which I need to retain.
My issue looks to be similar to JQ | Updating array element selected by `select`, but I've tried LOTS of combinations but just can't break through.
Edit: Here's the correct JQPlay I've been working with:
https://jqplay.org/s/dGDswqAEte
Thanks for any help.
That's a good attempt. The key here is to use the select() function to identify the object you are going to update and overwrite the overall array using |= operator, i.e.
.platforms |= ( map(select(.name == "CBus").accessories += [{ "values" : "NEW" }] ) )
For the snippet in your jq-play link (now removed), you need to do
.gcp_price_list."CP-COMPUTEENGINE-OS"
|= with_entries(select(.value.cores == "shared").value.cores = "0.5")
Or if you want to be even more specific, and keep the entry in gcp_price_list configurable, do
.gcp_price_list |=
with_entries (
select(.key == "CP-COMPUTEENGINE-OS").value |=
with_entries(
select(.value.cores == "shared").value.cores = "0.5") )
componentsSeparatedByString: of NSString is nice, but this time I need it to include also the components. So a hypothetic
NSArray* array = [#",hello,world,kominami," componentsSeparatedByAndIncludingString:#","];
would give me an array with components
,
hello
,
world
,
kominami
,
Is there any such thing?
What action are you attempting to perform with this method?
There are a couple of ways to address that, but you could start with an array literal, but that is immutable:
NSArray *strings = #[ #[ #"A", #"B", #"C" ], #[ #"D", #"E", #"F" ], #[ #"G", #"H", #"I" ] ];
However I would look at a mutable array to perform this.
NSMutableArray *strings = [NSMutableArray array];
for(int i = 0; i < DESIRED_MAJOR_SIZE; i++)
{
[strings addObject: [NSMutableArray arrayWithObject:#""
count:DESIRED_MINOR_SIZE]];
}
I have a hidden field that gets populated with a javascript array of ID's. When I try to iterate the hidden field(called "hidExhibitsIDs") it gives me an error(in the title).
this is my loop:
foreach(string exhibit in hidExhibitsIDs.Value)
{
comLinkExhibitToTask.Parameters.AddWithValue("#ExhibitID", exhibit);
}
when I hover over the .value it says it is "string". But when I change the "string exhibit" to "int exhibit" it works, but gives me an internal error(not important right now).
You need to convert string to string array to using in for loop to get strings not characters as your loop suggests. Assuming comma is delimiter character in the hidden field, hidden field value will be converted to string array by split.
foreach(string exhibit in hidExhibitsIDs.Value.Split(','))
{
comLinkExhibitToTask.Parameters.AddWithValue("#ExhibitID", exhibit);
}
Value is returning a String. When you do a foreach on a String, it iterates over the individual characters in it. What does the value actually look like? You'll have to parse it correctly before you try to use the data.
Example of what your code is somewhat doing right now:
var myString = "Hey";
foreach (var c in myString)
{
Console.WriteLine(c);
}
Will output:
H
e
y
You can use Char.ToString in order to convert
Link : http://msdn.microsoft.com/en-us/library/3d315df2.aspx
Or you can use this if you want convert your tab of char
char[] tab = new char[] { 'a', 'b', 'c', 'd' };
string str = new string(tab);
Value is a string, which implements IEnumerable<char>, so when you foreach over a string, it loops over each character.
I would run the debugger and see what the actual value of the hidden field is. It can't be an array, since when the POST happens, it is converted into a string.
On the server side, The Value property of a HiddenField (or HtmlInputHidden) is just a string, whose enumerator returns char structs. You'll need to split it to iterate over your IDs.
If you set the value of the hidden field on the client side with a JavaScript array, it will be a comma-separated string on the server side, so something like this will work:
foreach(string exhibit in hidExhibitsIDs.Value.Split(','))
{
comLinkExhibitToTask.Parameters.AddWithValue("#ExhibitID", exhibit);
}
public static string reversewordsInsentence(string sentence)
{
string output = string.Empty;
string word = string.Empty;
foreach(char c in sentence)
{
if (c == ' ')
{
output = word + ' ' + output;
word = string.Empty;
}
else
{
word = word + c;
}
}
output = word + ' ' + output;
return output;
}
Now I am trying to add a front end check on my app to detect if user input only number in the textfield.
I use:
- (IBAction)checkID:(UITextField *)sender {
if ([sender.text isEqualToString:#""]) {
sender.text = #"This information is required";
sender.backgroundColor =[UIColor redColor];
}else if (![sender.text intValue]) {
sender.text = [sender.text stringByAppendingString:#" is not valid number"];
sender.backgroundColor =[UIColor redColor];
}
NSLog(#"send.text is %#, intValue is %d",sender.text,[sender.text intValue]);
}
But I found it text begins with number and ends with string, its intValue is still the number.
In my text, text is "00001aa", but the intValue is 1.
Is there any other way to filter out this "00001aa" text?
Thanks in advance.
Yes, you can use NSRegularExpression, or NSCharacterSet (works for positive numbers).
For regular expressions, use ^[-+]?[0-9]+$.
NSRegularExpression *numEx = [NSRegularExpression
regularExpressionWithPattern:#"^[-+]?[0-9]+$" options:0 error:nil
];
NSLog(#"%ld", [numEx numberOfMatchesInString:#"-200" options:0 range:NSMakeRange(0, 4)]);
NSLog(#"%ld", [numEx numberOfMatchesInString:#"001A" options:0 range:NSMakeRange(0, 4)]);
For character set, use [NSCharacterSet decimalDigitCharacterSet].
BOOL isNum = [[NSCharacterSet decimalDigitCharacterSet]
isSupersetOfSet:[NSCharacterSet characterSetWithCharactersInString:#"001AA"]
];