latest pushes

This commit is contained in:
2024-10-04 21:12:19 -04:00
parent 159fd823a7
commit a4dfa81ed4
6 changed files with 265 additions and 129 deletions

View File

@ -4,66 +4,33 @@
# Demonstrating string manipulation
# String concatenation
str1="Hello"
str2="World"
concat="$str1 $str2"
echo "Concatenated string: $concat"
first_name=John
last_name=Doe
full_name=$first_name $last_name
echo Full name: $full_name
# String length
echo "Length of '$concat': ${#concat}"
string="Hello, World!"
echo "The string '$string' has ${#string} characters."
# Substring extraction
echo "Substring (index 0-4): ${concat:0:5}"
echo "Substring (from index 6): ${concat:6}"
echo "The first 5 characters are: ${string:0:5}"
# String replacement
sentence="The quick brown fox jumps over the lazy dog"
echo "Original sentence: $sentence"
replaced=${sentence/fox/cat}
echo "After replacing 'fox' with 'cat': $replaced"
new_sentence=${sentence/fox/cat}
echo "Modified sentence: $new_sentence"
# Replace all occurrences
many_the="the the the dog the cat the mouse"
replaced_all=${many_the//the/a}
echo "Replace all 'the' with 'a': $replaced_all"
# Converting to uppercase and lowercase
echo "Uppercase: ${string^^}"
echo "Lowercase: ${string,,}"
# String to uppercase
uppercase=${sentence^^}
echo "Uppercase: $uppercase"
# String to lowercase
lowercase=${sentence,,}
echo "Lowercase: $lowercase"
# Check if string contains substring
if [[ $sentence == *"fox"* ]]; then
echo "The sentence contains 'fox'"
else
echo "The sentence does not contain 'fox'"
fi
# Split string into array
IFS=' ' read -ra words <<< "$sentence"
echo "Words in the sentence:"
for word in "${words[@]}"; do
echo " $word"
done
# Join array elements into string
joined=$(IFS=", "; echo "${words[*]}")
echo "Joined words: $joined"
# Trim whitespace
whitespace_string=" trim me "
trimmed_string=$(echo $whitespace_string | xargs)
echo "Original string: '$whitespace_string'"
# Trimming whitespace
padded_string=" trim me "
echo "Original string: '$padded_string'"
trimmed_string=${padded_string## }
trimmed_string=${trimmed_string%% }
echo "Trimmed string: '$trimmed_string'"
# String comparison
str_a="apple"
str_b="banana"
if [[ $str_a < $str_b ]]; then
echo "$str_a comes before $str_b alphabetically"
else
echo "$str_b comes before $str_a alphabetically"
fi
echo "String manipulation operations completed."