holydancer原创,如需转载,请在显要位置注明:
转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/7343561
objective C中的字符串操作
在OC中创建字符串时,一般不使用C的方法,因为C将字符串作为字符数组,所以在操作时会有很多不方便的地方,在Cocoa中NSString集成的一些方法,可以很方便的操作字符串,下面举几个例子:
1、创建:
直接利用等号赋值
NSString * str1=@"我是字符串";
stringWithFormat方法
NSString *str2=[NSString stringWithFormat:@"我是参数字符串%d,%d",11,111"];
2,合并:
NSString 字符串是不可改变的,可以在原字符串的基础上生成新的字符串,但是原字符串并没有改变,但是NSString 提供了一个子类:NSMutableString.该类是可变的,NSString可用的方法也同样适用于NSMutableString,但是使用之前要预先声明:
NSMutableString *str3 = [NSMutableString stringWithCapacity:50];
需要注意的是,该处声明时虽然给了50的容量,但是NSMutableString会自动扩充,所以不用担心给的空间小了。
创建了NSMutableString字符串,我们就可以进行合并操作了,常用方法有:
以上代码输出结果为:“NSString字符串5”;
3,删减:
字符串的删减,需要用到一个辅助结构体:NSRange;
NSRange的两个属性length,location,既可用于图形辅助,又可用于字符串辅助,在字符串辅助中,表示字符串中的一段范围,location指起始位置,length指包含字符的个数。创建NSRange的方法有三种:
(一)
NSRange range;
range.location = 10;
range.length = 2;
(二)
NSRange range={10,2};
(三)
NSRange range=NSMakeRange(10,2);//此种方法较为常用。
下面看段代码,如何使用NSRange辅助修改字符串内容:
输出结果如下:
I holydancer,2,2
4,比较:
(一)是否相等的比较:
和java中某些情况类似,字符串并不能直接用==比较,==比较的是两个字符串是否是同一个对象,而我们要比较的通常是两个字符串是否相等,这时应该使用isEqualToString,而不是用==比较其指针值。isEqualToString的返回值是BOOL类型,为YES和NO。
(二)字符串大小的比较:
字符串根据排列顺序的不同,会有大小的差异,如果我们要比较两个字符串的大小的话,可以使用compare方法。compare 方法返回的结果有三种:NSOrderedAscending,NSOrderedSame,NSOrderedDescending,另外,在使用compare方法时可以添加参数来决定是否区分大小写,或者声明为比较字符串个数而不是字符值等比较条件。
具体使用看如下DEMO:
如果要忽略大小写的话,可以在options 里面加一个参数NSCaseInsensitiveSearch;具体如下
NSComparisonResult result2 =[str1 compare:str2 options:NSCaseInsensitiveSearch];
和NSCaseInsensitiveSearch对应的条件还有NSLiteralSearch(区分大小写),NSNumericSearch(按字符个数比较),并可以使用"|"来同时满足多个条件。
(三)判断字符串的开头和结尾
hasPrefix方法判断是否以某字符串开头,hasSuffix判断是否以某字符串结尾。
关键字:objective-c ,objective c , oc ,字符串
一、效果图:
二、布局文件
header_view.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app1="http://schemas.android.com/apk/res/com.johnny.flowindicatortest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Gallery
android:id="@+id/home_gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:spacing="5dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#65000000"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_gal_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:textColor="#ffffff"
android:textSize="18sp" />
<com.johnny.flowindicatortest.FlowIndicator
android:id="@+id/myview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:gravity="center"
app:count="4"
app:point_normal_color="#45000000"
app:point_radius="3dip"
app:point_seleted_color="#ffffff"
app:point_size="5dip"
app:space="10dp" />
</LinearLayout>
</FrameLayout>
gallery_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/home_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="/blog_article/@drawable/t1/index.html" />
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FlowIndicator">
<attr name="count" format="integer" />
<attr name="space" format="dimension" />
<attr name="point_size" format="dimension" />
<attr name="point_seleted_color" format="color|reference" />
<attr name="point_normal_color" format="color|reference" />
<attr name="point_radius" format="dimension" />
</declare-styleable>
</resources>
FlowIndicator.java
public class FlowIndicator extends View {
private int count;
private float space, radius;
private int point_normal_color, point_seleted_color;
// 选中
private int seleted = 0;
public FlowIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
//提供TypedArray(用于Drawable对象数组)的XML资源。
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.FlowIndicator);
count = typedArray.getInteger(R.styleable.FlowIndicator_count,4);
space = typedArray.getDimension(R.styleable.FlowIndicator_space, 9);
radius = typedArray.getDimension(R.styleable.FlowIndicator_point_radius, 9);
point_normal_color = typedArray.getColor(R.styleable.FlowIndicator_point_normal_color, 0x000000);
point_seleted_color = typedArray.getColor(R.styleable.FlowIndicator_point_seleted_color, 0xffff07);
int sum = attrs.getAttributeCount();
if(Constans.DEBUG){
String str = "";
for(int i=0;i<sum;i++){
String name = attrs.getAttributeName(i);
String value = attrs.getAttributeValue(i);
str += "sttr_name:" + name +": " + value +"\n";
}
Log.i("attribute", str);
}
typedArray.recycle();
}
public void setSeletion(int index){
this.seleted = index;
//重绘
invalidate();
}
public void setCount(int count){
this.count = count;
invalidate();
}
public void next(){
if(seleted < count-1){
seleted++;
}else{
seleted = 0;
}
invalidate();
}
public void previous(){
if(seleted > 0 ){
seleted--;
}else{
seleted = count-1;
}
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true);
float width = (getWidth() - ((radius * 2 * count) + (space * (count - 1))))/2.f;
for (int i = 0; i < count; i++) {
if (i == seleted)
paint.setColor(point_seleted_color);
else
paint.setColor(point_normal_color);
canvas.drawCircle(width + getPaddingLeft() + radius + i
* (space + radius + radius), getHeight() / 2, radius, paint);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
}
private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = (int) (getPaddingLeft() + getPaddingRight()
+ (count * 2 * radius) + (count - 1) * radius + 1);
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
private int measureHeight(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = (int) (2 * radius + getPaddingTop() + getPaddingBottom() + 1);
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
}
MainActivity.java
public class MainActivity extends Activity {
private static final int SCROLL_ACTION = 0;
private TextView textView;
private Gallery mGallery;
private FlowIndicator myView;
Timer mTimer;
private GalleryAdapter galleryAdapter;
private String[] titles = {"标题1","标题2","标题3","标题4","标题5","标题6","标题7","标题8","标题9"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.header_view);
viewInit();
//定时滚动
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new MyTask(), 0, 5000);
}
private void viewInit(){
textView = (TextView) findViewById(R.id.tv_gal_title);
mGallery = (Gallery) findViewById(R.id.home_gallery);
myView = (FlowIndicator) findViewById(R.id.myview);
galleryAdapter = new GalleryAdapter(this);
myView.setCount(galleryAdapter.getCount());
mGallery.setAdapter(galleryAdapter);
mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
textView.setText(titles[arg2]);
myView.setSeletion(arg2);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
private class GalleryAdapter extends BaseAdapter{
Context mContext;
int[] res = new int[] { R.drawable.t1, R.drawable.t2,
R.drawable.t3, R.drawable.t1, R.drawable.t2,
R.drawable.t3, R.drawable.t1, R.drawable.t2,
R.drawable.t3 };
public GalleryAdapter(Context cnt) {
this.mContext = cnt;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return res.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return res[arg0];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.gallery_item, null);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.home_img);
imageView.setImageResource(res[position]);
return convertView;
}
}
private class MyTask extends TimerTask {
@Override
public void run() {
mHandler.sendEmptyMessage(SCROLL_ACTION);
}
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case SCROLL_ACTION:
MotionEvent e1 = MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,
89.333336f, 265.33334f, 0);
MotionEvent e2 = MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,
300.0f, 238.00003f, 0);
mGallery.onFling(e1, e2, -1300, 0);
break;
default:
break;
}
}
};
}
Constans.java
public class Constans {
public static boolean DEBUG = true;
}
<style>
/* center icons */
.ui-grid-a { text-align: center; }
/* set row height */
.ui-block-a, .ui-block-b { height: 150px; position: relative; }
/* set label size and color */
.icon-label { color:#009966; display: block; font-size:12px; }
/* position the icons at the bottom of the block to adjust for uneven icon heights */
.icon-springboard { position: absolute; bottom: 0; width: 100%;}
a:link, a:visited, a:hover, a:active { text-decoration:none; }
</style>
<div data-role="content" data-theme="b"> <div > <div > <div > <a href="#" > <img src="/blog_article/images/Live%20Mail.png" alt="Coming Soon" > <span >收件箱</span> </a> </div> </div> <div > <div > <a href="#"> <img src="/blog_article/images/Email%20Chat.png" alt="Coming Soon" > <span >已发邮件</span> </a> </div> </div> <div > <div > <a href="#" > <img src="/blog_article/images/myspace_grn.png" alt="Coming Soon" > <span >通讯录</span> </a> </div> </div> <div > <div > <a href="#"> <img src="/blog_article/images/Google%20Drive%20Folder.png" alt="Coming Soon" > <span >网络硬盘</span> </a> </div> </div> <div > <div > <a href="#"> <img src="/blog_article/images/Gmai%20lalt.png" alt="Tickets" > <span >写邮件</span> </a> </div> </div> <div > <div > <a href="/blog_article/ch4/contact-us.html"> <img src="/blog_article/images/Gmail.png" alt="Contact Us" > <span >我的邮件</span> </a> </div> </div> </div> </div>