Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Snippets] Added String Manipulation Snippets for java #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Mathys-Gasnier merged 23 commits into quicksnip-dev:main from Mcbencrafter:main
Jan 30, 2025
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9eb5875
case conversion snippets
Mcbencrafter Jan 18, 2025
93a89b7
ascii/unicode to/from string conversion snippets
Mcbencrafter Jan 18, 2025
d532525
tab/space conversion snippets
Mcbencrafter Jan 18, 2025
aa6683b
palindrome/anagram check snippets
Mcbencrafter Jan 18, 2025
872faf2
counting snippets
Mcbencrafter Jan 18, 2025
7d7d077
normalisation snippets
Mcbencrafter Jan 18, 2025
aa4cd5f
reverse snippets
Mcbencrafter Jan 18, 2025
7832e60
hide/truncate snippets
Mcbencrafter Jan 18, 2025
f5b07b5
slugify snippet
Mcbencrafter Jan 18, 2025
dee9622
password generator snippet
Mcbencrafter Jan 18, 2025
0e1bf3a
text between delimiters extraction snippet
Mcbencrafter Jan 18, 2025
ea96f63
finding snippets
Mcbencrafter Jan 18, 2025
51a63b1
renamed snippet for clarity
Mcbencrafter Jan 18, 2025
f330c02
corrected spaces to tabs snippet
Mcbencrafter Jan 18, 2025
648c6bf
added accidentally removed file
Mcbencrafter Jan 18, 2025
41c0404
tried to fix accidentally removed pre-commit file
Mcbencrafter Jan 18, 2025
abd118f
tried to revert pre commit file
Mcbencrafter Jan 18, 2025
31ec9f8
Tried to restore pre-commit file
Mcbencrafter Jan 18, 2025
b1d436c
Merge branch 'main' into main
Mcbencrafter Jan 18, 2025
e15fc2a
replaced count consonants/vowels with a more generic snippet
Mcbencrafter Jan 19, 2025
12abe5f
Merge branch 'main' of https://github.com/Mcbencrafter/quicksnip
Mcbencrafter Jan 19, 2025
c7b4d83
fixed remove punctuation snippet
Mcbencrafter Jan 19, 2025
9f019a9
reset pre-commit file to origin/main because i messed up
Mcbencrafter Jan 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
password generator snippet
  • Loading branch information
Mcbencrafter committed Jan 18, 2025
commit dee9622a9f84893a84e77f44db0d3cffd91b099f
38 changes: 38 additions & 0 deletions snippets/java/string-manipulation/password-generator.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: Password Generator
description: Generates a random string with specified length and character set, including options for letters, numbers, and special characters
author: Mcbencrafter
tags: string,password,generator,security,random,token
---

```java
public static String randomString(int length, boolean useLetters, boolean useNumbers, boolean useSpecialCharacters) {
String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String numbers = "0123456789";
String specialCharacters = "!@#$%^&*()_+-=[]{}|;:,.<>?";

String allowedCharacters = "";

if (useLetters)
allowedCharacters += characters;

if (useNumbers)
allowedCharacters += numbers;

if (useSpecialCharacters)
allowedCharacters += specialCharacters;

SecureRandom random = new SecureRandom();
StringBuilder result = new StringBuilder(length);

for (int i = 0; i < length; i++) {
int index = random.nextInt(allowedCharacters.length());
result.append(allowedCharacters.charAt(index));
}

return result.toString();
}

// Usage:
System.out.println(randomString(10, true, true, false)); // Random string containing letters, numbers but no special characters with 10 characters
```

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /