מהם התווים החדשים ב-Java?
מה מדפיס "\n" ב-Java?
בואו נסתכל על דוגמה של הדפסת "\n" על המסוף.דוגמא
public class NewLineChar {
public static void main(String[] args) {
System.out.println("Hi, I am Sponge Bob.\nI just signed up at Code Gym.\nI love the Java learning experience here so far!");
}
}
תְפוּקָה
היי, אני בוב ספוג. הרגע נרשמתי ל-Code Gym. אני אוהב את חווית הלימוד של Java כאן עד כה!
כיצד להוסיף תו חדש למחרוזת?
ישנן דרכים מרובות להוסיף את תו השורה החדשה למחרוזת. אתה יכול להשתמש ב-"\n" או "\r\n" כדי להוסיף מעבר שורה בפסקה רציפה. בואו נסתכל על כמה דרכים לעשות זאת.דוגמא
public class NewLineChar {
public static void main(String[] args) {
// Method 1: newline after every sentence
String sentence1 = "What a beautiful day to be alive!";
String sentence2 = "Let's be thankful for all the blessings we've been showered with.";
String sentence3 = "It only makes sense to be helpful, cooperative and generous to those who are less fortunate.";
// without newline after each sentence
System.out.println("-------Printing without newline character--------");
System.out.println(sentence1 + sentence2 + sentence3);
// newline character after each sentence
System.out.println("\n-------Printing with newline character-----------");
System.out.println(sentence1 + "\n" + sentence2 + "\n" + sentence3);
// Method 2: new line after each paragraph
String paragraph1 = "Java is the most dynamic and easy to plug and play language. It has been transforming lives for more than 2 decades now. It still continues to grow to date.";
String paragraph2 = "If you're a beginner who wants to be a professional in Java, you need to focus on targeted learning with consistent practice. If you're looking for a responsive community to grow with, then Code Gym is the way to go!";
// without newline after each paragraph
System.out.println("\n-------Printing without newline character--------");
System.out.println(paragraph1 + paragraph2);
// new line character after each paragraph
System.out.println("\n-------Printing with newline character-----------");
System.out.println(paragraph1 + "\r\n" + paragraph2);
}
}
תְפוּקָה
-------הדפסה ללא דמות חדשה-------- איזה יום יפה לחיות! בואו נהיה אסירי תודה על כל הברכות שהרעיפו עלינו. זה רק הגיוני להיות מועיל, לשתף פעולה ונדיב למי שפחות בר מזל. -------הדפסה עם תו חדש----------- איזה יום יפה לחיות! בואו נודה על כל הברכות שהרעיפו עלינו. זה רק הגיוני להיות מועיל, משתף פעולה ונדיב למי שפחות ברי מזל. -------הדפסה ללא תו חדש-------- Java היא השפה הדינמית והקלה ביותר לחיבור ולהפעלה. זה משנה חיים כבר יותר מ-2 עשורים. זה עדיין ממשיך לצמוח עד היום. אם אתה מתחיל שרוצה להיות מקצוען ב-Java, אתה צריך להתמקד בלמידה ממוקדת עם תרגול עקבי. אם אתם מחפשים קהילה מגיבה לצמוח איתה, אז Code Gym היא הדרך ללכת! -------הדפסה עם תו חדש----------- Java היא השפה הדינמית והקלה ביותר לחיבור ולהפעלה. זה משנה חיים כבר יותר מ-2 עשורים. זה עדיין ממשיך לגדול עד היום. אם אתה מתחיל שרוצה להיות מקצוען ב-Java, אתה צריך להתמקד בלמידה ממוקדת עם תרגול עקבי. אם אתם מחפשים קהילה מגיבה לצמוח איתה, אז Code Gym היא הדרך ללכת!
GO TO FULL VERSION