NSString *stringList=@"nice,good,perfect";
NSArray *list=[stringList componentsSeparatedByString:@","];
http://www.cnblogs.com/phonlin/articles/1941478.html /******************************************************************************************* NSString *******************************************************************************************/ //一、NSString /*----------------创建字符串的方法----------------*/ //1、创建常量字符串。 NSString *astring = @"This is a String!"; //2、创建空字符串,给予赋值。 NSString *astring = [[NSString alloc] init]; astring = @"This is a String!"; [astring release]; NSLog(@"astring:%@",astring); // NSString *astring = [[NSString alloc] init]; NSLog(@"0x%.8x", astring); astring=@"This is a String!"; NSLog(@"0x%.8x", astring); [astring release]; NSLog(@"astring:%@",astring); //3、在以上方法中,提升速度:initWithString方法 NSString *astring = [[NSString alloc] initWithString:@"This is a String!"]; NSLog(@"astring:%@",astring); [astring release]; //4、用标准c创建字符串:initWithCString方法 char *Cstring = "This is a String!"; NSString *astring = [[NSString alloc] initWithCString:Cstring]; NSLog(@"astring:%@",astring); [astring release]; //5、创建格式化字符串:占位符(由一个%加一个字符组成) int i = 1; int j = 2; NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]]; NSLog(@"astring:%@",astring); [astring release]; //6、创建临时字符串 NSString *astring; astring = [NSString stringWithCString:"This is a temporary string"]; NSLog(@"astring:%@",astring); /*----------------从文件读取字符串:initWithContentsOfFile方法----------------*/ NSString *path = @"astring.text"; NSString *astring = [[NSString alloc] initWithContentsOfFile:path]; NSLog(@"astring:%@",astring); [astring release]; /*----------------写字符串到文件:writeToFile方法----------------*/ NSString *astring = [[NSString alloc] initWithString:@"This is a String!"]; NSLog(@"astring:%@",astring); NSString *path = @"astring.text"; [astring writeToFile: path atomically: YES]; [astring release]; /*----------------比较两个字符串----------------*/ //用C比较:strcmp函数 char string1[] = "string!"; char string2[] = "string!"; if(strcmp(string1, string2) = = 0) { NSLog(@"1"); } //isEqualToString方法 NSString *astring01 = @"This is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 isEqualToString:astring02]; NSLog(@"result:%d",result); //compare方法(comparer返回的三种值) NSString *astring01 = @"This is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02] = = NSOrderedSame; NSLog(@"result:%d",result); //NSOrderedSame判断两者内容是否相同 NSString *astring01 = @"This is a String!"; NSString *astring02 = @"this is a String!"; BOOL result = [astring01 compare:astring02] = = NSOrderedAscending; NSLog(@"result:%d",result); //NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真) NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02] = = NSOrderedDescending; NSLog(@"result:%d",result); //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真) //不考虑大小写比较字符串1 NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame; NSLog(@"result:%d",result); //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真) //不考虑大小写比较字符串2 NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02 options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame; NSLog(@"result:%d",result); //NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。 /*----------------改变字符串的大小写----------------*/ NSString *string1 = @"A String"; NSString *string2 = @"String"; NSLog(@"string1:%@",[string1 uppercaseString]);//大写 NSLog(@"string2:%@",[string2 lowercaseString]);//小写 NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小 /*----------------在串中搜索子串----------------*/ NSString *string1 = @"This is a string"; NSString *string2 = @"string"; NSRange range = [string1 rangeOfString:string2]; int location = range.location; int leight = range.length; NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]]; NSLog(@"astring:%@",astring); [astring release]; /*----------------抽取子串 ----------------*/ //-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符 NSString *string1 = @"This is a string"; NSString *string2 = [string1 substringToIndex:3]; NSLog(@"string2:%@",string2); //-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符 NSString *string1 = @"This is a string"; NSString *string2 = [string1 substringFromIndex:3]; NSLog(@"string2:%@",string2); //-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串 NSString *string1 = @"This is a string"; NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)]; NSLog(@"string2:%@",string2); //扩展路径 NSString *Path = @"~/NSData.txt"; NSString *absolutePath = [Path stringByExpandingTildeInPath]; NSLog(@"absolutePath:%@",absolutePath); NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]); //文件扩展名 NSString *Path = @"~/NSData.txt"; NSLog(@"Extension:%@",[Path pathExtension]); /******************************************************************************************* NSMutableString *******************************************************************************************/ /*---------------给字符串分配容量----------------*/ //stringWithCapacity: NSMutableString *String; String = [NSMutableString stringWithCapacity:40]; /*---------------在已有字符串后面添加字符----------------*/ //appendString: and appendFormat: NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; //[String1 appendString:@", I will be adding some character"]; [String1 appendFormat:[NSString stringWithFormat:@", I will be adding some character"]]; NSLog(@"String1:%@",String1); */ /*--------在已有字符串中按照所给出范围和长度删除字符------*/ /* //deleteCharactersInRange: NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [String1 deleteCharactersInRange:NSMakeRange(0, 5)]; NSLog(@"String1:%@",String1); /*--------在已有字符串后面在所指定的位置中插入给出的字符串------*/ //-insertString: atIndex: NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [String1 insertString:@"Hi! " atIndex:0]; NSLog(@"String1:%@",String1); /*--------将已有的空符串换成其它的字符串------*/ //-setString: NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [String1 setString:@"Hello Word!"]; NSLog(@"String1:%@",String1); /*--------按照所给出的范围,和字符串替换的原有的字符------*/ //-setString: NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"]; NSLog(@"String1:%@",String1); /*-------------判断字符串内是否还包含别的字符串(前缀,后缀)-------------*/ //01:检查字符串是否以另一个字符串开头- (BOOL) hasPrefix: (NSString *) aString; NSString *String1 = @"NSStringInformation.txt"; [String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO"); [String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO"); //02:查找字符串某处是否包含其它字符串 - (NSRange) rangeOfString: (NSString *) aString,这一点前面在串中搜索子串用到过; /******************************************************************************************* NSArray *******************************************************************************************/ /*---------------------------创建数组------------------------------*/ //NSArray *array = [[NSArray alloc] initWithObjects: @"One",@"Two",@"Three",@"Four",nil]; self.dataArray = array; [array release]; //- (unsigned) Count;数组所包含对象个数; NSLog(@"self.dataArray cound:%d",[self.dataArray count]); //- (id) objectAtIndex: (unsigned int) index;获取指定索引处的对象; NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]); /*--------------------------从一个数组拷贝数据到另一数组(可变数级)----------------------------*/ //arrayWithArray: //NSArray *array1 = [[NSArray alloc] init]; NSMutableArray *MutableArray = [[NSMutableArray alloc] init]; NSArray *array = [NSArray arrayWithObjects: @"a",@"b",@"c",nil]; NSLog(@"array:%@",array); MutableArray = [NSMutableArray arrayWithArray:array]; NSLog(@"MutableArray:%@",MutableArray); array1 = [NSArray arrayWithArray:array]; NSLog(@"array1:%@",array1); //Copy //id obj; NSMutableArray *newArray = [[NSMutableArray alloc] init]; NSArray *oldArray = [NSArray arrayWithObjects: @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil]; NSLog(@"oldArray:%@",oldArray); for(int i = 0; i
【方法一】使用自定义AlertDialog
private static final int PHOTO_SUCCESS = 1;
private static final int CAMERA_SUCCESS = 2;
private ImageButton pic; //图片选择按钮
// 从相册或相机选择图片
pic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final CharSequence[] items = { "手机相册", "相机拍摄" };
AlertDialog dlg = new AlertDialog.Builder(NewTopic.this).setTitle("选择图片").setItems(items,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int item) {
//这里item是根据选择的方式,
//在items数组里面定义了两种方式, 拍照的下标为1所以就调用拍照方法
if(item==1){
Intent getImageByCamera= new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(getImageByCamera, CAMERA_SUCCESS);
}else{
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType("image/*");
startActivityForResult(getImage, PHOTO_SUCCESS);
}
}
}).create();
dlg.show();
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
ContentResolver resolver = getContentResolver();
if (resultCode == RESULT_OK) {
switch (requestCode) {
case PHOTO_SUCCESS:
//获得图片的uri
Uri originalUri = intent.getData();
Bitmap bitmap = null;
try {
Bitmap originalBitmap = BitmapFactory.decodeStream(resolver.openInputStream(originalUri));
bitmap = resizeImage(originalBitmap, 200, 200);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(bitmap != null){
//根据Bitmap对象创建ImageSpan对象
ImageSpan imageSpan = new ImageSpan(NewTopic.this, bitmap);
//创建一个SpannableString对象,以便插入用ImageSpan对象封装的图像
SpannableString spannableString = new SpannableString("[local]"+1+"[/local]");
// 用ImageSpan对象替换face
spannableString.setSpan(imageSpan, 0, "[local]1[local]".length()+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//将选择的图片追加到EditText中光标所在位置
int index = edit.getSelectionStart(); //获取光标所在位置
Editable edit_text = edit.getEditableText();
if(index <0 || index >= edit_text.length()){
edit_text.append(spannableString);
}else{
edit_text.insert(index, spannableString);
}
}else{
Toast.makeText(NewTopic.this, "获取图片失败", Toast.LENGTH_SHORT).show();
}
break;
case CAMERA_SUCCESS:
Bundle extras = intent.getExtras();
Bitmap originalBitmap1 = (Bitmap) extras.get("data");
if(originalBitmap1 != null){
bitmap = resizeImage(originalBitmap1, 200, 200);
//根据Bitmap对象创建ImageSpan对象
ImageSpan imageSpan = new ImageSpan(NewTopic.this, bitmap);
//创建一个SpannableString对象,以便插入用ImageSpan对象封装的图像
SpannableString spannableString = new SpannableString("[local]"+1+"[/local]");
// 用ImageSpan对象替换face
spannableString.setSpan(imageSpan, 0, "[local]1[local]".length()+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//将选择的图片追加到EditText中光标所在位置
int index = edit.getSelectionStart(); //获取光标所在位置
Editable edit_text = edit.getEditableText();
if(index <0 || index >= edit_text.length()){
edit_text.append(spannableString);
}else{
edit_text.insert(index, spannableString);
}
}else{
Toast.makeText(NewTopic.this, "获取图片失败", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
}
/**
* 图片缩放
* @param originalBitmap 原始的Bitmap
* @param newWidth 自定义宽度
* @param newHeight自定义高度
* @return 缩放后的Bitmap
*/
private Bitmap resizeImage(Bitmap originalBitmap, int newWidth, int newHeight){
int width = originalBitmap.getWidth();
int height = originalBitmap.getHeight();
//定义欲转换成的宽、高
// int newWidth = 200;
// int newHeight = 200;
//计算宽、高缩放率
float scanleWidth = (float)newWidth/width;
float scanleHeight = (float)newHeight/height;
//创建操作图片用的matrix对象 Matrix
Matrix matrix = new Matrix();
// 缩放图片动作
matrix.postScale(scanleWidth,scanleHeight);
//旋转图片 动作
//matrix.postRotate(45);
// 创建新的图片Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(originalBitmap,0,0,width,height,matrix,true);
return resizedBitmap;
}
alertDialog 的运行效果类似这样:
【方法二】使用Intent.createChooser
还可以使用Intent选择器+隐式Intent的方法达到效果,只是这种方式比较繁琐,界面效果也差点。
首先,定义两个Activity来接受隐式intent, AndroidManifest.xml:
<activity android:name=".activity.action_get_content.PickPicFromLocalFile" android:label="手机相册">
<intent-filter>
<action android:name="open_pic_intent" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.OPENABLE" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
<activity android:name=".activity.action_get_content.PickPicFromCamera" android:label="相机拍摄" android:icon="@drawable/logo1">
<intent-filter>
<action android:name="open_pic_intent" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.OPENABLE" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
PickPicFromLocalFile.java:
调用了系统的打开本地图库的方法,把intent传给了我们自定义的页面,自定义页面再把Intent传给事件源页面。
/**
* 手机相册 使用Intent.createChooser弹出的菜单
* @author wangyx
* @version 1.0.0 2011-12-16
*/
public class PickPicFromLocalFile extends Activity {
protected static final int PICRESULT_LOCAL = 0;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
intent = getIntent();
Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);
//action_get_content是通过intent中设置的type属性来判断具体调用哪个程序的
innerIntent.setType("image/*");
startActivityForResult(innerIntent,PICRESULT_LOCAL);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
if(resultCode == RESULT_OK){
switch (reqCode) {
case (PICRESULT_LOCAL):
intent.setData(data.getData());
intent.putExtra("flag", 1); //回传用于判断是哪个网页传递的数据
setResult(RESULT_OK, intent);
finish();
break;
}
}
}
}
PickPicFromCamera.java:调用系统打开相机的方法,照片的数据保存在intent的bundle中,key值为"data"。
/**
* 手机相册 使用Intent.createChooser弹出的菜单
* @author wangyx
* @version 1.0.0 2011-12-16
*/
public class PickPicFromCamera extends Activity {
protected static final int PICRESULT_CAMERA = 0;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
intent = this.getIntent();
Intent getImageByCamera= new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(getImageByCamera, PICRESULT_CAMERA);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
if(resultCode == RESULT_OK){
switch (reqCode) {
case (PICRESULT_CAMERA):
Bundle extras = data.getExtras();
intent.putExtra("flag", 2);
intent.putExtras(extras);
setResult(RESULT_OK, intent);
finish();
break;
}
}
}
}
处理回传的数据,显示在ImageView上:
protected static final int PICRESULT_CODE = 0;
private ImageButton pic; //图片选择按钮
private ImageView iv;
// 从相册或相机选择图片
pic.setOnClickListener(new View.OnClickListener() {
Intent wrapperIntent = new Intent("open_pic_intent");
wrapperIntent.setType("image/*");
startActivityForResult(Intent.createChooser(wrapperIntent, "设置"), PICRESULT_CODE);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
ContentResolver resolver = getContentResolver();
if(resultCode == RESULT_OK){
switch (reqCode) {
case PICRESULT_CODE:
Bitmap myBitmap = null;
if(data.getIntExtra("flag", 0) == 1){
//处理本地图库返回的数据
try {
//获得图片的uri
Uri originalUri = data.getData();
//将图片内容解析成字节数组
byte[] mContent=readStream(resolver.openInputStream(Uri.parse(originalUri.toString())));
//将字节数组转换为ImageView可调用的Bitmap对象
myBitmap = getPicFromBytes(mContent, null);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}else if(data.getIntExtra("flag", 0) == 2){
try {
Bundle extras = data.getExtras();
myBitmap = (Bitmap) extras.get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG , 100, baos);
} catch (Exception e) {
e.printStackTrace();
}
}
//把得到的图片绑定在控件上显示
iv.setImageBitmap(myBitmap);
break;
default:
break;
}
}
}
public static Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) {
if (bytes != null)
if (opts != null)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,opts);
else
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return null;
}
public static byte[] readStream(InputStream inStream) throws Exception {
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
return data;
}
这两个自定义页中中你也可以直接处理图片,在回传的Intent中保存流给接收页面处理,这样做的好处是接受页不必知道是哪个页面回传的数据,少了层逻辑判断,直接将流转为BitMap显示即可,但缺点也很明显:
public Intent putExtra (String name, byte[] value)这个方法传递的字节数不能>40KB,如果超出会报 错。
在数返回显示的逻辑中使用了getPicFromBytes和readStream两个自定义方法,这里只是提供了一种方案,你仍然可以使用方法一中的处理方法。
效果图:
选项前的图标是AndroidManifest.xml中相关Activity的icon属性,默认会调用Activity所在项目的icon,目前还没有解决如何不显示选项之前的图标,希望知道的朋友,不吝赐教,留言告知。