范文无忧网面试笔试面试回答

使用Java的String类操作字符串和子串

01月09日 编辑 fanwen51.com

[数据结构有关字符串的面试题求解答]建一张hash表,记录a-z 26个字母的出现次数 char table[ 26 ] ; 第一题,假设第一个字符串为s1,第2个字符串为s2 for( i = 0 ; i table[ i ] = 0 ; for( i = 0 ; i table[ s1[i]...+阅读

public class Du02 {

public static void main(String[] args) {

String str = "I am a student,I am at ccit";

System.out.println(str.length());

System.out.println(str.charAt(0));

System.out.println(str.charAt(str.length()-1));//

int firstIndex = str.indexOf(" ");

System.out.println("First word: " + str.substring(0, firstIndex));

int lastIndex = str.lastIndexOf(" ");

System.out.println("Last word: " + str.substring(lastIndex));

System.out.println(str.replace('c', 'C')); //

String[] ary = str.replace(",", " ").split("\\s+");

for(int i =0; i < ary.length; i++){

if(!ary[i].trim().equals("")){

System.out.println(ary[i].trim());

}

}

System.out.println("String contains ma? " + str.concat(" ma "));

System.out.println("String starts with 'i', ends with 'i'? " + str.matches("I(.)*I"));

}

}

-----------testing

27

I

t

First word: I

Last word: ccit

I am a student,I am at CCit

I

am

a

student

I

am

at

ccit

String contains ma? I am a student,I am at ccit ma

String starts with 'i', ends with 'i'? false

延伸阅读:

c语言面试题关于字符串还有字符指针str是个字符指针,也可以表示为字符数组或者字符串,str = &a;表示str指向的地方只能存下一个字符。 strcpy(str, “hello”); 肯定放不下hello啊,所以会有内存错误 如果你不相信,...

c语言 2题目:字符串的处理 3题目:求一个字符串的子串程序2: #include <stdio.h> #include <stdlib.h> int main() { char str[100], c; int i, j; printf (”Enter string:”); gets (str); for ( i=0; 【 str[i] != '\0' 】; i+...

推荐阅读
图文推荐
栏目列表