在本文中,我们将详细介绍如何从NSString捕获最后4个字符的各个方面,并为您提供关于获取string最后一个字符的相关解答,同时,我们也将为您带来关于159-PHPstrstr函数,取最后几个字符
在本文中,我们将详细介绍如何从NSString捕获最后4个字符的各个方面,并为您提供关于获取string最后一个字符的相关解答,同时,我们也将为您带来关于159-PHP strstr函数,取最后几个字符和除去最后几个字符、BASH:从Standard Out,Inline获取输出的最后4个字符、c – 为什么添加两个字符串文字不使用operator(const string&,const string&)?、cocoa – 如何从NSAttributedString获取图像数据的有用知识。
本文目录一览:- 如何从NSString捕获最后4个字符(获取string最后一个字符)
- 159-PHP strstr函数,取最后几个字符和除去最后几个字符
- BASH:从Standard Out,Inline获取输出的最后4个字符
- c – 为什么添加两个字符串文字不使用operator(const string&,const string&)?
- cocoa – 如何从NSAttributedString获取图像数据
如何从NSString捕获最后4个字符(获取string最后一个字符)
我接受了NSString
随机size
从UITextField
并将其传递到一个方法,我创造,只有将捕获在进入最后4个字符string
。
我已经浏览了NSString
类引用库,并且发现了唯一真正的选项,它看起来像我想要的那样
- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange
我之前使用过一次,但是静态参数“不会改变”,但是对于此实现,我想使用随输入字符串的大小而变化的非静态参数。
到目前为止,这是我创建的方法,该方法是NSString
从IBAction 传递到其他地方的。
- (void)padString:(NSString *)funcString{ NSString *myFormattedString = [NSString stringWithFormat:@"%04d",[funcString intValue]]; // if less than 4 then pad string // NSLog(@"my formatedstring = %@", myFormattedString); int stringLength = [myFormattedString length]; // captures length of string maybe I can use this on NSRange? //NSRange MyOneRange = {0, 1}; //<<-------- should I use this? if so how?}
答案1
小编典典使用substringFromIndex方法,
OBJ-C:
NSString *trimmedString=[string substringFromIndex:MAX((int)[string length]-4, 0)]; //in case string is less than 4 characters long.
迅速:
let trimmedString: String = (s as NSString).substringFromIndex(max(s.length-4,0))
159-PHP strstr函数,取最后几个字符和除去最后几个字符
<?php
$str=''programming''; //定义一个字符串
$res=substr($str,-4); //处理字符串
echo "返回{$str}字符串的最后4个字符{$res}";
$res=substr($str,0,-4); //处理字符串
echo "<br />返回{$str}字符串除去后4个字符{$res}";
?>
BASH:从Standard Out,Inline获取输出的最后4个字符
我有一个正在运行和使用的脚本
lspci -s 0a.00.1
这返回
0a.00.1 usb controller some text device 4dc9
我想要得到最后4个字符内联
lspci -s 0a.00.1 | some command to give me the last 4 characters.
Nginx和Unicorn的多个Rails应用程序的一个域名
让一名工人在Unicorn Nginx Rails中连续运行
Nginx,独angular兽和Heroku
-su:bundle:启动独angular兽时找不到命令
如何使用文件服务器和wamp的Angular 2
Nginx:使用try_files指令禁止单个文件的caching
Webpack构build不会更新Apache服务器上的内容
我怎么知道为什么我的Azure实例不启动?
独angular兽vs独立后面的Nginx
Unicorn.rbconfiguration取决于环境
tail怎么样,用-c开关。 例如,要获得“hello”的最后4个字符:
echo "hello" | tail -c 5 ello
请注意,我使用5(4 + 1),因为换行添加了换行符。 正如下面Brad Koch所建议的那样,使用echo -n来防止添加换行符。
你真的想要最后四个字吗? 它看起来像你想要的最后一个“单词”就行了:
awk '{ print $NF }'
如果ID是3个字符或5,这将工作。
使用sed :
lspci -s 0a.00.1 | sed 's/^.*(.{4})$/1/'
输出:
4dc9
试试这个,如果字符串存储在变量foo中。
foo=`lspci -s 0a.00.1` # the foo value should be "0a.00.1 usb controller some text device 4dc9" echo ${foo:(-4)} # which should output 4dc9
我通常使用
echo 0a.00.1 usb controller some text device 4dc9 | rev | cut -b1-4 | rev 4dc9
尝试使用grep :
lspci -s 0a.00.1 | grep -o ....$
这将打印每行的最后4个字符。
但是,如果您想要输出整个输出的最后4个字符,请改用tail -c4 。
如果真正的请求是复制最后一个由空格分隔的字符串而不考虑它的长度,那么最好的解决方案似乎是使用... | awk '{print $NF}' ... | awk '{print $NF}'由@Johnsyweb给出。 但是,如果这确实是从字符串的末尾复制固定数量的字符,那么有一个特定于bash的解决方案,而不需要通过管道调用任何进一步的子进程:
$ test="1234567890"; echo "${test: -4}" 7890 $
请注意,冒号和减号之间的空格是必不可少的,因为没有它,完整的字符串将被传送:
$ test="1234567890"; echo "${test:-4}" 1234567890 $
还有一种方法是使用<<<符号:
tail -c 5 <<< '0a.00.1 usb controller some text device 4dc9'
而不是使用命名变量,开发使用位置参数的做法,如下所示:
set -- $( lspci -s 0a.00.1 ); # then the bash string usage: echo ${1:(-4)} # has the advantage of allowing N PP's to be set,eg: set -- $(ls *.txt) echo $4 # prints the 4th txt file.
c – 为什么添加两个字符串文字不使用operator(const string&,const string&)?
为什么这样做:
struct A {}; struct B { B(A){} }; void operator+(const B&,const B&) {} int main() { A a1,a2; a1 + a2; }
这不是吗?
struct B { B(const char*){} }; void operator+(const B&,const B&) {} //error: invalid operands of types 'const char [6]' and 'const char [6]' to binary 'operator+'| int main() { "Hello" + "world"; }
本质上,在第一个例子中,a1和a2都通过隐式转换转换为B对象,并使用运算符(const B& const B&)来添加.
从这个例子中,我将期望“Hello”和“world”通过隐式构造函数再次转换为B对象,并使用operator(const B& const B&)来相互添加.相反,有一个错误,这表示C样式的字符串不会尝试用户定义的转换为B以添加.为什么是这样?是否有根本的财产阻止这一点?
解决方法
[C++14: 13.3.1.2/2]:
If either operand has a type that is a class or an enumeration,a user-defined operator function might be declared that implements this operator or a user-defined conversion can be necessary to convert the operand to a type that is appropriate for a built-in operator. In this case,overload resolution is used to determine which operator function or built-in operator is to be invoked to implement the operator. [..]
[C++14: 13.3.2/1]:
From the set of candidate functions constructed for a given context (13.3.1),a set of viable functions is chosen,from which the best function will be selected by comparing argument conversion sequences for the best fit (13.3.3). The selection of viable functions considers relationships between arguments and function parameters other than the ranking of conversion sequences.
[C++14: 13.3.2/2]:
First,to be a viable function,a candidate function shall have enough parameters to agree in number with the arguments in the list.
- If there are
m
arguments in the list,all candidate functions having exactlym
parameters are viable.- [..]
[C++14: 13.3.2/3]
: Second,forF
to be a viable function,there shall exist for each argument an implicit conversion sequence (13.3.3.1) that converts that argument to the corresponding parameter ofF
. [..]
(您可以自己检查“隐式转换序列”的措辞,看看运算符电话是允许的;这些规则太冗长,无法保证在这里逐字复制.)
然而,在第二个例子中,重载分辨率被限制为一个基本的算术加法机制(一个没有为const char [N]或const char *定义),有效地禁止任何运算符函数被考虑:
[C++14: 13.3.1.2/1]:
If no operand of an operator in an expression has a type that is a class or an enumeration,the operator is assumed to be a built-in operator and interpreted according to Clause 5.
[C++14: 5.7/1]:
[..] For addition,either both operands shall have arithmetic or unscoped enumeration type,or one operand shall be a pointer to a completely-defined object type and the other shall have integral or unscoped enumeration type. [..]
[C++14: 5.7/3]:
The result of the binary + operator is the sum of the operands.
cocoa – 如何从NSAttributedString获取图像数据
我在NSAttributedString上使用类别来获取文本附件.如果可能的话,我宁愿不写入磁盘.
- (NSArray *)allAttachments { NSError *error = NULL; NSMutableArray *theAttachments = [NSMutableArray array]; NSRange theStringRange = NSMakeRange(0,[self length]); if (theStringRange.length > 0) { NSUInteger N = 0; do { NSRange theEffectiveRange; NSDictionary *theAttributes = [self attributesAtIndex:N longestEffectiveRange:&theEffectiveRange inRange:theStringRange]; NSTextAttachment *theAttachment = [theAttributes objectForKey:NSAttachmentAttributeName]; if (theAttachment != NULL){ NSLog(@"filewrapper: %@",theAttachment.fileWrapper); [theAttachments addobject:theAttachment]; } N = theEffectiveRange.location + theEffectiveRange.length; } while (N < theStringRange.length); } return(theAttachments); }
解决方法
>获取附件的文件包装器.
>写一个URL.
[textStorage enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0,textStorage.length) options:0 usingBlock:^(id value,NSRange range,BOOL *stop) { NSTextAttachment* attachment = (NSTextAttachment*)value; NSFileWrapper* attachmentWrapper = attachment.fileWrapper; [attachmentWrapper writetoURL:outputURL options:NSFileWrapperWritingAtomic originalContentsURL:nil error:nil]; (*stop) = YES; // stop so we only write the first attachment }];
此示例代码仅将第一个附件写入outputURL.
我们今天的关于如何从NSString捕获最后4个字符和获取string最后一个字符的分享已经告一段落,感谢您的关注,如果您想了解更多关于159-PHP strstr函数,取最后几个字符和除去最后几个字符、BASH:从Standard Out,Inline获取输出的最后4个字符、c – 为什么添加两个字符串文字不使用operator(const string&,const string&)?、cocoa – 如何从NSAttributedString获取图像数据的相关信息,请在本站查询。
本文标签: