简体   繁体   中英

how to cut words and characters from a string in java

i want to remove from a string the first word and the first letter from the second word. any way to do that ?

for example,

String a = "hello world!"

result must be:

"orld!"

I tried using the method substring but didn't know how to use it properly. thanks

int index = a.indexOf(" ");
String result = index == -1 || index == a.length() - 1 ? "" : a.substring(index + 2);
String helloWorld = "Hello World!";

String newString = helloWorld.substring ( 7 , helloWorld.length ( ) );

EDIT: the first parameter in substring is the character you want to begin on, for instance, in "Hello World!" the W in World is the 7nth character. The second Parameter is the last character to end on. In this case just choose the length of the String to end on the last character.

It works for me

String str = "hello world";
String[] strs = str.split(" ");
StringBuilder builder = new StringBuilder();
builder.append(strs[1].substring(1));
for (int i = 2; i < strs.length; i++) {
    builder.append(strs[i]);
}
System.out.println(builder);
    String newString = "";
    String s = "I am from China!";
    String[] arr = s.split(" ");
    for (int i = 0; i < arr.length; i++) {
        if (i == 0)
            continue;
        newString += arr[i] + " ";
    }
    newString = newString.substring(1, newString.length() - 1);
    System.out.println("result:"+newString);

result:m from China!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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