简体   繁体   English

对于给定的字符串数组,如何在字符串的索引处打印该字符串的字符

[英]How would you print out the character of that string at the index of the string for a given array of Strings

Sorry my question is kind of hard to understand. 抱歉,我的问题有点难以理解。 So in a array such as String []arr = {"abs, "def", "ghi"}; I want to know how it can print out this aei 因此,在诸如String [] arr = {“ abs,” def“,” ghi“}等数组中;我想知道它如何打印出此aei

this is my code that i have tried to write but failed. 这是我尝试编写但失败的代码。

public static void printCharsAt(String[] arr) { 
    System.out.println("how many inputs do you want in your array");
    String array[] = {"hello", "Ap", "CS"};        
    System.out.println("What term of the array do you want, do you wan the first, 2nd, or third");
    tnt char = kb.nextInt(); 
    System.out.println(array[char]);
}

Without any bounds checking: 没有任何界限检查:

for (int i=0; i<arr.length; i++)
    System.out.println(arr[i].charAt(i));

You have to loop through each String in the array. 您必须遍历数组中的每个String。 You may use for each loop in Java or the traditional for loop. 您可以使用Java中的for each循环或传统的for循环。 Once you get a String, You can use the charAt() method of String class to obtain the character at a particular position. 一旦获得字符串,就可以使用String类的charAt()方法来获取特定位置的字符。 Don't forget the length method also, It may come in handy. 也不要忘记length方法,它可能派上用场。 No code, since it is homework :) 没有代码,因为这是家庭作业:)

注意charAt(int index)方法

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

相关问题 给定一个字符串数组,如何找出特定字符串的索引? - Given an array of Strings, how to find out the index of a particular String? 如何在字符串数组中找到给定String的索引? - How to find an index of given String in array of strings? 按字符打印给定的字符串 - Print given string character by character 使用for循环在字符串数组中打印索引号和该索引中的字符 - Using a for loop to print index number and the character in that index in a string array 如何在Java中打印给定字符串中的字符、数字和特殊字符? - How to print character, Number and special character from given String in Java? 如何完全按原样打印字符串? - How do you print out a string exactly as it is? 您将如何在不使用数组而仅使用字符串的情况下按字母顺序排列单词? - How would you alphabetize words with out using array and only strings? 您如何将字符串转换为数组/数组列表 - How would you convert A string into A Array/ArrayList 在给定字符串组合生成的字符串中找出字典中第一个出现的字符串 - Find out string that would come in first in dictionary among strings generated by combination of given strings 如何将字符串的二维数组打印为String - How to print two dimensional array of strings as String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM