简体   繁体   中英

How to modify a string into another string?

I have a string like this:

sunday-monday-tuesday-wednesday-thursday-friday-saturday

How to modify it into:

sun-mon-tue-wed-thu-fri-sat

There are many ways to go about this problem, I have shown two:

1. Make an array with all the days of the week, then divide them with "-"

To print the first 3 letters of a string, you can make an array, and using a for loop, print only the first 3 characters, dividing them with "-". Your program can look like this:

String days = "";
String[] str = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
for (int i = 0; i < str.length; i++) {
    String word = str[i].replace(str[i].substring(3), "") + "-";
    days += word;
}
System.out.println(days.substring(0, days.length()-1));

This will replace everything past the first 3 characters with nothing, add "-" to the end of it, and trim the last character, which is the "-" after "sat". This gives the output:

sun-mon-tue-wed-thu-fri-sat

2. Make a string with all the days of the week, split them into an array, then divide the members with "-"

This option is if you want to keep your original String . This will split the String wherever "-" is found. It will then do the same as the first program, use a for loop to print only the first 3 characters, divide them with "-", and trim the last character. Program below:

String days = "";
String x = "sunday-monday-tuesday-wednesday-thursday-friday-saturday";
String [] str = x.split("-");
for (int i = 0; i < str.length; i++) {
    String word = str[i].replace(str[i].substring(3), "") + "-";
    days += word;
}
System.out.println(days.substring(0, days.length()-1));

And the program gives the same output:

sun-mon-tue-wed-thu-fri-sat

Both programs are very similar, the only difference being that number 1 already has separate members, whereas number 2 keeps the original String and divides it into the array. Please comment below if you have any questions.

One possible way to achieve that output is like so:

String test = "sunday-monday-tuesday-wednesday-thursday-friday-saturday";
String [] arr = test.split("-");
String res = "";
int len = arr.length;
for(int i=0 ;i<len;i++){
    res = res + arr[i].substring(0, 3);
    if(i<len-1){
        res = res+"-";
    }
}
System.out.println(res);

And the output is how you desired:

sun-mon-tue-wed-thu-fri-sat

This program will only print the first 3 characters of each day of the week, and divide them with "-".

A String is immutable in Java, so you'll have to assign it to another String . In your case, we can do the following with Java 8:

String days = "sunday-monday-tuesday-wednesday-thursday-friday-saturday";

days = String.join("-", Arrays.stream(days.split("-"))
                                    .map(s -> s.substring(0, 3))
                                    .toArray(String[]::new));

System.out.println(days);

>> sun-mon-tue-wed-thu-fri-sat

If you don't want to use Java 8 and its Stream API, then you can write the following:

String days = "sunday-monday-tuesday-wednesday-thursday-friday-saturday";

StringBuilder sb = new StringBuilder();

for (String day : days.split("-")) {
    sb.append(day.substring(0, 3)).append('-');
}

days = sb.deleteCharAt(sb.length() - 1).toString();

System.out.println(days);

>> sun-mon-tue-wed-thu-fri-sat
String printWord = string.Empty;

    String[] str = { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" };
    for (int i = 0; i < str.Length; i++)
    {
        printWord += str[i].Replace(str[i].Substring(3), "") + "-";
    }


    lblresult.Text = printWord.TrimEnd('-');

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