
你是需要输出那个字符指针指向的内容吗,那样可以用%s, 如printf("%s", str);
如果是要查看整个字符串的值,只能够一次取出一个,
如
char p = "abc#d";
while(p!='\0') {
if(p == '#') {
printf("Find the char #");
break;
}
p ++; //这儿可以把指针挪到字符串之中的下一个字符上去,这样循环的移动指针就可以处理完整个字符串。
}
不知道,这个有没有解决你的问题?
$mysql_server_name= ; //改成自己的mysql数据库服务器
$mysql_username= 用户 ; //改成自己的mysql数据库用户名
$mysql_password= 密码 ; //改成自己的mysql数据库密码
$mysql_database= 数据库 ; //改成自己的mysql数据库名
mysql_connect( $mysql_username $mysql_password) or die( database not access );
mysql_select_db($mysql_database);
mysql_query("SET NAMES utf ");
$equery = " select title from 表 ";
$result =mysql_query($equery );
while ($row = mysql_fetch_array($result MYSQL_BOTH))
{
$title=$row["title"];
if (ord($title)> ) { //汉字开头
echo $letter=getfirstchar($title);
}else if(ord($title)>= and ord($title)<= ){ //数字开头
echo $letter=iconv_substr($title utf );
}else if(ord($title)>= and ord($title)<= ){ //大写英文开头
echo $letter=iconv_substr($title utf );
}else if(ord($title)>= and ord($title)<= ){ //小写英文开头
echo $letter=iconv_substr($title utf );
}
}
function getfirstchar($s ){
$s=iconv("UTF " "gb " $s );
$asc=ord($s{ }) +ord($s{ }) ;
if($asc>= and $asc<= )return "A";
if($asc>= and $asc<= )return "B"; if($asc>= and $asc<= )return "C";
if($asc>= and $asc<= )return "D";
if($asc>= and $asc<= )return "E";
if($asc>= and $asc<= )return "F";
if($asc>= and $asc<= )return "G";
if($asc>= and $asc<= )return "H";
if($asc>= and $asc<= )return "J";
if($asc>= and $asc<= )return "K";
if($asc>= and $asc<= )return "L";
if($asc>= and $asc<= )return "M";
if($asc>= and $asc<= )return "N";
if($asc>= and $asc<= )return "O";
if($asc>= and $asc<= )return "P";
if($asc>= and $asc<= )return "Q";
if($asc>= and $asc<= )return "R";
if($asc>= and $asc<= )return "S";
if($asc>= and $asc<= )return "T";
if($asc>= and $asc<= )return "W";
if($asc>= and $asc<= )return "X";
if($asc>= and $asc<= )return "Y";
if($asc>= and $asc<= )return "Z";
return false;
lishixinzhi/Article/program/PHP/201311/20913
你先用函数mid提取中文文字,再通过我找的别人提取中文字首字母的方法
先定义名称:插入-名称-定义
拼音={"","";"吖","A";"八","B";"嚓","C";"咑","D";"鵽","E";"发","F";"猤","G";"铪","H";"夻","J";"咔","K";"垃","L";"呒","M";"旀","N";"噢","O";"妑","P";"七","Q";"囕","R";"仨","S";"他","T";"屲","W";"夕","X";"丫","Y";"帀","Z"}
假设你的文字在A列第一行,B1写公式:
=VLOOKUP(LEFT(A1),拼音,2)&VLOOKUP(MID(A1,2,1),拼音,2)&VLOOKUP(MID(A1,3,1),拼音,2)&VLOOKUP(MID(A1,4,1),拼音,2)&VLOOKUP(MID(A1,5,1),拼音,2)&VLOOKUP(MID(A1,6,1),拼音,2)&VLOOKUP(MID(A1,7,1),拼音,2)&VLOOKUP(MID(A1,8,1),拼音,2)
这个公式可以取得前8的首字母,你可以根据实际需要修改。
最后100p和刚才的结果用&连续一下。
获取首字母需要对汉字表和字母表进行映射,如下示例代码是以gb2312编码为入手点,进行匹配的,也可以使用gbk、utf-8等编码进行匹配,但代码就完全不同了。
示例代码如下:
public class FirstLetterUtils {
// 简体中文的编码范围从B0A1(45217)一直到F7FE(63486)
private static int BEGIN = 45217;
private static int END = 63486;
// 按照声 母表示,这个表是在GB2312中的出现的第一个汉字,也就是说“啊”是代表首字母a的第一个汉字。
// i, u, v都不做声母, 自定规则跟随前面的字母
private static char[] chartable = { '啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈', '击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然', '撒', '塌', '塌', '塌', '挖', '昔', '压', '匝', };
// 二十六个字母区间对应二十七个端点
// GB2312码汉字区间十进制表示
private static int[] table = new int[27];
// 对应首字母区间表
private static char[] initialtable = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 't', 't', 'w', 'x', 'y', 'z', };
// 初始化
static {
for (int i = 0; i < 26; i++) {
table[i] = gbValue(chartable[i]);// 得到GB2312码的首字母区间端点表,十进制。
}
table[26] = END;// 区间表结尾
}
// ------------------------public方法区------------------------
// 根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串 最重要的一个方法,思路如下:一个个字符读入、判断、输出
public static String cn2py(String SourceStr) {
String Result = "";
int StrLength = SourceStrlength();
int i;
try {
for (i = 0; i < StrLength; i++) {
Result += Char2Initial(SourceStrcharAt(i));
}
} catch (Exception e) {
Result = "";
eprintStackTrace();
}
return Result;
}
// ------------------------private方法区------------------------
/
输入字符,得到他的声母,英文字母返回对应的大写字母,其他非简体汉字返回 '0'
/
private static char Char2Initial(char ch) {
// 对英文字母的处理:小写字母转换为大写,大写的直接返回
if (ch >= 'a' && ch <= 'z') {
return (char) (ch - 'a' + 'A');
}
if (ch >= 'A' && ch <= 'Z') {
return ch;
}
// 对非英文字母的处理:转化为首字母,然后判断是否在码表范围内,
// 若不是,则直接返回。
// 若是,则在码表内的进行判断。
int gb = gbValue(ch);// 汉字转换首字母
if ((gb < BEGIN) || (gb > END))// 在码表区间之前,直接返回
{
return ch;
}
int i;
for (i = 0; i < 26; i++) {// 判断匹配码表区间,匹配到就break,判断区间形如“[,)”
if ((gb >= table[i]) && (gb < table[i + 1])) {
break;
}
}
if (gb == END) {// 补上GB2312区间最右端
i = 25;
}
return initialtable[i]; // 在码表区间中,返回首字母
}
/
取出汉字的编码 cn 汉字
/
private static int gbValue(char ch) {// 将一个汉字(GB2312)转换为十进制表示。
String str = new String();
str += ch;
try {
byte[] bytes = strgetBytes("GB2312");
if (byteslength < 2) {
return 0;
}
return (bytes[0] << 8 & 0xff00) + (bytes[1] & 0xff);
} catch (Exception e) {
return 0;
}
}
public static void main(String[] args) throws Exception {
Systemoutprintln(cn2py("这是一个获取首字母的class"));
}
}
本文实例讲述了PHP自定义函数获取汉字首字母的方法。分享给大家供大家参考,具体如下:
首字母很重要,可以进行排序使用。
城市列表等等。
<php
/
Created
on
2016-12-1
/
function
getFirstCharter($str)
{
if
(empty($str))
{
return
'';
}
$fchar
=
ord($str{0});
if
($fchar
>=
ord('A')
&&
$fchar
<=
ord('z'))
return
strtoupper($str{0});
$s1
=
iconv('UTF-8',
'gb2312',
$str);
$s2
=
iconv('gb2312',
'UTF-8',
$s1);
$s
=
$s2
==
$str
$s1
:
$str;
$asc
=
ord($s{0})
256
+
ord($s{1})
-
65536;
if
($asc
>=
-20319
&&
$asc
<=
-20284)
return
'A';
if
($asc
>=
-20283
&&
$asc
<=
-19776)
return
'B';
if
($asc
>=
-19775
&&
$asc
<=
-19219)
return
'C';
if
($asc
>=
-19218
&&
$asc
<=
-18711)
return
'D';
if
($asc
>=
-18710
&&
$asc
<=
-18527)
return
'E';
if
($asc
>=
-18526
&&
$asc
<=
-18240)
return
'F';
if
($asc
>=
-18239
&&
$asc
<=
-17923)
return
'G';
if
($asc
>=
-17922
&&
$asc
<=
-17418)
return
'H';
if
($asc
>=
-17417
&&
$asc
<=
-16475)
return
'J';
if
($asc
>=
-16474
&&
$asc
<=
-16213)
return
'K';
if
($asc
>=
-16212
&&
$asc
<=
-15641)
return
'L';
if
($asc
>=
-15640
&&
$asc
<=
-15166)
return
'M';
if
($asc
>=
-15165
&&
$asc
<=
-14923)
return
'N';
if
($asc
>=
-14922
&&
$asc
<=
-14915)
return
'O';
if
($asc
>=
-14914
&&
$asc
<=
-14631)
return
'P';
if
($asc
>=
-14630
&&
$asc
<=
-14150)
return
'Q';
if
($asc
>=
-14149
&&
$asc
<=
-14091)
return
'R';
if
($asc
>=
-14090
&&
$asc
<=
-13319)
return
'S';
if
($asc
>=
-13318
&&
$asc
<=
-12839)
return
'T';
if
($asc
>=
-12838
&&
$asc
<=
-12557)
return
'W';
if
($asc
>=
-12556
&&
$asc
<=
-11848)
return
'X';
if
($asc
>=
-11847
&&
$asc
<=
-11056)
return
'Y';
if
($asc
>=
-11055
&&
$asc
<=
-10247)
return
'Z';
return
null;
}
$firstChar
=
getFirstCharter('脚本之家');
print_r($firstChar);//输出:J
>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php常用函数与技巧总结》、《PHP数组(Array) *** 作技巧大全》、《php字符串(string)用法总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库 *** 作入门教程》及《php常见数据库 *** 作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
string str;
str是你要判断的字符串。
char c;
c是你要判断的字符。
如果你仅仅是判断字符,那么最简单的方法是使用
str[0] == c
string s;
如果不是判断字符而是字符串s,那么就只能使用
strStartsWith(s);
或者strIndexOf(s) == 0。不过推荐前者,因为可读性高。
另外,你也可以使用正则表达式来判断。不过在这个问题上有点大材小用。
以上就是关于C语言中。字符指针取出来的都是首字母。但是我想要的是那个首字母连带的整个单词。应该怎么办全部的内容,包括:C语言中。字符指针取出来的都是首字母。但是我想要的是那个首字母连带的整个单词。应该怎么办、php批量获取首字母(汉字、数字、英文)、提取表格中文字的首字母等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)