Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Hallo Leute, hab eine Frage zum „splitten“ von Strings. Es sieht folgendermaßen aus. Ich haben einen String diesen will ich in 5 Teile teilen. Wie mache ich das am besten? Bis jetzt habe ich folgendes gemacht:


public static String NoSplitting(){

		int i = StringKek.length();

		float x = i/5;

		//runden

	int kekParts = Math.round(x);

		if(i % kekParts > 0)

		  kekParts += 1;


		ArrayList myStringParts = new ArrayList();

			int k = 0;

		for(int j = 0; j < kekParts; j++)

		if(j == (kekParts - 1)){


			String myPart = StringKek.substring(k);

			myStringParts.add(myPart);

		}else

		{

				String myPart = StringKek.substring(k, 5 * (j+1));

				k = 5 * (j+1);

				myStringParts.add(myPart);


		}

		return myStringParts.toString();		

	}


Das ergibt folgendes Ergebnis:

No: a57e1d2ab03952072150ceadf7295e843cc25f7e0cf74eaac8a46d766de4fe7b

Splitted - No: [a57e1, d2ab0, 39520, 72150, ceadf, 7295e, 843cc, 25f7e, 0cf74, eaac8, a46d7, 66de4, fe7b]

No ist der String der gesplittet werden soll. Splittet – No ist dann der geteilte String. Ich möchte nun aber nur 5 teile haben, nicht 13. Weiss jemand rat? Vielen Dank

Gruß

steve

Sorry, ich wollte Deinen Code nicht durch sehen, anbei mal meine Version ohne Garantie:


		String myString = "abcdefghijklmnopqrstuvwxyz";	

		int parts = 5;


		String[] myParts = new String[ parts ];

		for(int i=0; i < parts; i++) {

			int start = i*myString.length()/parts;

			int end = (i+1)*myString.length()/parts;


			if (end > myString.length())

				end = myString.length();


			myParts[i] = myString.substring(start, end);

		}


		for(int i=0; i < parts; i++)

			System.out.println( myParts[i] );


HTH Phil

Excellent, hat funktioniert, super vielen dank!!!!

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.