kristofer a révisé ce gist . Aller à la révision
Aucun changement
kristofer a révisé ce gist . Aller à la révision
1 file changed, 154 insertions
string-work.md(fichier créé)
| @@ -0,0 +1,154 @@ | |||
| 1 | + | ## Working with Strings | |
| 2 | + | ||
| 3 | + | In both python and Java, samples of working with common string operations: split, | |
| 4 | + | capitalization, concatenation, replacing characters, working with sentences. | |
| 5 | + | ||
| 6 | + | Key differences: | |
| 7 | + | ||
| 8 | + | - Python: Has built-in title(), capitalize() methods; f-strings for formatting | |
| 9 | + | - Java: More verbose; often requires manual implementation or StringBuilder | |
| 10 | + | - Python: Strings are immutable but syntax is cleaner | |
| 11 | + | - Java: Strings are also immutable; use StringBuilder for efficiency with multiple operations | |
| 12 | + | ||
| 13 | + | ### in Python | |
| 14 | + | ||
| 15 | + | ```python | |
| 16 | + | # Split operations | |
| 17 | + | text = "hello world python" | |
| 18 | + | words = text.split() # ['hello', 'world', 'python'] | |
| 19 | + | csv = "apple,banana,orange" | |
| 20 | + | fruits = csv.split(',') # ['apple', 'banana', 'orange'] | |
| 21 | + | ||
| 22 | + | # Capitalization | |
| 23 | + | name = "john doe" | |
| 24 | + | print(name.capitalize()) # "John doe" | |
| 25 | + | print(name.title()) # "John Doe" | |
| 26 | + | print(name.upper()) # "JOHN DOE" | |
| 27 | + | print(name.lower()) # "john doe" | |
| 28 | + | ||
| 29 | + | # Concatenation | |
| 30 | + | # Method 1: + operator | |
| 31 | + | greeting = "Hello" + " " + "World" # "Hello World" | |
| 32 | + | ||
| 33 | + | # Method 2: join() - more efficient for multiple strings | |
| 34 | + | words = ["Python", "is", "awesome"] | |
| 35 | + | sentence = " ".join(words) # "Python is awesome" | |
| 36 | + | ||
| 37 | + | # Method 3: f-strings (Python 3.6+) | |
| 38 | + | name = "Alice" | |
| 39 | + | age = 25 | |
| 40 | + | message = f"{name} is {age} years old" # "Alice is 25 years old" | |
| 41 | + | ||
| 42 | + | # Replacing characters/substrings | |
| 43 | + | text = "Hello World" | |
| 44 | + | new_text = text.replace("World", "Python") # "Hello Python" | |
| 45 | + | phone = "123-456-7890" | |
| 46 | + | clean = phone.replace("-", "") # "1234567890" | |
| 47 | + | ||
| 48 | + | # Working with sentences | |
| 49 | + | sentence = "this is a sentence. and another one." | |
| 50 | + | # Capitalize first letter of each sentence | |
| 51 | + | sentences = sentence.split('. ') | |
| 52 | + | capitalized = '. '.join(s.capitalize() for s in sentences) | |
| 53 | + | # Result: "This is a sentence. And another one." | |
| 54 | + | ||
| 55 | + | # Strip whitespace | |
| 56 | + | messy = " hello world \n" | |
| 57 | + | clean = messy.strip() # "hello world" | |
| 58 | + | ||
| 59 | + | # Check string properties | |
| 60 | + | text = "Hello123" | |
| 61 | + | print(text.isalnum()) # True | |
| 62 | + | print(text.isalpha()) # False | |
| 63 | + | print(text.startswith("Hello")) # True | |
| 64 | + | print(text.endswith("123")) # True | |
| 65 | + | ||
| 66 | + | ``` | |
| 67 | + | ||
| 68 | + | ||
| 69 | + | ### in Java | |
| 70 | + | ||
| 71 | + | ```java | |
| 72 | + | // Split operations | |
| 73 | + | String text = "hello world java"; | |
| 74 | + | String[] words = text.split(" "); // ["hello", "world", "java"] | |
| 75 | + | String csv = "apple,banana,orange"; | |
| 76 | + | String[] fruits = csv.split(","); // ["apple", "banana", "orange"] | |
| 77 | + | ||
| 78 | + | // Capitalization | |
| 79 | + | String name = "john doe"; | |
| 80 | + | // Capitalize first letter only | |
| 81 | + | String capitalized = name.substring(0, 1).toUpperCase() + name.substring(1); | |
| 82 | + | // Result: "John doe" | |
| 83 | + | ||
| 84 | + | // Title case (manual - Java doesn't have built-in) | |
| 85 | + | String[] words = name.split(" "); | |
| 86 | + | StringBuilder titleCase = new StringBuilder(); | |
| 87 | + | for (String word : words) { | |
| 88 | + | titleCase.append(word.substring(0, 1).toUpperCase()) | |
| 89 | + | .append(word.substring(1).toLowerCase()) | |
| 90 | + | .append(" "); | |
| 91 | + | } | |
| 92 | + | String title = titleCase.toString().trim(); // "John Doe" | |
| 93 | + | ||
| 94 | + | // Upper and lower case | |
| 95 | + | String upper = name.toUpperCase(); // "JOHN DOE" | |
| 96 | + | String lower = name.toLowerCase(); // "john doe" | |
| 97 | + | ||
| 98 | + | // Concatenation | |
| 99 | + | // Method 1: + operator (creates new strings) | |
| 100 | + | String greeting = "Hello" + " " + "World"; // "Hello World" | |
| 101 | + | ||
| 102 | + | // Method 2: StringBuilder (efficient for multiple operations) | |
| 103 | + | StringBuilder sb = new StringBuilder(); | |
| 104 | + | sb.append("Java").append(" is ").append("awesome"); | |
| 105 | + | String sentence = sb.toString(); // "Java is awesome" | |
| 106 | + | ||
| 107 | + | // Method 3: String.join() | |
| 108 | + | String[] words = {"Java", "is", "awesome"}; | |
| 109 | + | String joined = String.join(" ", words); // "Java is awesome" | |
| 110 | + | ||
| 111 | + | // Method 4: String.format() | |
| 112 | + | String name = "Alice"; | |
| 113 | + | int age = 25; | |
| 114 | + | String message = String.format("%s is %d years old", name, age); | |
| 115 | + | // Result: "Alice is 25 years old" | |
| 116 | + | ||
| 117 | + | // Replacing characters/substrings | |
| 118 | + | String text = "Hello World"; | |
| 119 | + | String newText = text.replace("World", "Java"); // "Hello Java" | |
| 120 | + | String phone = "123-456-7890"; | |
| 121 | + | String clean = phone.replace("-", ""); // "1234567890" | |
| 122 | + | ||
| 123 | + | // Replace first occurrence only | |
| 124 | + | String repeated = "abc abc abc"; | |
| 125 | + | String replaced = repeated.replaceFirst("abc", "xyz"); // "xyz abc abc" | |
| 126 | + | ||
| 127 | + | // Working with sentences | |
| 128 | + | String sentence = "this is a sentence. and another one."; | |
| 129 | + | // Capitalize sentences (manual approach) | |
| 130 | + | String[] sentences = sentence.split("\\. "); | |
| 131 | + | StringBuilder result = new StringBuilder(); | |
| 132 | + | for (String s : sentences) { | |
| 133 | + | if (!s.isEmpty()) { | |
| 134 | + | result.append(Character.toUpperCase(s.charAt(0))) | |
| 135 | + | .append(s.substring(1)) | |
| 136 | + | .append(". "); | |
| 137 | + | } | |
| 138 | + | } | |
| 139 | + | String capitalized = result.toString().trim(); | |
| 140 | + | // Result: "This is a sentence. And another one." | |
| 141 | + | ||
| 142 | + | // Trim whitespace | |
| 143 | + | String messy = " hello world \n"; | |
| 144 | + | String clean = messy.trim(); // "hello world" | |
| 145 | + | ||
| 146 | + | // Check string properties | |
| 147 | + | String text = "Hello123"; | |
| 148 | + | System.out.println(text.matches("[a-zA-Z0-9]+")); // true (alphanumeric) | |
| 149 | + | System.out.println(text.matches("[a-zA-Z]+")); // false (not just letters) | |
| 150 | + | System.out.println(text.startsWith("Hello")); // true | |
| 151 | + | System.out.println(text.endsWith("123")); // true | |
| 152 | + | ||
| 153 | + | ``` | |
| 154 | + | ||
Plus récent
Plus ancien