简体   繁体   English

将字符串数组中的元素转换为字符

[英]Convert element in string array to character

given an array String [] stringtest= new String[]{"dog","bark","1","m"}; 给定一个数组String [] stringtest= new String[]{"dog","bark","1","m"};

how could I store the fourth element as a character? 如何将第四个元素存储为字符? The reason being that I need to compare the last element to a character inside an if statement. 原因是我需要将最后一个元素与if语句中的字符进行比较。

I need if(stringtest[3]=='m') to work 我需要if(stringtest[3]=='m')来工作

you can convert string to char array 您可以将字符串转换为char数组

char[] ch= stringtest[3].toCharArray();
if(ch[0]=='m')

or you can use charAt method 或者你可以使用charAt方法

if(stringtest[3].charAt(0)=='m')

you can use toCharArray() method of string 您可以使用字符串的toCharArray()方法

this would work, although not clear why you want it like that 这会起作用,尽管不清楚为什么要这样

if( stringtest[3].toCharArray()[0] == 'm')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM